Search⌘ K
AI Features

CRUD at Your Fingertips

Explore how to implement CRUD operations for restaurant management APIs in Laravel. Understand validation, API resources to customize responses, authorization checks to secure updates, and soft deletes for archiving records. Test and manage these endpoints using Postman with authentication.

Add a restaurant

First, we will cover the add restaurant functionality.

PHP
<?php
namespace App\Http\Controllers;
use App\Http\Requests\RestaurantValidation;
use App\Models\Restaurant;
use Illuminate\Support\Facades\Auth;
class RestaurantController extends Controller
{
public function store(RestaurantValidation $request): array
{
$restaurant = Restaurant::create(array_merge($request->validated(), [
'user_id' => Auth::id(),
]));
return [
'restaurant_id' => $restaurant->id,
];
}
// ...
// Methods to handle list, update, and archive functionality
// are not implemented for brevity. API calls for them will
// not work while you're running code from this widget.
}
  1. The RestaurantValidation form request class was already covered in the last lesson.
  2. We append the user_id to the validated data array to create a new restaurant record.
  3. Return the auto-generated id of the new restaurant for reference.

List restaurants

Time to cover one more super helpful Laravel feature: API resources. We do not always need to share all the columns of the table in ...