dimanche 20 septembre 2015

Laravel 5 - Eloquent relationships returning empty

this is my first time trying out eloquent relationships. I've looked at multiple tutorials already.

I have two tables. Albums and Artists. An Artist can have many Albums. An Album can only have one Artist.

Here are the two Schemas and Models for both of these.

Artist Model

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

Album Model

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

Artist Schema

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

Album Schema

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

I have one album and one artist sitting in the database, with the album's artist_id set to the artist's id.

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

I need to find out why these are returning empty.

Thanks for any help! Toby



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire