【发布时间】:2017-10-21 08:17:47
【问题描述】:
所以我有两张桌子:
Schema::create('goods', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('amount');
$table->integer('shop_id');
$table->timestamp('onPurchase');
$table->timestamps();
});
Schema::create('shops', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('adress')->nullable();
$table->timestamps();
});
我想打印所有商品的清单并将商店名称添加到商品的每一行。当路由打开时,此函数运行:
function toBuy(){
$good = Good::all();
$data ['good'] = $good;
return view('tobuy', $data);
}
刀片:
@foreach ($good as $ginfo)
<tr>
<td>{{$ginfo->name}}</td>
<td>{{$ginfo->amount}}</td>
<td>
@foreach ($ginfo->shop as $shop)
{{$shop->name}}
@endforeach
</td>
<td>
<a href="{{ route ('goodDelete', $ginfo->id) }}" type="submit" class="btn btn-default">Delete</a>
</td>
</tr>
@endforeach
那么我如何使用模型 Good 和 Shop 建立适当的关系并打印我需要的东西。尝试了很多方法,都失败了。请帮忙。
【问题讨论】:
标签: php sql laravel controller