Home » Make Model In Laravel 9 Tutorial With Example

Make Model In Laravel 9 Tutorial With Example

Last updated on June 14, 2022 by

In this tutorial, we will learn how to make a model in Laravel 9 with an example. Throughout this article, we will see how to create a model in Laravel with options. So without wasting a second, let’s just jump into it.

Make Model In Laravel 9

To make a model in Laravel 9 we will use the make:model Artisan command followed by the Model name.

Syntax:

php artisan make:model [ModelName]

Example:

php artisan make:model Post

After running the above command a new Post.php model file will be created in app/Models directory. If you open the file then it will look like the below:

app/Models/Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;
}

Notes: As per the Laravel naming conventions Post model by default connected with the posts table. So if you have created a table with another name then you have to define protected $table = 'blog_posts'; in the model.

Laravel Create Model With Migration

Sometimes, you want to create a model with database migration in Laravel then you need to pass the --migration or -m option along with make:model command as below:

php artisan make:model [ModelName] -m
               // OR
php artisan make:model [ModelName] --migration

As per our case, we need to generate a Post model with migration as below:

php artisan make:model Post -m

After running the above command, two new files will be generated. The first model file as we have seen above and the second migration file which will be generated in database/migrations/2022_06_04_1000_create_posts_table.php and it will look like below:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Laravel Create Model With Other Classes/Options

We can generate lots of other things along with the make model in Laravel such as factories, seeders, policies, controllers, and form requests. In addition, these options may be combined to create multiple classes at once:

# Generate a model and a FlightFactory class...
php artisan make:model Flight --factory
php artisan make:model Flight -f
 
# Generate a model and a FlightSeeder class...
php artisan make:model Flight --seed
php artisan make:model Flight -s
 
# Generate a model and a FlightController class...
php artisan make:model Flight --controller
php artisan make:model Flight -c
 
# Generate a model, FlightController resource class, and form request classes...
php artisan make:model Flight --controller --resource --requests
php artisan make:model Flight -crR
 
# Generate a model and a FlightPolicy class...
php artisan make:model Flight --policy
 
# Generate a model and a migration, factory, seeder, and controller...
php artisan make:model Flight -mfsc
 
# Shortcut to generate a model, migration, factory, seeder, policy, controller, and form requests...
php artisan make:model Flight --all
 
# Generate a pivot model...
php artisan make:model Member --pivot

Additionally, read our guide:

  1. Laravel 9 Image Upload Tutorial With Example
  2. Laravel 9 Multiple Database Connections Example
  3. Laravel 9 Automatically Generates Sitemap With Example
  4. Laravel: Change Column Name In Migration
  5. How To Use Where Date Between In Laravel
  6. How To Add Laravel Next Prev Pagination
  7. Laravel Remove Column From Table In Migration
  8. Show All Fields Of Model In Django Admin
  9. Laravel 9 Resource Controller And Route With Example
  10. How To Display All Errors In PHP
  11. How To Install Vue In Laravel 8 Step By Step
  12. How To Handle Failed Jobs In Laravel
  13. Best Ways To Define Global Variable In Laravel
  14. How To Get Latest Records In Laravel
  15. Laravel Twilio Send SMS Tutorial With Example
  16. How To Pass Laravel URL Parameter
  17. Set Default Value Of Timestamp In Laravel Migration
  18. Laravel 9 File Upload Tutorial With Example
  19. How To Schedule Tasks In Laravel With Example
  20. Laravel Collection Push() And Put() With Example

That’s it from our end. We hope this article helped you to learn Laravel 9 create a model tutorial with an example.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment