CRUD at Your Fingertips
Discover one of the many ways to keep your resource controllers clean and use various Laravel features to easily offer the CRUD functionality.
We'll cover the following...
Add a restaurant
First, we will cover the add restaurant functionality.
Press + to interact
<?phpnamespace 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.}
- The
RestaurantValidation
form request class was already covered in the last lesson. - We append the
user_id
to the validated data array to create a new restaurant record. - 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 the ...