我正在尝试从 table 获取与 id 相关的第一张图片,但不幸的是获取所有图片而不是第一张图片
I am trying to get first image from table related to id but unfortunately getting all images instead of first image
我正在尝试从 table 获取第一张图片,但不幸的是我正在获取与 id 相关的所有图片,我只需要来自 table 的第一张图片请帮我解决这个问题?谢谢
enter image description here
房间table
编号 |姓名 |描述
31 |共同工作 space | fsdfdsf
房间详情
编号 | room_id |图片
1 | 31 | php8AB0.tmp.PNG
2 | 31 | php8AB0.tmp.PNG
**控制器**
public function index()
{
$data=[
'rooms' => Room::with('roomDetail')->orderBy('created_at','DESC')->paginate(3),
// 'roomDetail'=> RoomDetail::get(),
];
return view('cms.rooms',$data);
}
<div class="row">
<div class="col-md-12">
@if(count($rooms) > 0)
@foreach ($rooms as $value)
@foreach ($value->roomDetail as $value2)
<div class="card mb-4">
<div class="d-flex">
<img height="145px" src="{{Config('wfh.file').$value2->image}}" alt="">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div class="left">
<h4 class="fw-bold">{{$value->name}}</h4>
<p class="mb-2">{{$value->description}}</p>
<span class="badge bg-light-outline p-2 me-1"> <i class='bx bx- user'></i> {{$value->capacity}}</span>
</div>
<button class="btn bg-primary-light px-3 me-2 py-2"><i class='bx bxs-wrench'></i></button>
</div>
</div>
</div>
</div>
@endforeach
@endforeach
@endif
{!! $rooms->links('pagination::bootstrap-4') !!}
</div>
</div>
如果您只想要第一个 roomDetail
而不是全部,请不要使用 foreach 遍历它们。
而是使用 $value->roomDetail->first()
,然后从中获取您想要的属性。
例如,对于您的图片,请使用:
<img height="145px" src="{{ Config('wfh.file') . $value->roomDetail->first()->image }}" alt="">
我正在尝试从 table 获取第一张图片,但不幸的是我正在获取与 id 相关的所有图片,我只需要来自 table 的第一张图片请帮我解决这个问题?谢谢
enter image description here
房间table
编号 |姓名 |描述
31 |共同工作 space | fsdfdsf
房间详情
编号 | room_id |图片
1 | 31 | php8AB0.tmp.PNG
2 | 31 | php8AB0.tmp.PNG
**控制器**
public function index()
{
$data=[
'rooms' => Room::with('roomDetail')->orderBy('created_at','DESC')->paginate(3),
// 'roomDetail'=> RoomDetail::get(),
];
return view('cms.rooms',$data);
}
<div class="row">
<div class="col-md-12">
@if(count($rooms) > 0)
@foreach ($rooms as $value)
@foreach ($value->roomDetail as $value2)
<div class="card mb-4">
<div class="d-flex">
<img height="145px" src="{{Config('wfh.file').$value2->image}}" alt="">
<div class="card-body">
<div class="d-flex justify-content-between align-items-center">
<div class="left">
<h4 class="fw-bold">{{$value->name}}</h4>
<p class="mb-2">{{$value->description}}</p>
<span class="badge bg-light-outline p-2 me-1"> <i class='bx bx- user'></i> {{$value->capacity}}</span>
</div>
<button class="btn bg-primary-light px-3 me-2 py-2"><i class='bx bxs-wrench'></i></button>
</div>
</div>
</div>
</div>
@endforeach
@endforeach
@endif
{!! $rooms->links('pagination::bootstrap-4') !!}
</div>
</div>
如果您只想要第一个 roomDetail
而不是全部,请不要使用 foreach 遍历它们。
而是使用 $value->roomDetail->first()
,然后从中获取您想要的属性。
例如,对于您的图片,请使用:
<img height="145px" src="{{ Config('wfh.file') . $value->roomDetail->first()->image }}" alt="">