Laravel 4 有口才的麻烦

Laravel 4 有口才的麻烦,laravel-4,eloquent,Laravel 4,Eloquent,我对雄辩(ORM作为一个整体真的是如此)相当陌生。我读了很多背景资料,但不能很好地理解这些关系 我有一个汽车模型,涉及一个颜色模型,一个制造模型和一个模型 我将我的Car::getAll()作为$cars传递给我的视图。当我调用dd(toArray($cars))时,我得到以下信息: array (size=1) 0 => array (size=12) 'id' => string '1' (length=1) 'registration' =

我对雄辩(ORM作为一个整体真的是如此)相当陌生。我读了很多背景资料,但不能很好地理解这些关系

我有一个汽车模型,涉及一个颜色模型,一个制造模型和一个模型

我将我的Car::getAll()作为$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();
    }
}
class Color extends \Eloquent {
    protected $fillable = ['title', 'slug'];

    public function cars() {
        return $this->hasMany('Car', 'color');
    }
}
class Make extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'make');
    }
}
class Model extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'model');
    }
}
Color.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();
    }
}
class Color extends \Eloquent {
    protected $fillable = ['title', 'slug'];

    public function cars() {
        return $this->hasMany('Car', 'color');
    }
}
class Make extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'make');
    }
}
class Model extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'model');
    }
}
Make.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();
    }
}
class Color extends \Eloquent {
    protected $fillable = ['title', 'slug'];

    public function cars() {
        return $this->hasMany('Car', 'color');
    }
}
class Make extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'make');
    }
}
class Model extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'model');
    }
}
Model.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();
    }
}
class Color extends \Eloquent {
    protected $fillable = ['title', 'slug'];

    public function cars() {
        return $this->hasMany('Car', 'color');
    }
}
class Make extends \Eloquent {
    protected $fillable = [];

    public function cars() {
        return $this->hasMany('Car', 'make');
    }
}
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();
        });
    }
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');
        });
    }
public function up()
    {
        Schema::create('colors', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }
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');
         });
    }
CreateModelsTable

public function up()
    {
        Schema::create('makes', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }
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');
        });
    }
public function up()
    {
        Schema::create('colors', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }
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');
         });
    }
CreateColorsTable

public function up()
    {
        Schema::create('makes', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }
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');
        });
    }
public function up()
    {
        Schema::create('colors', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }
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');
         });
    }
CreateCarsTable

public function up()
    {
        Schema::create('makes', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }
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');
        });
    }
public function up()
    {
        Schema::create('colors', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('title');
            $table->string('slug');
            $table->timestamps();
        });
    }
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');
         });
    }

正如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
}
在其他车型上有很多关系都可以

然后在这个循环中:

foreach ($cars as $car)
{
   if (count($car->color))
   {
       // do something with color
   }
}
出于我使用
count
的原因,请阅读以下内容:

您还可以仅退回具有相关颜色、品牌和任何您需要的车辆,如:

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

外键在哪里?这些关系定义是错误的,这是肯定的。你永远不应该假设一个关系存在于一个循环中。您可能有一辆没有返回相关颜色模型的汽车。先检查,然后做你需要做的任何事情。@deczo我已经添加了我的Schema-up方法来显示外键的位置。@JasonLewis对不起,你能告诉我怎么检查吗?我假设能够像这样以数组的形式返回它,这表明我得到了预期的对象-尽管很明显它没有:/