繁体   English   中英

Laravel 5 - 雄辩的关系返回空

[英]Laravel 5 - Eloquent relationships returning empty

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

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

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

艺术家模型

    <?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: [],
   }

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

谢谢你的帮助! 托比

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM