[英]displaying an array but it return empty
Good day, i have a laravel 5.8 project and i'm having a little problem i have this code美好的一天,我有一个 laravel 5.8 项目,我有一个小问题我有这个代码
public function doctor_details($doctor_id){
$doctor = DB::table('doctors')->select('specification')->where('doctor_id', $doctor_id)->get>first();
$specs_data = explode(',', $doctor_specs);
$specs_array = [];
foreach ($specs_data as $doctor_spec) {
$doctor_spec_result = DB::table('specializations')->where { return explode('speciality_id'',', $doctor_spec)->get();
foreach ($doctor_spec_result as $doctor_spec_res) {
$specs_array[] = $doctor_spec_res->speciality_name;
}
}
return view ('doctor_details', compact('doctor', 'specs_array'));
} }
now if i do dd($doctor_spec_result);现在如果我做 dd($doctor_spec_result); the result is
结果是
as you can see i'm getting an empty array but if i do dd($specs_data);如您所见,我得到一个空数组,但如果我这样做 dd($specs_data); the result is
结果是
as you can see there's definitely a data but i can't make it work如您所见,肯定有数据,但我无法使其正常工作
this is my blade这是我的刀片
<div class="row">
<div class="col-lg-12">
<h3>{{ $doctor->doctor_name }}</h3>
</div>
<div class="col-lg-12">
@foreach( $specs_array as $spec_output )
<p>{!! $spec_output !!}</p>
@endforeach
</div>
</div>
I think you are trying to get an list only containing the values of the specification
field for the particular rows queried, so that you can then get the specialty_name
s.我认为您正在尝试获取一个列表,该列表仅包含查询的特定行的
specification
字段的值,以便您可以获取specialty_name
。 You can use the pluck
method on the builder to do this for you.您可以使用构建器上的
pluck
方法为您执行此操作。 If a search by doctor_id
could return more than one result:如果通过
doctor_id
进行的搜索可能返回多个结果:
$doctor_specs = DB::table('doctors')
->where('doctor_id', $doctor_id)
->pluck('specification')
->transform(function ($item) { return explode(',', $item); })
->flatten();
$specs_array = DB::table('specializations')
// use whereIn to find all the rows by 'specialty_id'
->whereIn('speciality_id', $doctor_specs)
->pluck('specialty_name')
->toArray();
return view ('doctor_details', compact('doctor', 'specs_array'));
Laravel 6.x Docs - Database - Query Builder - Retrieving Results pluck
Laravel 6.x 文档 - 数据库 - 查询生成器 - 检索结果
pluck
Laravel 6.x Docs - Collections - Methods - transform
Laravel 6.x 文档 - Collections - 方法 -
transform
Laravel 6.x Docs - Collections - Methods - flatten
Laravel 6.x 文档 - Collections - 方法 -
flatten
Update:更新:
Though since doctor_id
is a key there will only be one, we can remove the collection methods and deal with this simpler:虽然由于
doctor_id
是一个键,所以只有一个,我们可以删除收集方法并更简单地处理这个:
$doctor_specs = explode(
',',
DB::table('doctors')->where('doctor_id', $doctor_id)
->value('specification')
);
Ideally :理想情况下:
Or if $doctor
was retrieved with all columns from the doctors
table, including specification
:或者,如果使用
doctors
表中的所有列检索$doctor
,包括specification
:
$doctor_specs = explode(',', $doctor->specification);
You can simplify your code using join您可以使用 join 简化代码
$doctor_spec_result = DB::table('doctors')
->select('specializations.speciality_name')
->join('specializations','specializations.speciality_id','doctors.specification')
->where('doctors.doctor_id', $doctor_id)
->get();
return view ('doctor_details', compact('doctor', 'doctor_spec_result '));
Change in this line.改变这一行。
$doctor_spec_result = DB::table('specializations')->where('speciality_id', $doctor_spec)->get();
you are checking the worng attribute.您正在检查 wearg 属性。 As you are getting specification in 0 index.
当您在 0 索引中获得规范时。
speciality_id to specification
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.