How to make Crud on Laravel

Build a basic CRUD app with Laravel & MySQL: Step-by-step guide with an example application.

Creating a CRUD App with Laravel

Laravel is an open-source PHP web framework that makes building apps easier and faster. It provides a great starting point for developers looking to create their own applications. Laravel is the perfect choice for creating a CRUD (Create, Read, Update, Delete) application. Here's an example of how to use Laravel to make a basic CRUD app.

Step 1: Install Laravel

The first step is to install Laravel. To do this, you'll need to have a server setup with PHP 7.2 or higher and a database such as MySQL or Postgres. Once you've set up your server, you can install Laravel using the Composer package manager by running the following command:

composer create-project --prefer-dist laravel/laravel blog

This will create a new Laravel project in the blog directory. You can then navigate to the project directory and start the development server with the following command:

cd blog
php artisan serve

This will start the development server on localhost:8000. You can now open the project in your browser.

Step 2: Set up the Database

The next step is to set up the database for the app. You can do this by editing the .env file in the root of the project. This file contains the configuration settings for the app. You'll need to set the database credentials in this file and then run the following command to create the tables:

php artisan migrate

This will create the necessary tables in the database.

Step 3: Create the Model

The next step is to create the model for the app. This is the class that will contain the data that will be stored in the database. To create the model, you can use the Artisan command line tool. To do this, run the following command:

php artisan make:model BlogPost

This will create a new model class in the app directory. You can then open the class and add the necessary properties for the model, such as the title, body, and published date.

Step 4: Create the Controller

The next step is to create the controller for the app. This is the class that will handle the requests from the browser and return the necessary data. To create the controller, you can use the Artisan command line tool. To do this, run the following command:

php artisan make:controller BlogPostController

This will create a new controller class in the app directory. You can then open the class and add the necessary methods for the controller, such as the index, store, show, update, and delete methods.

Step 5: Create the Routes

The next step is to create the routes for the app. This is where you define the URLs that the app will respond to. To do this, open the routes/web.php file and add the necessary routes, such as the following:

Route::get('/', 'BlogPostController@index');
Route::get('blog-post/{id}', 'BlogPostController@show');
Route::post('blog-post', 'BlogPostController@store');
Route::put('blog-post/{id}', 'BlogPostController@update');
Route::delete('blog-post/{id}', 'BlogPostController@delete');

These are the basic routes for the CRUD app. You can add more routes as needed.

Step 6: Create the Views

The last step is to create the views for the app. This is where you define what the app will look like. To do this, create a folder called resources/views and add the necessary files, such as the index.blade.php and show.blade.php files. You can then add the necessary HTML and Blade code to these files to create the user interface for the app.

And that's it! You now have a basic CRUD app created with Laravel. You can now start adding the necessary code to make the app more feature-rich and user-friendly.

Answers (0)