D
D
DTX2017-01-03 01:21:57
Laravel
DTX, 2017-01-03 01:21:57

How does Laravel find out the name of a MySQL table?

The model class is called User, but the entry occurs in the Users table, although there is no mention of this table in the model. How does Laravel do it?
It didn't work out. I rest against the
Builder::insertGetId function,
and it is internal.
[internal function]: Illuminate\Database\Query\Builder->insertGetId(Array, 'id')

use Illuminate\Database\Eloquent\Model;

class User extends Model
{

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'login', 'password_hash'
    ];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [
        'password_hash',
    ];
}

//

    $user = new User;
    $user->login = $login;
    $user->password_hash = $hash;
    
    $user->save(); //Сохраняет в таблицу users

There is a mention of this table in the migration, but it is not related to the model in any way. Are there connoisseurs?
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('login')->unique();
            $table->string('password_hash');

            $table->timestamps();
        });
    }

    /**

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arman, 2017-01-03
@DirecTwiX

The getTable() method must return the name of the table, while processing the table property, you can either set the property or immediately reassign the method. By default, you can see that it takes the name of the model and processes it through the plural and snake functions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question