0

I did some searching but still I can't solve this, I'm coding a little library site for learning and I can't get relationships to work. (libro = book, autore = author, genere = genre)

In my tinker the command

$libro->autore

returns null or empty, even if I call it as a method and use toArray

this is my code :

Libro model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Libro extends Model
{
    protected $table = 'libri';
    protected $fillable = ['titolo', 'id_autore', 'pubblicazione', 'trama', 'voto', 'image_url', 'id_genere'];
    public function genere() {
        return $this->belongsTo('App\Genere');
    }

    public function autore() {
        return $this->belongsTo('App\Autore');
    }
}

Autore Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Autore extends Model
{
    protected $table = 'autori';
    protected $fillable = ['nome', 'cognome', 'nascita', 'paese'];

    public function libri() {
        return $this->hasMany('App\Libro');
    }
    public function getFullNameAttribute()
    {
        return $this->nome . " " . $this->cognome;
    }
}

The relation in my migration

$table->foreign('id_autore')->references('id')->on('autori');
$table->foreign('id_genere')->references('id')->on('generi');

I added the foreign keys in the mysql db, checked that on phpmyadmin, but still, it doesn't work, what am I doing wrong?

**adding more tinker responses to try outs ** If I try:

>>> App\Autore::find(2)->libri()->get()

I get:

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'libri.autore_id' in 'where clause' (SQL: select * from `libri` where `libri`.`autore_id` = 2 and `libri`.`autore_id` is not null)'

If I try :

$libro-autore()

I get :

BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::autore()'

instead

$libro->autore

remains

null
4
  • can you add your controller logic. Commented Sep 20, 2016 at 15:38
  • I'm using tinker for showing content, I don't understand what's the point of pasting the controller which only do things for the views Commented Sep 20, 2016 at 15:46
  • have you tried $libro->autore() Commented Sep 20, 2016 at 16:07
  • Yes, I'm also adding more tinker responses to the main post, read it up Commented Sep 20, 2016 at 16:10

1 Answer 1

1

Your naming convention on the columns in your relation are incorrect (backwards). The convention is ${related_table}_id.

To solve this, alter your migration. Othwise, if you don't want to just adjust your migration, then specify the foreign key column in your Autore model as the 2nd argument to your hasMany relationship.

public function libri() {
    return $this->hasMany('App\Libro', 'id_autore')
}

And make sure to do the inverse for the Libro model.

Sign up to request clarification or add additional context in comments.

5 Comments

So the foreign keys don't set it automatically? I thought the combination of ->foreign and ->references would've set that
The database schema and the model pattern have no knowledge of each other. You must explicitly tell them how to communicate.
Also I'm sorry but tinker still returns empty arrays, and $libro->autore still gets null
Sorry, my bad, I had to add that also to the belongsTo, thanks a lot! that was my problem, sorry for bein blind not seeing that wrong sql query
@K3nzie Sorry, had to step away. I should've clarified you need to do the inverse as well. My apologies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.