DeveloperBreeze

Related Posts

More content you might like

Tutorial
php

Securing Laravel Applications Against Common Vulnerabilities

Redirect all HTTP traffic to HTTPS in AppServiceProvider:

   use Illuminate\Support\Facades\URL;

   public function boot()
   {
       if (app()->environment('production')) {
           URL::forceScheme('https');
       }
   }

Nov 16, 2024
Read More
Tutorial
php

Building a Custom Pagination System for API Responses

   public function index(Request $request)
   {
       $limit = $request->get('limit', 10);
       $cursor = $request->get('cursor');

       $query = Post::query();

       if ($cursor) {
           $query->where('id', '>', $cursor); // Fetch items after the cursor
       }

       $posts = $query->orderBy('id')->take($limit + 1)->get();

       $nextCursor = $posts->count() > $limit ? $posts->last()->id : null;

       return response()->json([
           'data' => $posts->take($limit), // Return only the requested number of items
           'meta' => [
               'limit' => $limit,
               'next_cursor' => $nextCursor,
           ],
       ]);
   }
   {
       "data": [
           { "id": 11, "title": "Post 11" },
           { "id": 12, "title": "Post 12" }
       ],
       "meta": {
           "limit": 10,
           "next_cursor": 20
       }
   }

Nov 16, 2024
Read More
Tutorial
php

Laravel Best Practices for Sharing Data Between Views and Controllers

Use Laravel's View::share method to make data available to all views.

Open or create a service provider like AppServiceProvider:

Nov 16, 2024
Read More
Tutorial
php

Creating a Configurable Pagination System in Laravel

Open any controller where pagination is used. For example:

   namespace App\Http\Controllers;

   use App\Models\Post;

   class PostController extends Controller
   {
       public function index()
       {
           $paginationLimit = getSetting('pagination_limit', 10);

           $posts = Post::paginate($paginationLimit);

           return view('posts.index', compact('posts'));
       }
   }

Nov 16, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!