Level 5
u rewrite your $patient = Discount::all(); with $patient = Discount::where('customer_id', $value->customer_id)->get();
use different variable names. like $patients = Discount::all();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
Please or to participate in this conversation.