I am trying to fetch a user based on their corresponding District/ area by the ID stored in the user table,
I am able to fetch the result for District where as for the Area, I am getting error,
Trying to get property 'area_name' of non-object (View:
user-index.blade.php)
User
id | district_id | area_id | user_name
District
id | district_name
area
id | district_id | area_name
Controller
$users = Users::simplePaginate(5);
return view('user-index',compact('users'));
Model
User
public function districts(){
return $this->belongsTo('App\Districts','district_id','id');
}
public function areas(){
return $this->belongsTo('App\Area','area_id','id');
}
blade
#foreach ($users as $user)
{{$user-> first_name}} - I get result
{{$user->districts->district_name}} - I get result
{{$user->areas->area_name}} -- I get error
#endforeach
Try this in your controller:
$users = Users::with('areas')->with('districts')->simplePaginate(5);
return view('user-index',compact('users'));
Edit Answer:
In User Model:
public function districts() {
return $this->belongsTo('App\Districts','district_id','id');
}
In District Model:
public function areas() {
return $this->belongsTo('App\Area','district_id','id');
}
And In you Controller, you will fetch the relation like this:
$users = Users::with('districts.areas')->simplePaginate(5);
return view('user-index',compact('users'));
I hope it would be helpful.
Error occurs becouse same user does not have area. For fix it use this
#foreach ($users as $user)
{{$user->first_name}}
{{$user->districts->district_name ?? ''}}
{{$user->areas->area_name ?? ''}}
#endforeach
or
#foreach ($users as $user)
{{$user-> first_name}}
#if($user->districts)
{{$user->districts->district_name}}
#endif
#if($user->areas)
{{$user->areas->area_name}}
#endif
#endforeach
Also for belongs to relations use singulare name
public function district(){
return $this->belongsTo('App\Districts','district_id','id');
}
public function area(){
return $this->belongsTo('App\Area','area_id','id');
}
That case in blade use
#foreach ($users as $user)
{{$user->first_name}}
{{$user->district->district_name ?? ''}}
{{$user->area->area_name ?? ''}}
#endforeach
And for avoid lazy loading in controller use
$users = Users::with(['area', 'district'])->simplePaginate(5);
return view('user-index',compact('users'));
Try in your controller
$users = Users::with(['areas','districts'])->simplePaginate(5);
return view('user-index',compact('users'));
In your blade file check relationship is set or not.
#foreach ($users as $user)
{{ $user->first_name }}
{{ $user->districts ? $user->districts->district_name : '' }}
{{ $user->areas ? $user->areas->area_name : '' }}
#endforeach
Related
i have 2 tables ( users and feedbacks ) . in the the 2nd table i have user_id as a foreign key and i'm trying to get the name of the user who made the feedback
in User.php :
public function feedbackuser()
{
return $this->hasMany('App\Feedback');
}
in the Feedback.php :
public function usersfeed(){
return $this->belongsTo('App\User','user_id');
}
i tried those to methodes in y view :
{{$feedbacks->yser_id->name}}
and
{{$feedback->user_id['name']}}
NB : this was working but when i added the relationships in phpmyadmin it stopped
i was using this code :
#foreach($users as $user)
#if ($user->id == $feedback->user_id)
{{$user->name}}
#endif
#endforeach
You have to query the relationship first then you can access the relational data.
Here is a pseudo code example:
{{ $user->feedbackuser->name }}
or the inverse of:
{{ $feedback->usersfeed->user_id }}
I'm using Laravel 5.6 and I have created two Models Question and User Model and these are linked to each other using one to many relation as:
Question Model
public function user() {
return $this->belongsTo('App\User');
}
User Model
public function questions() {
return $this->hasMany('App\Question');
}
And my controller code is:
public function index()
{
$user = User::all();
return view('home', compact('user'));
}
So, i'm trying to get question title and I have written this code in blade:
#foreach($user as $user)
{{ dd($user->questions->questions_title) }}
#endforeach
But getting error undefined index questions_title, but if only write this {{ dd($user->questions) }} it gave me all questions, so how to fix it.
I have also tried {{ dd($user->questions['questions_title']) }} but not fixed.
You'll want to loop over your questions relationship to see your question:
#foreach($user as $u)
#foreach($u->questions as $question)
{{ dd($question->questions_title) }}
#endforeach
#endforeach
Note: I changed $user to $u
In your controller, you are not eager loading your questions relationships. Change your code to the following:
public function index()
{
$user = User::with('questions')->get();
return view('home', compact('user'));
}
Using the with() method will load all specified relationship with the eloquent query. After this, you will need to loop through the questions collection using the ->each() collection method.
#foreach($users as $user)
#foreach($user->question as $question)
{{ $question->question_title }}
#endforeach
#endforeach
No need for two foreach loops we can simply use one like below:
#foreach ($user->questions as $question)
{{ dd($question->questions_title) }}
#endforeach
I'm trying to get all the users where a given method in User model meets. Please see my code below:
User.php
public function isPicker(){
return ($this->where('isPicker', 1)) ? true : false;
}
Now, I can use User::all();, but it returns all the users. What I want is to only return the users that meets the isPicker() method. What I'm trying to do in view is:
#foreach($users as $user)
#if($user->isPicker())
{{ $user->first_name }}
#endif
#endforeach
This is also working fine, but it is not that efficient to use. What if there's a lot of method to check? Any idea for this?
Just do:
$users = User::where('isPicker', 1)->get();
Or create a scope:
public function scopeIsPicker($query)
{
return $query->where('isPicker', 1);
}
// usage
$users = User::isPicker()->get();
Well you could change you code up a little to look like this.
#foreach($users->where('isPicker', 1)->all() as $user)
{{ $user->first_name }}
#endforeach
But this will only work if the users var is a collection.
Other wise just change you query on how your getting the users to something like this.
User::where('isPicker', 1)->get()
Instead of checking in model file, you can directly query in your controller try below code
if you want to display only isPicker=1 users.
Controller Code :
$users = User::where('isPicker', 1)->get();
Blade code :
#foreach($users as $user)
{{ $user->first_name }}
#endforeach
OR
if you want to display all users including isPicker 0&1.
Controller Code :
$users = User::all();
Blade code :
#foreach($users as $user)
#if($user->isPicker == 1)
{{ $user->first_name }}
#else
<p>Picker is 0</p>
#endif
#endforeach
Note : Remove your isPicker function from user model file because its unuse.
I have two database table
1. patient
--id
--name
2. report
--id
--description
and pivot talbe
patient_report
--id
--report_id
--patient_id
My Patient Model
class Patient extends Model
{
public function reports()
{
return $this->belongsToMany('App\Report' , 'patient_reports');
}
}
My Report Model
class Report extends Model
{
public function patients()
{
return $this->belongsToMany('App\Patient' , 'patient_reports');
}
}
My ReportControlller
public function viewList($reportFloor = null)
{
$report = Report::orderBy('created_at' , 'desc')->paginate(50);
return view('admin.report_list' , ['reports' => $report]);
}
Database : patients table have report_id column and reports table have patient_id column
N.B : I want to find the patient name who has a report. And i am using the laravel dynamic properties like that ---
And finally my blade
#foreach ($reports as $report)
{{ $report->patients->name }}
#endforeach
But it provides an error like that
Try this:
#foreach ($reports->patients as $patient)
{{ $patient->name }}
#endforeach
As the error implies, you're attempting to access the name attribute on a collection. To fix you should change:
#foreach ($reports as $report)
{{ $report->patients->name }}
#endforeach
to:
#foreach ($reports as $report)
#foreach ($report->patients as $patient)
{{ $patient->name }}
#endforeach
#endforeach
In your controller, i would first find the patients and afterwards get their reports.
$patients = Patient::with('reports')->get();
And then in the view i would do:
//This will first get all the patients
#foreach ($patients as $patient)
// This will get each of patients relational reports
#foreach ($patient->reports as $report)
{{ $report->name }}
#endforeach
#endforeach
I'm trying to get the total comments the user have..
Controller:
public function index()
{
$setting = Setting::findOrFail(1);
$comments = Comment::where('published', '=', '1')->get();
$users = User::all();
return view('page.index', compact('setting', 'comments', 'users'));
}
View:
#foreach($comments as $comment)
{{ count($users->where('user_id', '=', $comment->user_id)) }}
#endforeach
The problem is that it only returns 0 and i have 2 comments there.. even using the user id to instead of "$comment->user_id" it doesnt work. still display 0.
$users is a collection, not a query. Treat it as such:
#foreach ($comments as $comment)
{{ $users->where('user_id', $comment->user_id)->count() }}
#endforeach
The collection's where method does not take an operator.
From the wording in your question it seems you actually want it the other way around:
$comments = Comment::wherePublished(1)->get()->groupBy('user_id');
Then in your view:
#foreach ($users as $user)
{{ $comments->has($user->id) ? count($comments[$user->id]) : 0 }}
#endforeach
I'm late to answer your question and Joseph already showed you the problem but you may also do it differently. You can group comments using Collection::groupBy, for example:
$comments = Comment::all()->groupBy('user_id');
Then in your view you may try this:
#foreach($comments as $user => $userComments)
// count($userComments)
// Or
#foreach($userComments as $comment)
// {{ $comment->title }}
#endforeach
#endforeach
In the first loop (#foreach($comments as $user => $userComments)), the $user is the user_id and it's value would be all comments (an array) by this user but in group.