我对 Laravel 完全是菜鸟,不知道语言的结构和语法,但截止日期很快,我没有时间从头开始,所以我需要帮助。
我想随机显示表中的记录,但只有一张表必须只有一条记录(嵌套查询)
有什么解决办法吗?
这是我来自控制器的代码:
$alldepartments = Department::translatedIn(app()->getLocale())
->where('status', 1)
->first()
->inRandomOrder()
->get();
这是刀片:
<div style="overflow-x: scroll; overflow-y: hidden; white-space: nowrap">
<div class="d-flex flex-row flex-nowrap">
@foreach($alldepartments as $mydepartment)
@foreach($mydepartment->employees as $employee)
<div class="col-12 col-sm-6 col-lg-3">
<a href="{{ url('employee/'. $employee->id) }}">
<div class="single_advisor_profile wow fadeInUp" data-wow-delay="0.2s" style="visibility: visible; animation-delay: 0.2s; animation-name: fadeInUp;">
<div class="advisor_thumb">
<img src="{{ asset('uploads/employees/'.$employee->image) }}" alt="" />
</div>
<div class="single_advisor_details_info">
<h6>{{$employee->name}}</h6>
<p class="designation">{{$mydepartment->name}}</p>
</div>
</div>
</a>
</div>
@endforeach
@endforeach
</div>
</div>
它用于随机获取记录,但它为我提供了每个表中的所有记录,但我只需要获取一个!!
需要去掉first()方法,在get()方法前添加take(1)。 take() 方法用于获取特定数量的元素。