Only Authenticated Users Can Add Items to the Database
This tutorial is part of the Laravel Handbook. Download it from https://flaviocopes.com/access/
In this tutorial, we will be implementing the ability for authenticated users to add items to the database. Unlike our previous project, this time we will display the list of dogs even when users are logged out, but only logged-in users will have permission to modify the data.
To begin, let’s create a new migration for the “dogs” table:
php artisan make:migration create_dogs_table
Open the newly created migration file, which is located at database/migrations/2023_05_12_164831_create_dogs_table.php
, and update it as follows:
1 |
|
Save the file and run php artisan migrate
in your terminal to apply the migration.
Next, let’s scaffold the “Dog” model:
php artisan make:model Dog
In the routes/web.php
file, add the following line at the top to import the Dog
model:
1 | use App\Models\Dog; |
Then, locate the /
route and update it as follows, which retrieves the list of dogs and passes it to the “welcome” view:
1 | Route::get('/', function () { |
Now, open the resources/views/welcome.blade.php
file and modify it to loop over the dogs array using @foreach
directive:
1 | <h1 class="pb-2 mb-3 font-bold border-b border-b-gray-300"> |
Now, if you refresh the home page, you’ll see the list of dogs. However, since we don’t have any dogs in the database yet, the list will be empty.
To display an “empty state” when there are no dogs, you can use the @forelse
directive instead of @foreach
. Update the code in resources/views/welcome.blade.php
as follows:
1 | @forelse ($dogs as $dog) |
To add some dogs to the database, you can use SQL queries in a tool like TablePlus. Here’s an example SQL query to insert some dog data:
1 | INSERT INTO "dogs" ("id", "name", "created_at", "updated_at") VALUES |
Now, we’ll add the ability to add and delete dogs. First, let’s update the Dog
model to include the name
attribute in the $fillable
array:
1 | protected $fillable = ['name']; |
Next, let’s create a controller named DogController
:
php artisan make:controller DogController
Inside the DogController.php
file, add the following lines at the top to import the necessary classes:
1 | use App\Models\Dog; |
Then, add the create
and delete
methods to the DogController
class, which will handle the logic for adding and deleting dogs. Both methods will first check if the user is logged in before performing any actions:
1 | public function create(Request $request) |
Next, add the routes for creating and deleting dogs in the routes/web.php
file:
1 | use App\Http\Controllers\DogController; |
Finally, update the resources/views/welcome.blade.php
file to include buttons for deleting dogs and a form for adding new dogs:
1 | @forelse ($dogs as $dog) |
That’s it! Now, when logged in, you’ll see a form to add a new dog and a delete button next to each dog in the list. When logged out, you will only see the list of dogs.
Tags: Laravel, Authentication, Database, CRUD