雄辩的麻烦

时间:2014-06-26 22:40:45

标签: laravel-4 eloquent

我对Eloquent(以及整个ORM)很新。我做了相当多的背景阅读,但不能完全理解Eloquent中的关系。

我有一个与Color模型,Make模型和Model模型相关的Car模型。

我将我的Car :: getAll()传递给我的View作为$ cars。当我打电话给dd(toArray($ cars))时,我得到以下内容:

array (size=1)
  0 => 
    array (size=12)
      'id' => string '1' (length=1)
      'registration' => string '123' (length=3)
      'make' => 
        array (size=5)
          'id' => string '1' (length=1)
          'title' => string 'Ford' (length=4)
          'slug' => string 'ford' (length=4)
          'created_at' => string '2014-06-26 21:30:23' (length=19)
          'updated_at' => string '2014-06-26 21:30:23' (length=19)
      'model' => 
        array (size=5)
          'id' => string '1' (length=1)
          'title' => string 'Mustang' (length=7)
          'slug' => string 'mustang' (length=7)
          'created_at' => string '2014-06-26 21:30:41' (length=19)
          'updated_at' => string '2014-06-26 21:30:41' (length=19)
      'color' => 
        array (size=5)
          'id' => string '1' (length=1)
          'title' => string 'Red' (length=3)
          'slug' => string 'red' (length=3)
          'created_at' => string '2014-06-26 21:30:03' (length=19)
          'updated_at' => string '2014-06-26 21:30:03' (length=19)
      'year' => string '1991' (length=4)
      'is_classic' => string '1' (length=1)
      'price' => string '999.00' (length=6)
      'sold' => string '0' (length=1)
      'active' => string '1' (length=1)
      'created_at' => string '2014-06-26 22:17:27' (length=19)
      'updated_at' => string '2014-06-26 22:17:27' (length=19)`

这对我来说似乎是正确的,但是当我有:

foreach ($cars as $car) {
  echo $car->color-title;
}

我得到了“试图获取非对象的属性”错误。

我的模型如下:

Car.php

class Car extends \Eloquent {
    protected $fillable = ['color_id'];


    public function color() {
        return $this->belongsTo('Color', 'id');
    }

    public function model() {
        return $this->belongsTo('Model', 'id');
    }

    public function make() {
        return $this->belongsTo('Make', 'id');
    }

    public static function getAll() {
        return Car::with('color', 'make', 'model')->where('active', 1)->get();
    }
}

Color.php

class Color extends \Eloquent {
    protected $fillable = ['title', 'slug'];

    public function cars() {
        return $this->hasMany('Car', 'color');
    }
}

Make.php

class Make extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'make');
    }
}

Model.php

class Model extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'model');
    }
}

非常感谢任何帮助。谢谢

修改

抱歉,我应该包含我的架构方法:

CreateMakesTable

public function up()
    {
        Schema::create('makes', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }

CreateModelsTable

public function up()
    {
        Schema::create('models', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('make')->unsigned();
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });

        Schema::table('models', function(Blueprint $table)
        {
            $table->foreign('make')->references('id')->on('makes')->onDelete('cascade');
        });
    }

CreateColorsTable

public function up()
    {
        Schema::create('colors', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }

CreateCarsTable

public function up()
    {
        Schema::create('cars', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('registration');
            $table->integer('make')->unsigned();
            $table->integer('model')->unsigned();
            $table->integer('year');
            $table->integer('color')->unsigned();
            $table->boolean('is_classic');
            $table->float('price');
            $table->boolean('sold');
            $table->boolean('active');
            $table->timestamps();
         });

         Schema::table('cars', function(Blueprint $table)
         {
            $table->foreign('make')->references('id')->on('makes')->onDelete('cascade');
            $table->foreign('model')->references('id')->on('models')->onDelete('cascade');
            $table->foreign('color')->references('id')->on('colors')->onDelete('cascade');
         });
    }

1 个答案:

答案 0 :(得分:0)

就像Jason所指出的那样,错误是由其中一个关系返回的null引起的。但是你的设置问题是,关系定义是错误的。

首先让它们正确:

// it goes like this:
// belongsTo('Model', 'foreign_key_on_this_model', 'primary_key_on_related')

public function color() {
    return $this->belongsTo('Color', 'color'); // primary key is id so no need to specify
}
其他模型上的

hasMany关系没问题。

然后在那个循环中:

foreach ($cars as $car)
{
   if (count($car->color))
   {
       // do something with color
   }
}

我之所以使用count,请阅读:Laravel check if related model exists

您也可以只返回具有相关颜色,制作以及您需要的任何车辆,如下所示:

$cars = Car::has('color')->has('make')...