Php 如何从父数组中的子记录中检索?
车辆(车辆详细信息的父项) id-某些列1 1-某些值Php 如何从父数组中的子记录中检索?,php,laravel,parent-child,Php,Laravel,Parent Child,车辆(车辆详细信息的父项) id-某些列1 1-某些值 class Vehicle extends Model { public function carDetails() { return $this->hasMany('App\CarDetail'); } } class CarController extends Controller { $carDetails = Vehicle::with('carDetails')->first(
class Vehicle extends Model {
public function carDetails() {
return $this->hasMany('App\CarDetail');
}
}
class CarController extends Controller {
$carDetails = Vehicle::with('carDetails')->first();
}
2-一些_值
class Vehicle extends Model {
public function carDetails() {
return $this->hasMany('App\CarDetail');
}
}
class CarController extends Controller {
$carDetails = Vehicle::with('carDetails')->first();
}
车辆(车辆详细信息的父项):
id-某些列2
1-奔驰
2-宝马
车辆详细信息(儿童):
id-车辆id-车辆id-某些列3
1-1-1-某些_值
class Vehicle extends Model {
public function carDetails() {
return $this->hasMany('App\CarDetail');
}
}
class CarController extends Controller {
$carDetails = Vehicle::with('carDetails')->first();
}
2-1-1-某些_值
class Vehicle extends Model {
public function carDetails() {
return $this->hasMany('App\CarDetail');
}
}
class CarController extends Controller {
$carDetails = Vehicle::with('carDetails')->first();
}
3-1-2-一些_值
class Vehicle extends Model {
public function carDetails() {
return $this->hasMany('App\CarDetail');
}
}
class CarController extends Controller {
$carDetails = Vehicle::with('carDetails')->first();
}
4-1-2-一些_值
class Vehicle extends Model {
public function carDetails() {
return $this->hasMany('App\CarDetail');
}
}
class CarController extends Controller {
$carDetails = Vehicle::with('carDetails')->first();
}
现在,在我的blade视图中,我想分别迭代汽车及其子代的详细信息:
First Car:
@foreach($carDetails as $carDetail)
<ul>
<li>Car: {{$carDetail->car->some_column2}}</li>
<li>Car Detail: {{$carDetail->some_column3}}</li>
</ul>
@endforeach
Second Car:
@foreach($carDetails as $carDetail)
<ul>
<li>Car: {{$carDetail->car->some_column2}}</li>
<li>Car Detail: {{$carDetail->some_column3}}</li>
</ul>
@endforeach
//So on...
第一辆车:
@foreach($carDetail作为$carDetail)
- 汽车:{{$carDetail->Car->some_column2}
- 汽车详细信息:{{$carDetail->some_column3}
@endforeach
第二辆车:
@foreach($carDetail作为$carDetail)
- 汽车:{{$carDetail->Car->some_column2}
- 汽车详细信息:{{$carDetail->some_column3}
@endforeach
//等等。。。
问题是,汽车的细节显示不断,但我想分开的第一个汽车的细节,第二个细节和汽车显示之后
我怎样才能做到这一点呢?我找到了:)
我不是在SQL中使用了groupBy
,而是在获取数据之后使用的
$vehicle = Vehicle::find($id);
$vehicle->cars = $vehicle->cars->groupBy('car_id');
无论如何谢谢你你的问题不清楚。你期望的结果是什么?这是什么意思,他们“连续显示”?这意味着我希望它像car1=>speed-color | car2=>speed-color这样显示。。。但是它是这样的:car1=>speed-color-speed-color。