次の移行があります。
表: ベビダス:
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');
}
}
表:ティポビダス
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
の (飲み物) にはTipoBebida
(飲み物の種類) があり、その逆もあります。bebidas
テーブルとテーブルのすべてのフィールドを表示する構成テーブルを取得しようとしていtipobebidas
ます。
Laravel の熱心な読み込みに関するドキュメントに基づいて、次のコマンドを実行しています。
$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
、 type ごとにグループ化された飲み物を表示しますTipoBebida
。
ありがとうございました!