Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kanchan77's avatar

Problem in fetching data from database - only one record shows repeatedly .

and please tell how to fetch name from id. ->for treatment_needed I want to add treatment name ,please guide

Models:

class Speciality extends Model
{
   
    protected $table = 'specialities';
    protected $fillable = ['name'];

}
class Discount extends Model
{
    
    protected $table = 'discount';
    //protected $fillable = ['state'];
    protected $fillable = ['created_at','customer_id','name','treatment_needed','date_of_admission','date_of_discharge','status'];

   
}

controller

<?php

namespace App\Http\Controllers\Partners;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Discount;
use App\Models\Speciality;
use DB;

class PatientController extends Controller
{
    public function index()
    {
        $patient = Discount::all();

       // return $patient;

        foreach ($patient as $value) {
        $value->name='';
  
        $patient = Discount::where('customer_id', $value->customer_id)->get();
        
                        if (!empty($patient) && !is_null($patient)) {
                           
                            foreach ($patient as $pt) {
                                if (!empty($pt->customer_id) && !is_null($pt->customer_id)) {
                                    $list = Speciality::select('*')->where('id', $pt->id)->first();
                                  }
                                
                                 
                              $value->treatment_needed = $list->name;
                              $value->status =$list->status;
                            }   
                           }
                     }

        return view('home', compact('patient'));
    }
}

view- home.blade.php

 @foreach ($patient  as $value)  
   
        <tr>
        <td>{{ $value->created_at}}</td>
        <td>{{ $value->customer_id }}</td>
        <td>{{ $value->name }}</td>
        <td>{{ $value->treatment_needed }}</td>
        <td>{{ $value->date_of_admission }}</td>
        <td>{{ $value->date_of_discharge }}</td>
        <td>{{ $value->status }}</td>

         <td><span type="button" class="action" data-toggle="modal" data-target="#admit">Admit</span> | <span type="button" class=" action" data-toggle="modal" data-target="#discharge">Discharge</span></td>  
      </tr>
      @endforeach
0 likes
4 replies
trin's avatar

u rewrite your $patient = Discount::all(); with $patient = Discount::where('customer_id', $value->customer_id)->get(); use different variable names. like $patients = Discount::all();

kanchan77's avatar

please tell me simple logic for fetching data from both table.

Sinnbeck's avatar

@kanchan77 this

$patients = Discount::all();

foreach ($patients as $patient) {
        $value->name='';
        if (!empty($patient) && !is_null($patient)) {
kanchan77's avatar

data fetched but how to assign values because both table have same column name.

Please or to participate in this conversation.