0

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 enter image description here

as you can see i'm getting an empty array but if i do dd($specs_data); the result is enter image description here

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>
7
  • should there only be one doctor by doctor_id ... is it a key? is $doctor a model instance representing the doctors table? Commented Nov 15, 2019 at 6:17
  • is speciality a field that can contain a comma separated list of ids? trying to figure out why the explode Commented Nov 15, 2019 at 6:24
  • yes it some of the records has two ids @lagbox Commented Nov 15, 2019 at 6:27
  • is doctor_id unique on the doctors table? Commented Nov 15, 2019 at 6:28
  • @lagbox, yes it is a primary key from the doctors table, it's from my index page i passed the id to a link to another page to display the doctor's full info, this includes the specialities. Commented Nov 15, 2019 at 6:33

3 Answers 3

1

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_names. 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);
Sign up to request clarification or add additional context in comments.

2 Comments

@JamesRussel I updated it now that I know there will only be one doctor by doctor_id ... can you check to make sure this version works for you still?
the updated answer is giving me no result, the last one was perfect, and again thank, thank you so much, i'm only in my first few months learning laravel
0

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 '));

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.