Working with Route, Controller and Model will be introduced in this section. Building a CRUD users project with Laravel 5.5. Website Programming CRUD using PHP Framework Laravel.
We will continue with setting up the Route, Controller, Model and View in the PHP Framework Laravel 5.5 CRUD users project.
Step 5: Create the UserController
Use the following command in Terminal to create the UserController:
1 2 | php artisan make:controller UserController --resource |
The command to create the Controller will create the UserController.php file under the path user-crud-laravel / app / Http / Controllers / UserController.php. The –resource flag helps us create 7 functions with names according to the conventional CRUD Laravel standard. The UserController.php file will have the following source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | <?php namespace AppHttpControllers; use IlluminateHttpRequest; class UserController extends Controller { /** * Display a listing of the resource. * * @return IlluminateHttpResponse */ public function index() { // } /** * Show the form for creating a new resource. * * @return IlluminateHttpResponse */ public function create() { // } /** * Store a newly created resource in storage. * * @param IlluminateHttpRequest $request * @return IlluminateHttpResponse */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return IlluminateHttpResponse */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return IlluminateHttpResponse */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param IlluminateHttpRequest $request * @param int $id * @return IlluminateHttpResponse */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return IlluminateHttpResponse */ public function destroy($id) { // } } |
Each function has a specific task, for example index – display all users, destroy – delete specific users, …
Step 6: Create a Route to handle CRUD
Search for web.php file by following user-crud-laravel / routes / web.php and edit the following:
1 2 3 4 5 6 7 8 | <?php Route::get('/', function () { return view('welcome'); }); Route::resource('users', 'UserController'); |
The static function resource defined above will give us 7 routes and point specifically to the 7 functions in the UserController. Use the following command to view the entire route of the PHP Framework Laravel project in Terminal:
1 2 | php artisan route:list |
In addition to the first 2 routes created by Laravel, the remaining routes are created by the static function resource of the above Route class.
Step 7: Create a Model User
By default, Laravel has created our User model, we temporarily delete this model to become familiar with the Model creation command. Find the file User.php under the path user-crud-laravel / app / User.php. Use the following command in Terminal / GitBash to create a Model User:
1 2 | php artisan make:model User |
We have Migration, Seeder, Route, Controller and Model for Laravel 5.5 CRUD project. The next part I will introduce about View is Logic following the standard MVC architecture in Laravel 5.5.