Laravel 6 CRUD Brief Tutorial
- Create a database named "shows"
- create a model by following command
php artisan make:model show-m
- Previous command will create show.php and [timestamp]create_shows_table.php migration file. go to migration file and add table schema in up method like this
public function up() { Schema::create('shows', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('show_name'); $table->string('genre'); $table->float('imdb_rating'); $table->string('lead_actor'); $table->timestamps(); }); } </code>
-
Run migration command to add tables to the database
php artisan migrate
- Inside app/show.php we add fillable properties. These are the properties which can be mass assigned.
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Show extends Model { protected $fillable = ['show_name', 'genre', 'imdb_rating', 'lead_actor']; }
-
Now we create a show controller with
--resource
flag. which will add CRUD methods automatically
php artisan make:controller ShowController --resource
Then we go to routes/web.php and add following
<?php // ShowController.php Route::get('/', function () { return view('welcome'); }); Route::resource('shows', 'ShowController');