Laravel crudIn this topic, we will learn how to create the laravel crud in laravel 5.8. The following are the steps required to build the crud app:
The above screenshot shows that the 'crud' project has been created successfully.
In the above screenshot, we have provided the database name as laravel_crud.
The above screenshot shows that we have modified the .env file. We have provided database name as laravel_crud to the DB_Database field, root to the DB_Username. We left blank to the password field.
The above-highlighted statement creates a migration "create_user_table", and the name of the table is 'user'.
We have created four new columns (first name, last name, gender, qualifications) in the user table shown in the above code.
php artisan migrate; After migration, look at the database given in the below screenshot: The above screen shows that the user table has been created under the laravel_crud database.
The above screen shows that the 'Crud' model has been created successfully.
Crud.php In the above model, we have provided two attributes, i.e., $table and $fillable. The $table is an attribute that contains the name of the table, which Crud model is going to use while the $fillable attribute contains the array of the names of the columns which cannot be NULL.
The above screenshot shows that the CrudsController has been created successfully. The structure of CrudsController is given below: The CrudsController contains the inbuilt functions (index(), create(), store(), show(), edit(), update(), destroy()). Now, we implement the CRUD operations through the methods available in the CrudsController. Insert Operation
Output of the above code would be:
Suppose we enter some data in the form, and then click on the Insert button shown in the below screenshot:
The above screenshot shows that the data we entered in the form has been successfully saved in the database. Retrieving the records
Route::get('/show','CrudsController@index'); The above statement creates a route with a url '/show' which calls the index() method of the CrudsController class.
In the above code, we use the all() method that retrieves all the records of a table associated with a Crud model, and stores it in the $cruds object. We pass the $cruds object to the index.blade.php file by using the view() method.
Output of the above code would be: Update operationWhen we click on the Edit button, then it calls the edit() function of the CrudsController class. The code of the edit() method is given below: CrudsController.php In the above code, we use find() method that finds the record of a given id, and stores it in the $crud object. We pass the crud object to the edit.blade.php file. edit.blade.php After clicking on the Edit button, the screen appears which is shown below, and it asks you to update the data. In the above screen, you can change any field according to your requirement. Suppose I entered 'Harshita' in first name, tripathi in last name, and other fields remain same, click on the Update button as shown below: After clicking on the Update button, the control moves to the update() function of the CrudsController.php file. CrudsController.php In the above code, we use find() method that finds the record of a given id, and stores it in the $crud object. We pass the crud object to the edit.blade.php file. edit.blade.php The above code updates the database. Let's look at the database: The above screenshot shows that the data has been updated successfully. Delete operationIf we click on the delete button, then it calls the destroy() function of the CrudsController class. The code of the destroy() method is given below: Next TopicLaravel Validation |