In this tutorial you will learn about the Create Controller And Model Laravel 7/6 Using Command and its application with practical example.
In this Create Controller And Model Laravel 7/6 Using Command tutorial I will show you how to Create Controller And Model Using Command In laravel. In this tutorial you will learn to create or generate controller and model using command in laravel. In this article we will learn about laravel artisan command to create controller and model. In this article I will demonstrate you with example to create controller and model using laravel command.
Create Controller And Model Laravel 7/6 Using Command
In this step by step tutorial I will show you how to create or generate controller and model using command in laravel. Please follow the instructions given below:
Create model command
Below is example command to create laravel model. Please Use the php artisan make model for creating a model using the command line (CLI) :
1 |
php artisan make:model Photo |
The above command will create a photo model with following code:
1 2 3 4 5 6 7 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Photo extends Model { // } |
Create Controller command
Below is example command to create laravel controller. Please Use the php artisan make controller for creating a controller using the command line (CLI) :
1 |
php artisan make:controller PhotoController |
The above command will create a controller named photoController with following code:
1 2 3 4 5 6 7 8 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PhotoController extends Controller { } |
Create a Resource Controller Command
Below is example command to create a resource controller. Please Use the php artisan make controller with –resource option for creating a resource controller:
1 |
php artisan make:controller PhotoController --resource |
Below is default resource controller code generated with the above command. It has already created some methods like index, update, edit, destroy, etc. It looks like this:
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 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class PhotoController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { // } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { // } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } } |
Command For Create Model and Controller
Below is example command to create laravel model and controller together.
1 |
php artisan make:model -mc Photo |