Tasks CRUD with Laravel 5.5

If you read the last posts you already understand how MVC is applied in Laravel, but our example can be more powerful and next to real world. What if we made a little CRUD demonstration with Laravel 5.5.

Let’s do that!

Well,  start a new project called task. And let’s remember some things.

Initial config –  Homestead

I’m supposing that you have Homestead installed, if not see the documentation:
https://laravel.com/docs/5.5/homestead

First configuration steps, remember change hosts file /etc/hosts to add the line:

Make some changes in Homestead.yaml (Usually in /Users/YOURUSER/Homestead/Homestead.yaml) to map new site.

Only to understand, we are creating the map (links) and the database for it project.

Now restart your homestead

Is everything fine? Get to work!

First steps in Laravel 5.5

The .env file
Remember to change these variables in your .env file

Please attention in DB_HOST=192.168.10.10. I like to use the IP of homestead because in this way we can do php artisan migrate out of vagrant ssh. The default password of MySQL Homestead is secret.

First step, creating a model with migration

To start use artisan to generate your model with migration

Well, now we have a model in your app folder and a migration in your database folder as you can see:

Now, open your migration file to do some changes and add the title of your task.

Run the migrations to create our tables

Now make the tasks controller and associates to model Tasks.

For now, we don’t change anything in our model or controller. Firstly, add your index route in routes/web.php.

It means that our path /tasks will be assign to our index method at TasksController.

Our index controller should list and fetch all tasks in a view.

Now create the view. resources/views/tasks/index.blade.php

If you visit http://tasks.s/tasks you will see a blank page, don’t worry, it’s ok. It’s because we don’t have any tasks in our database, let’s add a link to add new task in our view.

resources/views/tasks/index.blade.php

Remember to add it route in our routes/web.php

See the name() method after the Route facade, we are naming our routes to do links easily. Now we need to return a view in our TasksController@create

To make our model we will need add the route to our store method first.

routes/web.php

It route receives a action post and will go to store.

Remember to create our resources/views/tasks/create.blade.php and add this form:

Attention to {{ csr_field() }} https://laravel.com/docs/5.5/csrf

Back to our Controller and do the store method.

If you try to post a new task you have 2 errors, 1 the mass assignment and 2 the route is undefined.

We need to fix the problem of route, change the route of index method and add the name for it:

The mass assignment is a ‘problem’ in our model, let’s fix that: (/app/Tasks.php)

The protected $fillable means that all itens of array can be fillable in a form, otherwise the system will throw an exception.

Now if you try to add a tasks it will works fine!

At this point the user can list the tasks and add a new task, but still don’t have how edit or delete the task.
Make some changes in your resources/views/tasks/index.blade.php to add the links.

When you call route() please attention that second parameter will be the resource or an array with parameters of this route. Note that method_field emulates other verbs in HTTP forms.

Let’s create the tasks.edit and tasks.delete routes in /routes/web.php

We start from destroy method:

Behind the scenes the field {mask} in our route means that it argument will be passed to destroy method, and destroy method ‘activates’ the route model binding to get the correct task from eloquent.

Lastly, we need to finish the edit action edit/update.

Now change our method TasksController@edit to return the Task data to view.

It’s time to create the edit form (view resources/views/tasks/edit.blade.php )

If you try to edit the task will see a error, it because the route tasks.update (put/patch) is undefined.

Fix it and, here’s the final routes/web.php file

Now we need to create the update method TasksController@update

See the new method update at the model, how you can expect it receives an array from request and select only field title to update the model.

And for now is it. In soon we will make a simple Access Level Control with roles and permissions in Laravel with default Auth methods.

See the final files in github https://github.com/diegosm/tasks-example .

Share this!




2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*