2014-06-26 33 views
0

我對Eloquent頗爲陌生(而且真的是ORM的)。我做了相當多的背景閱讀,但不能完全理解雄辯中的關係。疑難雜症

我有一個Car模型,涉及到一個Color模型,一個Make模型和一個模型模型。

我將我的Car :: getAll()傳遞給我的View作爲$ cars。當我打電話DD(指定者($汽車))我得到以下幾點:

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

哪裏是外鍵?那些關係定義是錯誤的,這是肯定的。 –

+0

你不應該假設循環內存在關係。您可能有一輛沒有返回相關顏色模型的汽車。先檢查,然後做任何你需要做的事情。 –

+0

@deczo我已經添加了我的Schema up方法來顯示外鍵的位置。 –

回答

0

像賈森指出,從這些關係中的一個返回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')...