advanced where clause fails - php

I am trying to get the records from a database where players have either played as player 1 or 2 in a game to calculate the gross profit for a player on a profile. I have the following Advanced Eloquent code:
$user = User::with('profile')->where('name', $username)->first();
$user_id = $user->id;
$games_model = Games::where(function ($q) {
global $user_id;
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
$games = $games_model->limit(3)->get();
// get gross profits
$gross_profits = $games_model->sum('bet');
dd($games_model->get()->toArray());
But when I try to take a look at the results dumped, I see a blank array. I am sure my user's ID is set correctly. The funny thing is it was only returning one result before instead of all the resulkts in the database. I'm confused. Thanks for the help in advance!

Something like this should work, assuming the database and naming conventions are follow.
Users::class
public function games()
{
return $this->hasMany(Games::class);
}
Query:
Users::where('name', $username)
->has('games')
->with('profile')
->limit(3)
->get();
Haven't tested but this should give you an idea.

here's the solution.
global dosen't work every time depending on variable scope. I changed
$games_model = Games::where(function ($q) {
global $user_id;
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
to
$user_id = $user->id;
$games_model = Games::where(function ($q) use ($user_id) {
$q->where('player1', '=', $user_id)->orWhere('player2', '=', $user_id);
});
and it works perfectly every time.

Related

Check if auth user has voted and then take count of polls?

I encountered an issue that blocks my way forward.
I have tried getting count of polls as of now as shown below:
public function user_dashboard(){
$activepolls = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->count();
return view('user_dashboard',compact('activepolls'));
}
As of Now I am thinking that this below function would have help me but i am not sure.
public function hasVoted($poll_id)
{
$poll = Poll::findOrFail($poll_id);
if ($poll->canGuestVote()) {
$result = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->where('larapoll_options.poll_id', $poll_id)->count();
return $result !== 0;
}
return $this->options()->where('poll_id', $poll->id)->count() !== 0;
}
Simply i had made a poll box in my website and i want to show the count of polls in right corner of that box.
i need the count of poll in which user has not voted.
if user votes on poll it should remove from count as well.
If anyone can help , the response would much appreciated..
Thanks..
I have Solved The issue by following code...
$activepolls = DB::table('larapoll_polls')
->selectRaw('count(*) As total')
->join('larapoll_options', 'larapoll_polls.id', '=', 'larapoll_options.poll_id')
->join('larapoll_votes', 'larapoll_votes.option_id', '=', 'larapoll_options.id')
->where('larapoll_votes.user_id', auth()->user()->id)
->count();
$polls = Poll::count();
$unvotedpolls = ($polls - $activepolls);
in ($unvotedpolls) i got the count of polls in which auth user has not voted...

LARAVEL: How to fetch id dynamically in a query builder?

I want to join multiple tables in laravel with query builder. My problem is that my code only works if I specify the id myself that I want like this:
$datauser = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=','1')
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();
return response()->json($datauser);
But I would want something like this(which I just can't seem to figure out)
public function showuser($id)
{
$userid = User::findOrFail($id);
$datauser = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=',$userid)
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();
return response()->json($datauser);
}
Am I making a syntax mistake? When I check the page for my json response in second page it just returns empty brackets, but when I specify the id it fetches me the right data
The findOrFail method will return the entire user model, with all its properties, since you already have the user id. You dont need to get the entire user model for that, you could just use the $id you receveid as a parameter like this:
$datauser = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=',$id)
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();
return response()->json($datauser);
public function showuser($id)
{
$getUserByID = User::findOrFail($id); //not used
$userData = DB::table('users')
->join('activitates','users.id','=','activitates.user_id')
->join('taga_cars','taga_cars.id','=','activitates.tagacar_id')
->join('clients','users.id','=','clients.user_id')
->where('users.id','=',$id)
->select('users.*','activitates.*','taga_cars.model','taga_cars.id','clients.name')
->get();
return response()->json($userData);
}
But the best way is to have relations set on models
public function showuser($id)
{
$userData = User::where('id', $id)->with(['activitates','taga_cars','clients'])->first();
return response()->json($userData);
}

How to get data from relation in Laravel used in with()?

I am getting data from multiple tables in relation with each other. But in one of the relation I want to get userAnswers records by where('user_id, $userID). What is correct syntax for it
public function survey_completed_show($userSurvey, $userID)
{
$userSurvey = UserSurvey::with('survey.questions.userAnswers')->find($userSurvey);
return view('surveys.conducted-answers', compact('userSurvey'));
}
I just want to get answers of the selected User, I am currently getting all answers by each user
you can use deep with:
$userSurvey = UserSurvey::with(['survey'=>function($query)use( $userID){
$query->with(['questions'=>function($query)use( $userID){
$query->with(['userAnswers'=>function($query)use( $userID){
$query->where('user_id', $userID);
}]);
}]);
}])->find($userSurvey);
Assuming you only need to filter the relation and not the UserSurvey you might wanna try this
public function survey_completed_show($userSurvey, $userID)
{
$userSurvey = UserSurvey::with(['survey.questions.userAnswers' => function($q) use ($userID){
$q->where('user_id', $userID);
}])->find($userSurvey);
return view('surveys.conducted-answers', compact('userSurvey'));
}

How to fetch users not assigned to a particular role in Laravel [duplicate]

In Laravel we can setup relationships like so:
class User {
public function items()
{
return $this->belongsToMany('Item');
}
}
Allowing us to to get all items in a pivot table for a user:
Auth::user()->items();
However what if I want to get the opposite of that. And get all items the user DOES NOT have yet. So NOT in the pivot table.
Is there a simple way to do this?
Looking at the source code of the class Illuminate\Database\Eloquent\Builder, we have two methods in Laravel that does this: whereDoesntHave (opposite of whereHas) and doesntHave (opposite of has)
// SELECT * FROM users WHERE ((SELECT count(*) FROM roles WHERE user.role_id = roles.id AND id = 1) < 1) AND ...
User::whereDoesntHave('Role', function ($query) use($id) {
$query->whereId($id);
})
->get();
this works correctly for me!
For simple "Where not exists relationship", use this:
User::doesntHave('Role')->get();
Sorry, do not understand English. I used the google translator.
For simplicity and symmetry you could create a new method in the User model:
// User model
public function availableItems()
{
$ids = \DB::table('item_user')->where('user_id', '=', $this->id)->lists('user_id');
return \Item::whereNotIn('id', $ids)->get();
}
To use call:
Auth::user()->availableItems();
It's not that simple but usually the most efficient way is to use a subquery.
$items = Item::whereNotIn('id', function ($query) use ($user_id)
{
$query->select('item_id')
->table('item_user')
->where('user_id', '=', $user_id);
})
->get();
If this was something I did often I would add it as a scope method to the Item model.
class Item extends Eloquent {
public function scopeWhereNotRelatedToUser($query, $user_id)
{
$query->whereNotIn('id', function ($query) use ($user_id)
{
$query->select('item_id')
->table('item_user')
->where('user_id', '=', $user_id);
});
}
}
Then use that later like this.
$items = Item::whereNotRelatedToUser($user_id)->get();
How about left join?
Assuming the tables are users, items and item_user find all items not associated with the user 123:
DB::table('items')->leftJoin(
'item_user', function ($join) {
$join->on('items.id', '=', 'item_user.item_id')
->where('item_user.user_id', '=', 123);
})
->whereNull('item_user.item_id')
->get();
this should work for you
$someuser = Auth::user();
$someusers_items = $someuser->related()->lists('item_id');
$all_items = Item::all()->lists('id');
$someuser_doesnt_have_items = array_diff($all_items, $someusers_items);
Ended up writing a scope for this like so:
public function scopeAvail($query)
{
return $query->join('item_user', 'items.id', '<>', 'item_user.item_id')->where('item_user.user_id', Auth::user()->id);
}
And then call:
Items::avail()->get();
Works for now, but a bit messy. Would like to see something with a keyword like not:
Auth::user()->itemsNot();
Basically Eloquent is running the above query anyway, except with a = instead of a <>.
Maybe you can use:
DB::table('users')
->whereExists(function($query)
{
$query->select(DB::raw(1))
->from('orders')
->whereRaw('orders.user_id = users.id');
})
->get();
Source: http://laravel.com/docs/4.2/queries#advanced-wheres
This code brings the items that have no relationship with the user.
$items = $this->item->whereDoesntHave('users')->get();

Laravel Join Returning odd values

Just come across a bug on my Join query.
I am echoing out data in a foreach, and showing the username. On other pages it works fine and each username matches the username from the ID in the row. However, in the example below the username return is ALWAYS the name of the logged in user.
Hope the below makes some more sense. Thanks.
The query is:
$query = DB::table('blogs')
->join('followers', 'blogs.id', '=', 'followers.blogid')
->where('followers.userid', Auth::user()->id)
->where('frontpage', '1')
->latest('lastupdated');
This is called via:
Route::get('following', array('before' => 'auth', function()
{
$slug = Route::getCurrentRoute()->uri();
$builds = Blog::findBuilds($slug);
return View::make('pages/home', compact('builds'), array('pageTitle' => 'Builds You Are Following'));
}));
And then on the pages/home I am showing the data like so:
foreach ($builds as $build)
{
$usernameOfOwner = User::usernameFromID($build->userid);
}
And then... the function for getting the username from ID is:
public static function usernameFromID($id) {
$user = DB::table('users')->where('id', $id)->first();
return $user->username;
}
Everywhere else on my website when I run a query similiar to the top one but not a join so e.g.:
$query = static::where('frontpage', '1')->latest('lastupdated');
It works fine, so my only guess is that its down to the Join as thats the only different part of the code.
The problem is that you have multiple columns named userid. followers.userid and blogs.userid. Now in this case, unfortunately followers.userid gets returned when you use $build->userid
You can change that by only selecting the columns you want in your result.
$userid = Auth::user()->id;
$query = DB::table('blogs')
->join('followers', function($q) use ($userid){
$q->on('blogs.id', '=', 'followers.blogid');
$q->where('followers.userid', '=', $userid);
})
->select('blogs.userid', 'other-column')
->where('frontpage', '1')
->latest('lastupdated');
By the way: * works too, so you can do select('blogs.*') if you want

Categories