我有以下迁移:
表:比比达斯:
class CreateBebidasTable extends Migration{
  public function up() {
    Schema::create('bebidas', function ($table) {
      $table->increments('id');
      $table->integer('tipo_id')->unsigned();
      $table->string('bebi_name');
      $table->string('bebi_size');
      $table->float('bebi_price');
      $table->timestamps();
      $table->foreign('tipo_id')->references('id')->on('tipobebidas');
    });
  }
  public function down() {
    Schema::drop('bebidas');
  }
}
表:tipobebidas
class CreateTiposBebidasTable extends Migration {
    public function up()
    {
        Schema::create('tipobebidas', function($table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::drop('tipobebidas');
    }
}
这些是模型:
class Bebida extends Eloquent{
  public function TipoBebida() {
    return $this->belongsTo('TipoBebida');
  }
}
class TipoBebida extends Eloquent{
  protected $table = "tipobebidas";
  public function Bebidas() {
    return $this->hasMany('Bebida');
  }
}
每个Bebida(drink) 都有一个TipoBebida(drink type) 和 vive-versa。我正在尝试获得一个组合表,显示bebidas表和tipobebidas表中的所有字段。
基于 Laravel 关于eager loading的文档,我正在运行以下命令:
$bebidas = Bebida::with('tipobebida')->get();
此时$bebidas具有以下值:(我正在删除时间戳字段)
[
{"id":1,"bebi_name":"COCA-COLA","bebi_size":"1 litro",
 "bebi_price":4,"tipo_id":1,"tipobebida":null},
{"id":2,"bebi_name":"COCA ZERO","bebi_size":"1 litro",
 "bebi_price":4,"tipo_id":1,"tipobebida":null}
]
"tipobebida":null我期待的不是,而是表格内容"name":"refrigerantes"的某种表示形式。tipobebidas
我检查了正在运行的 SQL 命令,这里是:
select * from `tipobebidas` where `tipobebidas`.`id` in (?)
我怎样才能让它工作?
我将在几个嵌套foreach循环中使用这些数据来显示Bebida按类型分组的饮料TipoBebida。
谢谢!