Laravel Route Model Controller Creation

( Normallly We create model name same as database table name . )
1) php artisan make : model <model_name>  -mf <{migration, factory}>
2) open in notpad folder >> database >>migrations >> {currentdate_create_modelname_table.php}
3) write in public function up() {} for our table structure like id , name,firstname
 example :  public function up()
        Schema::create('person2s', function (Blueprint $table) {

4).  PHP artisan migrate (That should create a
new table with database )
5).  Now we will add fake data in database.
6). open file database>>>factories>>>{model_name}.php file   and we will write like this
$factory->define(Person2::class, function (Faker $faker) {
    return [
        'first_name' => $faker->firstname
7). Now we create a seeder
8). php artisan make:seed {modelname_TableSeeder}
    exam : php artisan make:seed PersonTableSeeder
9). Our seeder has been created a file in  database>>>seeds>>> {modelname_TableSeeder.php}
10).    Now we call factory using seeds using  databsase>>>seeds>>>{model_name_TableSeeder.php}
write a function like this
use App\Person2;
public function run()     {  factory(Person2::class,50)->create();     }

11).     database>>>seeds>>>{DatabaseSeeder.php}
 public function run()
    {        $this->call(person2sTableSeeder::class);    }
12)  php artisan db:seed
13). routes >>>> api.php
        Route::get('/person2/{person2}',function(Person2 $person2){
             return $person2;
14). Now Create Controller
syntax : php artisan make:controller  <controllername>
exam:     php artisan make:controller Person2Controller
15) After creating a controller We can move function from api.php to controller so Our api.php will be like this
16) routes >>>> api.php
17) HTTP >>>>Controller>>>> PersonController.php
public function show(Person2 $person2)
             {     return $person2;       }
18) The same as we can create add, edit, delete function in our controller
We will check that in our next tutorials

No comments:

Post a Comment