Laravel leftJoin error Creating default object from empty value - php

I'm trying to take out "superadmins" by using this controller:
$user->superadmin = DB::table('role_users AS ru')
->leftJoin('users AS u', 'u.id', '=', 'ru.user_id')
->where('role_id', '=', '5')
->select("u.*")
->get('');
And blade:
#foreach ($user->superadmin as $superadmin)
{{ $superadmin->username }}
#endforeach
"users" table have a unique ID. That unique ID is the same as role_users.user_id.
But i'm getting error:
Creating default object from empty value
Any help? Thanks!

$user->superadmin = ...;
Creating default object from empty value
You need to define $user as some object before trying to set a property on it (using it like an object).
$user = new stdClass;
$user->superadmin = ...;
I am not sure what type you are expecting $user to be besides some object.

remove get(' ') from get as '' it selects an empty value
$user->superadmin = DB::table('role_users AS ru')
->leftJoin('users AS u', 'u.id', '=', 'ru.user_id')
->where('role_id', '=', '5')
->select("u.*")
->get();
To get users with the specific role id :
User::with(['role_users'=>function($user) use($someId){
$user->where('role_id', $someId);
}]);

Try this :
$user->superadmin = DB::table('role_users')->select(['users.*'])
->leftjoin('users', 'users.id', '=', 'role_users.user_id')
->where('role_users.role_id','5')
->get();

Related

Trying to get property of non-object error in Laravel when assigning an ID to a variable

I'm getting the row in the following code :
$grpusrow = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->get();
And then getting the ID in this line :
$grpusrid = $grpusrow->id;
I receive the following error on the line in number 2 :
Trying to get property of non-object
->get() will always return multiple objects at once. You're simply not telling it which index of $grpusrow to get the ID from.
$grpusrows = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->get();
foreach ($grpusrows as $grpusrow) {
dump($grpusrow->id);
}
// Or
$grpusrow = DB::table('grpusrs')
->where('group_id', $id)
->where('user_id', $u_id)
->first();
dump($grpusrow->id);
Maybe 2 solutions:
If you want a unique row in return (with a primary key in arguments for example), you should try ->first() instead of ->get().
The problem is the using of where clauses, I think it works with array when you want more than one where clause, try like this :
->where([['group_id', '=', $id],['user_id', '=', $u_id]])->get(); ( or first() )

Laravel Query where All

I wondered how to make a Where All clause with Laravel
I'm trying to check if the episodes that a user saw are all the episodes of the series.
I'm using the WhereIn clause but i returns the results if i saw one episode of the serie.
$alleps get all the episodes of the serie
$seriessaw get all the episodes a user saw
Thank you for your answers !
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id);
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();
I think you would need to set a having clause which would force records to only return if there's the same amount of records as there are amount of episodes.
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->having(\DB::raw('count(*)'), count($alleps))
->get();
You should note however that this would likely break if there's any possibility of having duplicates in the userepisodes table.
When you use WhereIn, you have to pass an array as the second parameter.
$alleps = DB::table('episodes')
->select('episodes.id as ep_id')
->join('seasonsepisodes', 'episodes.id', '=', 'seasonsepisodes.episode_id')
->join('seriesseasons', 'seasonsepisodes.season_id', '=', 'seriesseasons.season_id')
->where('seriesseasons.series_id', '=', $id)->get()->toArray();
$seriesSaw = DB::table('usersepisodes')
->select('usersepisodes.episode_id as ep_id')
->where('usersepisodes.user_id', '=', Auth::user()->id)
->whereIn('usersepisodes.episode_id', $alleps)
->get();

Laravel 5.2 DB Query

I am trying to use DB to build a query like so:
Get all Distributors belonging to the User with their Accounts that have Notes.
I cant wrap my head around that query. Here is what I had that works in that it gets spits out the info I need in one array but 2 issues: the note name overwrites the account name since they are both called that, and I cant pass in a $user-> to replace the 2
Working (kind of)
$user = Auth::user();
$accounts = DB::table('distributors')
->join('accounts', function ($join) {
$join->on('distributors.vip_abbv', '=', 'accounts.dist_abbv')
->where('distributors.user_id', '=', 2);
})
->join('notes', 'notes.account_id', '=', 'accounts.id')
->select('accounts.name', 'notes.*')
->get();
What I need
$user = Auth::user();
$accounts = DB::table('distributors')
->join('accounts', function ($join, $user) {
$join->on('distributors.vip_abbv', '=', 'accounts.dist_abbv')
->where('distributors.user_id', '=', $user->id);
})
->join('notes', 'notes.account_id', '=', 'accounts.id')
->select('accounts.name (this is overwritten. can I change it to come out account_name so it doesnt?)', 'notes.*')
->get();
Try this for account name:
->select('accounts.name as custom_account_name')
Hope this helps.
Try this:
$user = Auth::user();
DB::table('distributors')
->join('accounts', function ($join) use ($user) {
$join->on('distributors.vip_abbv', '=', 'accounts.dist_abbv')
->where('distributors.user_id', '=', $user->id);
})
->join('notes', 'notes.account_id', '=', 'accounts.id')
->select('accounts.name as account_name', 'notes.*')
->get();

cant get the data I want from two different tables using Laravel

I have a table called instructor_class: user_id, class_id and I have another table classes: id, time, active.
I would like to show classes for a single user but only those classes that active is 0 or 1.
My current code looks like this:
return InstructorClass::with('classes.session')->where('user_id', '=', $userId)->get();
This code is displaying me everything, then I tried the following code:
$active = 1;
return InstructorClass::with(['classes' => function ($q) use ($active) {
$q->where('active', '=', $active); // '=' is optional
}])
->where('user_id', '=', $userId)
->get();
This again returns me same records, but of course the class property is null for each record, which at some point looks correct, but my point is if the 'active' field does not corresponds at the classes table do not show the record, seems like the where() stm within with() is optional..
I am kinda stuck here...
Would appreciate your help, opinions!
You can use ::has('classes') to only return the models that have related classes
return InstructorClass::has('classes')->with(['classes' => function ($q) use ($active) {
$q->where('active', $active);
}])
->where('user_id', '=', $userId)
->get();
Never thought it could be this simple:
return InstructorClass::with('classes.session')
->join('classes', 'classes.id', '=', 'instructor_class.class_id')
->where('classes.active', '=', 1)
->where('user_id', '=', $userId)
->get();

Laravel 5.2 Query Builder get as a single array(not 2D or 3D)

This is my query.
$mem = DB::table('user_zakathorg')
->JOIN('zakathorgs', 'user_zakathorg.zakathorg_id', '=', 'zakathorgs.ID')
->WHERE('user_id', '=', Auth::user()->id )
->SELECT('zakathorg_id', 'orgName')->get();
And the output is always one raw. like following image
If i want to accesss the zakathorg_id. I have to use a foreach loop. And I can't access
$mem[0]['zakathorg_id]
how can i use this as simply $mem['zakathorg_id']
Use first() method instead of get():
$mem = DB::table('user_zakathorg')
->JOIN('zakathorgs', 'user_zakathorg.zakathorg_id', '=', 'zakathorgs.ID')
->WHERE('user_id', '=', Auth::user()->id )
->SELECT('zakathorg_id', 'orgName')->get();
// You can access $mem->zakathorg_id now

Categories