将数据合并为一个变量laravel php laravel
我有三个模型:将数据合并为一个变量laravel php laravel,php,laravel,laravel-6,Php,Laravel,Laravel 6,我有三个模型:Country.php、Category.php和Region.php。所有这些模型在数据库中都包含一个名为title的公共字段 我想从每个模型中提取title字段,并创建一个数组变量,以便在视图中的foreach循环中运行它 这样做的最佳方法是什么?选择了联合查询: $opt1=DB::table('countries') ->选择('countries.name'); $opt2=DB::表('tourcategories') ->选择('tourcategories.nam
Country.php
、Category.php
和Region.php
。所有这些模型在数据库中都包含一个名为title
的公共字段
我想从每个模型中提取title
字段,并创建一个数组变量,以便在视图中的foreach
循环中运行它
这样做的最佳方法是什么?选择了
联合查询:
$opt1=DB::table('countries')
->选择('countries.name');
$opt2=DB::表('tourcategories')
->选择('tourcategories.name');
$opt3=DB::表('区域')
->选择('regions.name')
->工会(opt1)
->工会(opt2)
->get();
dd(opt3)代码>https://laravel.com/docs/6.x/collections#method-Pull
将帮助您提取标题
。拔毛会给你一个阵法。您可以合并生成的数组以获得所需的数组。
// 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