Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #247
    dan
    Participant

    When you create a Flight model in Laravel you will be given this

    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class Flight extends Model
    {
    //
    }

    As you can see, is not being specified which table it is connected to.

    #248

    It is all about convention that Laravel used. For reference please check this documentation

    https://laravel.com/docs/9.x/eloquent#table-names

    Laravel use “snake case” and plural name of the class as a convention.

    For example

    Snake Case: Model: FlightControl Table: flight_control
    Plural Name: Model: Flight Table: flights

    So in that Flight model case for example, Laravel will find a table named flights to perform CRUD operations. If your table dont have the table then Laravel will throw an error.

    If you use a table thats is not snake case or plural version of the Model Name you have to specify it with a protected property.

    For example if you have a model Flight and your table is named travel you can do this

    <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

    class Flight extends Model
    {
    /**
    * The table associated with the model.
    *
    * @var string
    */
    protected $table = ‘flights’;
    }

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.