将数据组合成一个变量 laravel php laravel
Combine data into one single variable laravel php laravel
我有三个模型:Country.php
、Category.php
和 Region.php
。所有这些模型在数据库中都包含一个名为 title
的公共字段。
我想从每个模型中提取 title
字段并创建一个数组变量,这样我就可以 运行 它在我看来的 foreach
循环中。
这样做的最佳方法是什么?
进行了 union
查询:
$opt1 = DB::table('countries')
->select('countries.name');
$opt2 = DB::table('tourcategories')
->select('tourcategories.name');
$opt3 = DB::table('regions')
->select('regions.name')
->union($opt1)
->union($opt2)
->get();
dd($opt3);
如果有人有其他最佳方法,请随时post。
// assuming that you have related this models
$counties = County::with(['category','region'])->get();
// And in view
@foreach($counties as $county)
{{$county->title}}
{{$county->category->title}}
{{$county->region->title}}
@endforeach
我有三个模型:Country.php
、Category.php
和 Region.php
。所有这些模型在数据库中都包含一个名为 title
的公共字段。
我想从每个模型中提取 title
字段并创建一个数组变量,这样我就可以 运行 它在我看来的 foreach
循环中。
这样做的最佳方法是什么?
进行了 union
查询:
$opt1 = DB::table('countries')
->select('countries.name');
$opt2 = DB::table('tourcategories')
->select('tourcategories.name');
$opt3 = DB::table('regions')
->select('regions.name')
->union($opt1)
->union($opt2)
->get();
dd($opt3);
如果有人有其他最佳方法,请随时post。
// assuming that you have related this models
$counties = County::with(['category','region'])->get();
// And in view
@foreach($counties as $county)
{{$county->title}}
{{$county->category->title}}
{{$county->region->title}}
@endforeach