【问题标题】:Laravel 5 - Eloquent relationships returning emptyLaravel 5 - 雄辩的关系返回空
【发布时间】:2015-12-17 12:33:58
【问题描述】:

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

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

以下是这两种模式的两个模式和模型。

艺人模特

    <?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');
    }
}

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

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

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

感谢您的帮助! 托比

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.1


    【解决方案1】:

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

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

    而且,如果一个专辑只能有一个艺术家,那么在专辑类中你应该有这样的:

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

    在你的代码上你做了相反的事情。

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

    $album->artist;
    

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

    $artist->albums;
    

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

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 2021-06-20
      • 2013-04-10
      • 2017-08-22
      相关资源
      最近更新 更多