Laravel 5 - Eloquent 关系返回空

Laravel 5 - Eloquent relationships returning empty

这是我第一次尝试 eloquent 关系。我已经看过多个教程了。

我有两张桌子。专辑和艺术家。一个艺术家可以有很多专辑。一张专辑只能有一位艺术家。

这是这两个的两个模式和模型。

艺术家模特

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Artist extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'Artists';

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

    public function albums()
    {
        return $this->hasOne('App\Album');
    }
}

专辑型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Album extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'Albums';

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

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

艺术家架构

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArtistsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('Artists', function (Blueprint $table) {
            $table->increments('id');
            $table->string('artist_name');
            $table->string('artist_image_loc');
            $table->integer('followers');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('Artists');
    }
}

专辑架构

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAlbumsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('Albums', function (Blueprint $table) {
            $table->increments('id');
            $table->string('album_name');
            $table->string('album_image_loc');
            $table->integer('artist_id')->unsigned();
            $table->timestamps();

            $table->foreign('artist_id')
                  ->references('id')
                  ->on('Artists');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('Albums');
    }
}

我在数据库中有一张专辑和一位艺术家,专辑的 artist_id 设置为艺术家的 ID。

>>> $album = new App\Album;
=> App\Album {#694}
>>> $album->artists()->get()
=> Illuminate\Database\Eloquent\Collection {#704
     all: [],
   }

我需要找出为什么这些返回是空的。

感谢您的帮助! 托比

如果你说一个艺术家可以有很多专辑,那么,在艺术家 class 上你应该有这个:

public function albums()
{
  return $this->hasMany('App\Album');
}

而且,如果一张专辑只能有一个艺术家,那么在专辑 class 上你应该有这个:

public function artist()
{
  return $this->hasOne('App\Artist');
}

在您的代码中,您做的恰恰相反。

要从专辑中检索艺术家,您只需执行以下操作:

$album->artist;

并从艺术家那里检索专辑:

$artist->albums;

所以,你不需要调用方法,你可以使用eloquent的动态属性(在本例中,艺术家和专辑) 您可以在这里找到更多信息: http://laravel.com/docs/5.1/eloquent-relationships