My first trial day at ENATS

 Worked on : Laravel Pagination & API Development


Hello friends,

Today, I embarked on my first trial day at a small firm with a team of 10. After a successful interview, I was eager to dive into real-world development tasks. Here's a detailed account of my day:


Morning Session: Implementing Pagination in a CRUD Application

My day began at 9:30 AM amidst heavy rainfall. Upon arrival, I observed the team working diligently. After a brief period, I approached my supervisor to inquire about my tasks. He assigned me the responsibility of adding pagination to an existing CRUD application—a project I had worked on during the interview.

To efficiently manage a large dataset, I utilized Laravel's seeder functionality to generate fake data. This allowed me to populate the database with sufficient records for testing pagination.

For pagination, I employed Laravel's paginate() method to limit the number of records displayed per page. To enhance the user interface, I integrated Bootstrap 5 pagination by calling the links() method in the view, ensuring a seamless and responsive pagination experience.

Code Snippet:

// In Controller
$posts = Post::paginate(10);

// In Blade View
{{ $posts->links('pagination::bootstrap-5') }}

Afternoon Session: Diving into API Development

Post-lunch, I was tasked with learning about API development—a domain I was unfamiliar with. I dedicated the remainder of the day to studying Laravel's documentation and exploring ChatGPT for guidance.

In Laravel 12, the api.php route file isn't included by default. To set up API routes, I manually created the routes/api.php file and registered it in bootstrap/app.php.

Steps to Set Up API Routes:

  1. Create the api.php File:

    php artisan make:controller Api/PostController
  2. Define API Routes:

    // In routes/api.php
    use App\Http\Controllers\Api\PostController;
    Route::apiResource('posts', PostController::class);
  3. Register the api.php File:

    // In bootstrap/app.php
    $app->withRouting(
        web: __DIR__.'/../routes/web.php',
        api: __DIR__.'/../routes/api.php',
        commands: __DIR__.'/../routes/console.php',
    );
  4. Create Model, Migration, and Controller:

    php artisan make:model Post -mcr
  5. Update Migration File:

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }
  6. Define Controller Methods:

    public function index()
    {
        return Post::all();
    }
    
    public function store(Request $request)
    {
        $data = $request->validate([
            'title' => 'required|string',
            'body' => 'required|string',
        ]);
        return Post::create($data);
    }

By the end of the day, I had successfully set up basic API routes and operations. The next step involves implementing token-based authentication, which I plan to tackle in the coming days.


Conclusion:

My first trial day was both challenging and rewarding. I gained hands-on experience with Laravel's pagination system and initiated my journey into API development. I'm excited about the learning opportunities ahead and look forward to contributing more to the team.

Comments

Popular Posts