M
M
Mariik2016-07-14 02:37:26
Laravel
Mariik, 2016-07-14 02:37:26

Why doesn't belongsTo() method work in Laravel model?

Given:
1. migration to book users table

Schema::create('books_users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->string("email");
        });

2. migration to books table:
Schema::create('books', function (Blueprint $table) {
            $table->increments("id");
            $table->integer("book_user_id")->nullable()->unsigned();
            $table->string("author");
            $table->string("genre");
            $table->smallInteger("year")->unsigned();
            $table->string("title");
            $table->foreign('book_user_id')->references('id')->on('books_users');
        });

3. user model:
namespace App\Models\FrontEnd\Users;

use Illuminate\Database\Eloquent\Model;

class BookUser extends Model
{
    protected $table = 'books_users';
    protected $fillable = ['first_name', 'last_name'];
    public $timestamps = false;

    public function books(){
        return $this->hasMany('\App\Models\FrontEnd\Books\Book');
    }
}

4. book model:
class Book extends Model
{
    protected $table = 'books';
    protected $fillable = [
        'author',
        'genre',
        'year',
        'title',
        'book_user_id'
    ];
    public $timestamps = false;

    public function owner(){
        /*
            Вопрос:
            Почему тут мне приходится явно указывать колонку?
            В случае с моделью BookUser мне не приходилось это делать.
            В чем тут разница?
        */
        return $this->belongsTo('\App\Models\FrontEnd\Users\BookUser");
    }
}

the connection between the user and the book works fine, here is the code that works fine:
$x = new \App\Models\FrontEnd\Users\BookUser();
$res = $x->first();
dd($res->books->toArray());

But the connection between the book and the user does not work at all ...
$y = new \App\Models\FrontEnd\Books\Book();
 $res = $y->first();
   dd($res->owner->toArray());

The only thing that helps is explicitly specifying the column:
return $this->belongsTo('\App\Models\FrontEnd\Users\BookUser','id');

How can I get rid of the explicit column specification? Indeed, in the case of the user model, I did not have to explicitly specify the name of the column. why?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D', 2016-07-14
@Mariik

The column must be specified explicitly in both cases.
The fact that it works with an 'id' field is just a coincidence (because it looks like the id's just matched) and will lead to errors later on.
In both cases, you need to explicitly specify the book_user_id field, then everything will work as it should.
And you need to specify because laravel in this case cannot correctly display the name of the connection from the table name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question