简体   繁体   中英

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

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); the result is在此处输入图像描述

as you can see i'm getting an empty array but if i do 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. You can use the pluck method on the builder to do this for you. If a search by doctor_id could return more than one result:

$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 Docs - Collections - Methods - transform

Laravel 6.x Docs - Collections - Methods - 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_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 :

$doctor_specs = explode(',', $doctor->specification);

You can simplify your code using 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. As you are getting specification in 0 index.

speciality_id to specification

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM