If you are a beginner and want to learn how to start developing Laravel applications you came to the right place because today we are going to learn step-by-step how to create your first Laravel 5.6 project. You are going to learn:
- How to create your first Laravel project
- How to create posts
- How to edit posts
- How to delete posts
How to create your first Laravel project
Laravel framework has a few system requirements which are necessary to develop your applications smoothly. If you are not comfortable with installing different packages I would highly recommend you to use Laravel Homestead virtual machine which does all the job for you and makes your local development easy.
However, if you are not using Homestead, you will need to make sure your server meets the following requirements:
PHP >= 7.1.3
OpenSSL PHP Extension
PDO PHP Extension
Mbstring PHP Extension
Tokenizer PHP Extension
XML PHP Extension
Ctype PHP Extension
JSON PHP Extension
Laravel utilizes Composer to manage its dependencies. So, before using Laravel make sure you have Composer installed on your machine.
First, you will need to download Laravel installer using Composer. In your terminal run this command:
Make sure to place composer's system-wide vendor bin directory in your so the Laravel executable can be located by your system. This directory exists in different locations based on your operating system; however, some common locations include:
GNU / Linux Distributions:
Once installed, you now can create your fresh Laravel installation:
The word blog specifies in which directory to install Laravel project. The alternative method to install Laravel project would be using Composer command:
If you have PHP installed locally and would like to use built-in development server to serve your application you can use command which will make your server available at
The next thing you should do after installing your project is set your application key to a random string. You can do that by writing this command in terminal:
If you wish to configure your database you can open file in your project's root directory and add your database credentials:
You should now be ready to build your first Laravel application. For this example, we will create a simple blog where you can create, view, edit and delete posts.
How to create posts
First of all, we need to create a model. This model will represent our database table and will be used to communicate with it. To simply create a model in Laravel we can write this in terminal:
The option or is set to also create a migration file which we can find in folder. We can open this newly created file and specify our table's columns:
After we save the file we can now migrate everything to the database:
We can now create a post controller and specify some functions:
The option saves us a little bit of time and creates basic CRUD functions. If you don't know what CRUD is it is Create, Read, Update and Delete functionality. We can now go to our file and create function and return the view:
We can define our view in folder. In this case, we will rename file into and change the layout a little bit:
Lastly, we need to define a route for the view. We can do that by editing file and adding:
We specified a URL we want to use, a controller function and we set route name. Now when we visit we can see our newly created view.
Let's now create our first dynamic post. First of all, we will create a new controller function in our post controller:
Now we need to create our view. In our folder we will create a new folder called 'post' and inside this folder, we will create file. We will set up a simple form which we will use to store our post's data:
You can see that I already wrote an action URL for form. You can also see that right after form there is a line with code. This is Blade syntax and it creates a hidden token field which is required to submit forms in Laravel. You can also see that in the input fields value attribute there is . It is used when the form is submitted and rejected it puts the old form values so that we wouldn't need to enter all the values again.
We now can create routes for post create view and post store:
We need to write our function in file:
First of all, we validate the data from our form. Then we create a new model instance. Then we write our data to the model and we save it. In the end, we redirect the user to the main page. And also don't forget to include model into controller:
If you did everything correctly you now should be able to create a post. Go to your post create page, fill out the form and submit it. If the page redirects to the main one it means you probably saved your post into the database. Now we need to display all the posts on the index page. We can do that in function:
We take all the table posts entries and return them to the main index view. In our file we need to create a loop to display all of our records:
We used Blade syntax for loop and we passed variable and defined a single object as . Now all the content inside loop will continue until all the objects will be passed. And we can access object’s data like that . For the description we used php function which defines how many characters are displayed. And we also defined a 'Read More' button which we will link to our view. For a route, we need to pass post id which will tell the controller what post to show. Alternatively, you could pass a post slug to let the controller know which object to show. To find out more about the slugs you can read my other tutorial on How to Create Slugs in Laravel.
Now we can create a new route for view:
After we have done this, we need to find this post in a database and return it to the view in our post controller’s function :
And finally, we can create our new view in called :
We can see that now we don’t use loop because we only have one object instead of a collection of objects. We can display our post information just like that .
How to edit posts
You probably noticed that I already created an 'Edit Post' button with a new route. This route will be used to edit our post. Same as for 'Read More' button we need to pass an id of our post to the route. We now can define that route:
Now, in our controller we can return view with post object:
We can actually duplicate the file and rename it to . We only need to change a few things:
We changed the form action route and instead of we can write to display data in the fields. Now we need to define the new route and the post controller’s function:
And for the update function:
You can see that this method is almost the same as for function. The only difference is that we don’t need to create the new instance, we need to find an existing object in our database and just update it.
If you did everything correctly, you should now be able to update your post. The last thing we need to cover is deleting the post.
How to delete posts
In the file we can add an additional button named 'Delete' with a new route. The difference between 'Edit Post' button is that we cannot just simply pass a route we need to submit it with post method so we need to wrap this button inside the form:
Now we can create a route for this method:
And lastly, we need to create a function inside our file:
And that’s it! You now should be able to delete your posts.
Congratulations! You now have built your first Laravel application. I know it can be a little bit overwhelming but once you get it you could actually see that a lot of things repeat itself. You create a route, then define what that route does in your controller file and then you create a view to display your content. If you had any problems or just simply have questions feel free to comment down below.
- How is mess food at IISER Pune
- Is the blood pressure 142 95 healthy
- How are Florida Republicans talking about impeachment
- Why has copyright infringement decreased recently
- How can I overcome negativity
- What is prion Is it a disease
- What guns are not restricted in Canada
- Does Jainism believe in multiverses
- Which was is the most fascinating civilization
- Will they come to Bangladesh
- Where can I learn singing in Mumbai
- What do narcissists think of empaths
- Have you ever compromised yourself for promotion
- What is the Lorentz law
- When is Memorial Day 1
- Can I give an infant NyQuil
- Is racism always bad
- How is dried food for camping made
- Which are deep discharge cyclic batteries