Relationship one to one laravel 5.2 - laravel-5

i try it on this way but not dispalyed the relation
model Admission
public function dossierscocial(){
return $this->hasOne('\App\Dossierscocial');
}
model Dossiersocial
public function admission(){
return $this->belongsTo('\App\Admission');
}
in my view
#if(isset($admission->dossiersocials))
#foreach($admission->dossiersocials as $dossiersocial)
<tr>
<td>{{$dossiersocial->id}}</td>
<td>{{$dossiersocial->user_id}}</td>
<td>{{$dossiersocial->patient_id}}</td>
<td>{{$dossiersocial->admission_id}}</td>
<td>{{$dossiersocial->nationalite}}</td>
</tr>
#endforeach
#endif

because hasOne return single object not collection so you can not use foreach.
there is just one object you can use it like this:
#if(isset($admission->dossiersocials))
<tr>
<td>{{$admission->dossiersocials->id}}</td>
<td>{{$admission->dossiersocials->user_id}}</td>
<td>{{$admission->dossiersocials->patient_id}}</td>
<td>{{$admission->dossiersocials->admission_id}}</td>
<td>{{$admission->dossiersocials->nationalite}}</td>
</tr>
#endif
or maybe you can use hasMany instead of hasOne

Related

How to get the counts of Student for each Teachers in a single collection by Eloquent

A Teacher has many Students. When I am showing the Teachers list I also want to show the counts of Student for each Teachers. How can I do this by using Eloquent?
I can find the Teachers from this,
$teacher= Teacher::where('teacher_status','active')->get();
I can find the Student count from this
$student_count = Student::where('teacher_id','teachers.id')->count();
How can I conbine this two query and return the response in a single array/collection?
In your teacher model, create the relationship students:
class Teacher extends Model
{
public function students()
{
return $this->hasMany(Student::class, 'teacher_id');
}
}
In your controller you can do the following:
public function example(){
$teachers = Teacher::where('teacher_status','active')->withCount('students')->get();
return view('teacherViewExample', compact('teachers'));
}
In your view (teacherViewExample):
<table>
<thead>
<tr>
<th>Teacher Name</th>
<th>Students Count</th>
</tr>
</thead>
<tbody>
#foreach($teachers as $teacher)
<tr>
<td>{{ $teacher->name }}</td>
<td>{{ $teacher->students_count }}</td>
</tr>
#endforeach
</tbody>
</table>
Full documentation on how to use withCount() here: https://laravel.com/docs/9.x/eloquent-relationships#counting-related-models
Sometimes you may want to count the number of related models for a
given relationship without actually loading the models. To accomplish
this, you may use the withCount method. The withCount method will
place a {relation}_count attribute on the resulting models:
If you want to count the number of students related to teacher without actually loading them you may use the withCount method and this add new propery named by a {relation}_count column on your resulting models. For example:
Teacher::where('teacher_status','active')->withCount('students')->get();
Also you need and to your Teacher model hasMany relation method to Students
In case frontend and backend are separated, i.e Laravel for backend, Angular for frontend, below are examples for Laravel:
In api.php,
Route::get('teacher-with-students-count', 'TeacherController#getTeacherWithStudentsCount');
In Teacher.php (model)
class Teacher extends Model
{
public function students()
{
return $this->hasMany(Student::class, 'teacher_id', 'id');
}
}
In TeacherController.php
public function getTeacherWithStudentsCount()
{
$teachers = Teacher::where('teacher_status','active')->withCount('students')->get();
return $this->giveSuccessResponse($teachers);
}

How I car write Controller function on many to many with relation attributes

I am working on applying many to many relations on table Accident, Car where the relations are as follows:
In the Accident model:
public function car()
{
return $this->belongsToMany(Car::class,'accident_participateds',
'reportNo','vehicleId');
}
In-car Model
public function accidents()
{
return $this->belongsToMany(Accident::class,'accident_participateds',
'vehicleId','reportNo')->withPivot(['damageAmount', 'IBAN']);
}
I want to display the data from all 3 Models (Car, Accident,accident_participateds)My question is whether these statements are correct in the controller
public function AdminClaims()
{
$claim = Accident::with('car')->get();
$details = Car::with('accidentsr')->get();
return view('admin.AdminClaims',compact('claim','details'));
}
Because when I try to display like this:
<tbody>
#foreach($claim as $claims)
<tr>
<td>{{$claims->ReportNumber}}</td>
#foreach($details as $AP)
<td>{{$AP->vehicleId}}</td>
<td>{{$AP->Location}}</td>
<td>{{$AP->Date}}</td>
<td>{{$AP->damageAmount}}</td>
<td>{{$AP->IBAN}}</td>
#endforeach
</tr>
#endforeach
it print only ReportNumber
kindly tell me where I made a mistake?
From what I understand, you want to display the details of Accident(claim), associated Car and data from the pivot table.
To do so you can try
public function AdminClaims()
{
$claims = Car::with('accidents')->get();
return view('admin.AdminClaims',compact('claims'));
}
And in view
#foreach($claims as $claim)
<tr>
<td>{{$claim->ReportNumber}}</td>
<td>{{$claim->vehicleId}}</td>
<td>{{$claim->Location}}</td>
<td>{{$claim->Date}}</td>
<td>{{$claim->pivot->damageAmount}}</td>
<td>{{$claim->pivot->IBAN}}</td>
</tr>
#endforeach

get access to collection laravel

I am a new learner of the laravel framework working on 3 models
customer,
accidents,
License
where the relations are as follows:
public function License()
{
return $this->hasOne(license::class,'driver_id');
}
public function cars()
{
return $this->hasMany(car::class);
}
In the controller I do like this:
public function AdminCustomers()
{
$customers = Customer::with('cars')->with('License')->get();
return view('admin.AdminCustomers',compact('customers'));
}
now I want to display all 3 models' data on view.blade page
<tbody>
#foreach($customers as $customer)
<tr>
<td>{{$customer->id}}</td>
<td>{{$customer->name}}</td>
<td>{{$customer->adderss}}</td>
<td>{{$customer->mobileNo}}</td>
<th>{{$customer->License->id}}</th>
<th>{{$customer->License->Exp}}</th>
<td>{{$customer->cars->id}}</td>
<td>{{$customer->cars->color}}</td>
<td>{{$customer->cars->model_no}}</td>
<td>{{$customer->cars->company}}</td>
</tr>
#endforeach
</tbody>
But it doesn't work and I didn't know where is the problem
the error Exception
Property [id] does not exist on this collection instance.
$customer->cars will return a collection as the relationship is hasMany so you need to loop over the collection in the view
#foreach($customers as $customer)
<tr>
<td>{{$customer->id}}</td>
<td>{{$customer->name}}</td>
<td>{{$customer->adderss}}</td>
<td>{{$customer->mobileNo}}</td>
<th>{{$customer->License->id}}</th>
<th>{{$customer->License->Exp}}</th>
#foreach($customer->cars as $car)
<td>{{$car->id}}</td>
<td>{{$car->color}}</td>
<td>{{$car->model_no}}</td>
<td>{{$car->company}}</td>
#endforeach
</tr>
#endforeach
check the name in the corrisponding table on db, and if its different from "id" specify it into the model, as
protected $primaryKey = 'xxx';

Display Name instead of ID in laravel 8

I have two tables: User and Organisation. User contains foreign key organisation_id referencing organisation. For viewing user, it shows error 'Trying to get property 'name' of non object.
UserController
public function view($id)
{
$record = User::find($id);
return view('user.view',compact('record'));
}
User.php
public function getOrg()
{
return $this->belongsTo(Organisation::class);
}
View.blade.php
<tr>
<th>Organisation</th>
<td>{{ $record->organisation_id->name }}</td>
</tr>
Try to refer another question and answer but still can't solve it
In User.php your method name should be oranisation instead of getOrg. Laravel calls this method organisation() behind the scenes when accessing the $user->organisation property.
public function organisation()
{
return $this->belongsTo(Organisation::class, 'organisation_id', 'id');
}
Then in view.blade.php a slight tweak:
<tr>
<th>Organisation</th>
<td>{{ $record->organisation->name }}</td>
</tr>
First, your UserController does not have any bugs in it. However, consider using Implicit Route Model Binding to automatically fetch your User as an argument in your UserController method after you have gotten the rest of your code working.
Second, your User.php isn't defining the relationship according to Laravel's convention and I think that is a source of the problem. Prefer, instead, the following:
public function organisation()
{
return $this->belongsTo(Organisation::class);
}
The organisation() method utilizes a "magic" method that allows you to fetch the full Eloquent model of the relationship using the following in view.blade.php:
<tr>
<th>Organisation</th>
<td>{{ $record->organisation->name }}</td>
</tr>
Your code breaks for the following reasons:
$record->organization_id is referring to the actual number in the db, not the related model. To get the related model in your example, you would need to do $record->getOrg(). However, for reasons stated above, you should rename that method to make better use of Laravel's conventions.
Additionally, methods that begin with get...() are considered accessors to additional model attributes not found in the DB. So try to avoid using them for simple relationships.
Please let me know if you have any questions.
you have define the relation in the query
public function view($id)
{
$record = User::with('getOrg')->find($id)->first();
return view('user.view',compact('record'));
}
In View
<tr>
<th>Organisation</th>
<td>{{ $record->getOrg->name }}</td>
</tr>
Try this way

Linking to tables and get results through view(blade)

#foreach ($despatchitems as $despatchitem)
<tr>
<td>{{$despatchitem->tag_id}}</td>
<td>{{$despatchitem->qty}}</td>
<td>{{$despatchitem->description}}</td>
</tr>
#endforeach
i need to have the tag name instead of the tag_id, How can i make the connection to Tag table and get the tag name
There is no Direct relationship between DespatchItem Model and Tag Model
You can use the belongsTo relationship in your DespatchItem Model.
public function tags()
{
return $this->belongsTo('App\Tag','tag_id');
}
and Use in your View like below way.
<td>{{$despatchitem->tags->tag_name}}</td>

Resources