Linking to tables and get results through view(blade) - laravel

#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>

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

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

Trying to get property 'department' of non-object in laravel with one to many relationship

Actually I want to relationship with two tables one is the student table where some data of a student. another table has department notice. I want to access the department notice according to the student department. The student has login id, password, dept & much other information of an individual student on the student table. The screenshot is given below
Now I want to try to access according to the dept. Another Table dept. the screenshot is given below.
Here given the CseDepartment model
public function administration()
{
return $this->belongsTo('App\Administration','dept');
}
Administration model
public function department()
{
return $this->hasMany('App\CseDepartment','dept');
}
function is given below
public function individualdepartment(){
$this->AdminAuthCheck();
$id=Session::get('id');
$student=Administration::find($id);
return view('Student.department',compact('student'));
}
view page department
#foreach ($student as $students)
{{-- expr --}}
<tr>
<td>{{$students->department->name}}</td>
<td>{{$students->department->message}}</td>
<td></td>
<td></td>
<td></td>
</tr>
#endforeach
In your controller, you are calling the find() method, which gives you one object, a student.
$student=Administration::find($id);
In your blade page, you are then asking to loop on that single object and produce a collection:
#foreach ($student as $students) // There is only ONE student, this will not loop.
If you want the above to work, you can grab a collection from your controller:
// Note the plural naming convention to make it a little more clear
$students=Administration::all();
return view('Student.department',compact('students'));
Then in your blade file, you can loop like this:
#foreach ($students as $student) // note the name plural --> singluar
<tr>
<td>{{$student->department->name}}</td> // Now you have a single student
<td>{{$student->department->message}}</td>
This will resolve the non-object error.

Relationship one to one laravel 5.2

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

Laravel join query confusion

Very new to laravel and trying to do something simple but keep getting stuck. I have 3 tables
countries:
id
country
regions:
id
region
country_id
wineries:
id
winery
country_id
region_id
joined together as follows:
public function index()
{
$wineries = Country::join('wineries', 'countries.id', '=', 'wineries.country_id')
->join('regions', 'regions.id', '=', 'wineries.region_id')
->select('countries.*')
->orderBy('countries.country', 'asc')
->get()
->unique();
return view('winery.index', compact('wineries'));
}
I'm trying to return a table organized by country with each winery listed along with it's region. Countries without wineries are not to be displayed. My Index page, looks like this:
<tbody>
#foreach ($wineries as $country)
<tr>
<td colspan="2">{{ $country->country }}</td>
</tr>
#foreach ($country->wineries as $winery)
<tr>
<td>{{ $winery->winery }}</td>
<td>{{ $winery->region }}</td>
</tr>
#endforeach
#endforeach
</tbody>
All works well except I can't get the associated region for each winery since the region is not a part of the $winery array. Any advice on how to make it a part of array or how to accomplish this using a different query?
In laravel you can define relationships for Models. Models represent a Model of data (normally related to a DB table, but not always)
Laravel uses Eloquent for database defining relational tables within PHP.
http://laravel.com/docs/4.2/eloquent#relationships
For your case it sounds like you have three Models
Countries
Region
Wineries
Your counties will have many regions, but a region will only have one country.
Your regions will have many wineries, but a winery will only have one region.
To spec your relationships within Eloquent you want to write the following functions within each
(Country)
public function regions()
{
return $this->hasMany('Region');
}
(Region)
public function country()
{
return $this->hasOne('Country');
}
public function winery()
{
return $this->hasMany('Region');
}
etc..
From there you can then relate the data via the models. So if you have a Country object and wanted to get the regions you would do
$country->regions
You might want to wrap this in a loop to print all of the regions
foreach($country->regions as $region) {
echo $region->name;
}
Hope this helps
Define relationships using model. Laravel has Eloquent model in built. It's very easy to query database using that. Learning curve is well worth it.

Resources