1. Go to this page and download the library: Download ysm/responsable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ysm / responsable example snippets
'providers' => [
// Other providers
YSM\Responsable\ResponsableServiceProvider::class,
],
namespace App\Http\Controllers;
use App\Http\Filters\PostFilter;
use App\Http\Resources\PostResource;
use App\Models\Post;
use Illuminate\Support\Facades\Response;
class PostController extends Controller
{
public function index()
{
$posts = Post::filterable(PostFilter::class)->limit(5)->get();
return Response::success(
message: 'Posts fetched success',
data: PostResource::collection($posts)
);
}
}
public function paginated()
{
$posts = Post::filterable(PostFilter::class)->paginate(5);
return Response::success(
message: 'Posts fetched success',
data: PostResource::collection($posts),
paginator: $posts
);
}
public function errors(Request $request)
{
if (isset($request->case)) {
return Response::success('Posts fetched success');
}
return Response::error('Posts fetched error');
}
#### Example 1: Fetching a Collection of Posts with Helpers
public function index()
{
$posts = Post::filterable(PostFilter::class)->limit(5)->get();
return success('Posts fetched success', PostResource::collection($posts));
}
#### Example 3: Handling JSON Errors with Helpers
public function errors(Request $request)
{
if (isset($request->case)) {
return success('Posts fetched success');
}
return error('Posts fetched error');
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'created successfully', 201, ['title' => $validated['title']]);
}