I was wondering if there is a function or something else, where you can get an other element from a collection than the primary key... For example if votes would have a foreign key 'user_id', how do I check this? On the laravel doc there was only an example to check the primary key by using contains(). Can anyone help me out?
Example that checks if there is a vote with id = 2
#foreach($projects as $project)
#if ($project->votes->contains(2))
//
#endif
#endforeach
I would want something to check if there is a vote that has a 'user_id' = signed in users id
#foreach($projects as $project)
#if ($project->votes->contains('user_id' == Auth::id()))
//
#endif
#endforeach
if ($votes->contains('user_id', auth()->id())) {
//
}
In your model
public static checkForeign($thisId) {
( $thisId == Auth::user()->id ) ? return true : return false;
}
In the view
#if ( ModelName::checkForeign($project->votes->id) )
// Do something
#endif
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 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
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.
This is what I want to do:
Display a checkbox for the tvshow field entry(from the tvshow table) only if that field entry doesn't match the entries for a given user id in the 'watchedtvshow' table
Table structure for 'tvshow':
id tvshow
Table structure for 'watchedtvshow'
id uid tvid(id of the tvshow)
Here is my controller method:
$tvshow = TVShow::with('watchedtvshow')->get();
return View::make('browse',['tvshow' => $tvshow]);
My View:
#foreach($tvshow as $show)
{{ $show->title }} {{ 'a checkbox' }}
#endforeach
What I tried:
In my controller method:
$tvshow = TVShow::with('watchedtvshow')->get();
$uid = NULL;
if(Auth::check())
$uid = Auth::user()->id;
return View::make('browse',[
'tvshow' => $tvshow,
'uid' => $uid,
]);
In my view:
#foreach($tvshow as $show)
{{ $show->title }}
#foreach($show->watchedtvshow as $watchedtvshow)
#if($watchedtvshow->uid == $uid)
{{'don't show checkbox'}}
#else
{{'show checkbox'}}
#endif
#endforeach
#endforeach
The problem:
The thing is the second foreach loop executes only for the times it finds a watched tv show, otherwise it doesn't. So it just won't show any checkboxes.
I'm not an experienced coder, haven't really encountered anything like this before, I've spent three whole days trying to solve this, using count, for loops and what not, but I can't. Does anybody know how to achieve this?
First off a quick pointer: you can get the logged-in user's ID with Auth::id() rather than having to set it to null, then check if they're logged in then get the id directly off the model.
As for your problem, you're quite right that using the code you have you won't be getting the full story. What you need to do is get a list of all TV shows (regardless of user having it) and additionally a list of all TV shows the user has seen. Now, you can do this many ways, but the best 'Laravel way' is to model the relationship between User and TVShow. Your code doesn't mention this so I won't assume you have already done it. Your database is, of course, already set up for this so all you need to do is create the relationship. In this case, the relationship you need is a belongsToMany (a user can 'have' (have watched) many shows, and a show can 'have' (have been watched by) many users:
// in User.php
public function shows()
{
return $this->belongsToMany('TVShow', 'watchedtvshow', 'uid', 'tvid');
}
// in TVShow.php
public function users()
{
return $this->belongsToMany('User', 'watchedtvshow', 'tvid', 'uid');
}
Once you have this you can get a list of all users that have watched a show with:
$show->users;
Or you can get a list of all shows a user has watched with:
$user->shows;
Now putting that all together you should use Laravel's collections to detect whether a given item if in both arrays:
// in the controller:
$shows = TVShow::all();
if (Auth::check()) {
$watched = Auth::user()->shows;
} else {
// just create an empty collection so we can assume a consistent API
$watched = new \Illuminate\Support\Collection;
}
return View::make('browse', compact('shows', 'watched'));
// in browse.blade.php
#foreach($shows as $show)
{{ $show->title }}
#if ($watched->contains($show)
<span class="glyphicon glyphicon-ok"></span>
#else
<span class="glyphicon glyphicon-remove"></span>
#endif
#endforeach
Something like that?
I'm using Laravel to query users that have a score ( row in eventscore table )
And i want to return all the users that have scores for the event with an ID of 3
Is it possible to only return the score of every user, found in the pivot table ( see results below )
This is the code i'm using
$persons = Person::whereHas('eventscore', function($q) {
$q->where('id', 3);
})->with('eventscore')->get();
return $persons;
Thank you!
Try this:
// Person model
// accessor for easy fetching the score from the pivot model
public function getScoreAttribute()
{
return ($this->pivot && $this->pivot->score)
? $this->pivot->score
: null;
}
// example usage for the event's page (I assume participants is the relation)
$event = Event::with('participants')->find(3);
// somewhere in the view template
#foreach ($event->participants as $participant)
{{ $participant->name }} - score: {{ $participant->score }}
#endforeach