Laravel 5 eloquent relationship returns empty or null but the code seems ok
<p>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)</p>

<p>In my tinker the command</p>

<pre><code>$libro-&gt;autore
</code></pre>

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

<p>this is my code :</p>

<p><strong>Libro model</strong></p>

<pre><code>namespace App;
</code></pre>

<p>use Illuminate\Database\Eloquent\Model;</p>

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

    public function autore() {
        return $this-&gt;belongsTo('App\Autore');
    }
}
</code></pre>

<p><strong>Autore Model</strong></p>

<pre><code>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-&gt;hasMany('App\Libro');
    }
    public function getFullNameAttribute()
    {
        return $this-&gt;nome . " " . $this-&gt;cognome;
    }
}
</code></pre>

<p><strong>The relation in my migration</strong></p>

<pre><code>$table-&gt;foreign('id_autore')-&gt;references('id')-&gt;on('autori');
$table-&gt;foreign('id_genere')-&gt;references('id')-&gt;on('generi');
</code></pre>

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

<p>**adding more tinker responses to try outs **
If I try:</p>

<pre><code>&gt;&gt;&gt; App\Autore::find(2)-&gt;libri()-&gt;get()
</code></pre>

<p>I get:</p>

<pre><code>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)'
</code></pre>

<p>If I try :</p>

<pre><code>$libro-autore()
</code></pre>

<p>I get :</p>

<pre><code>BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::autore()'
</code></pre>

<p>instead</p>

<pre><code>$libro-&gt;autore
</code></pre>

<p>remains </p>

<pre><code>null
</code></pre>