Laravel Spatie Call to undefined method Illuminate\Database\Query\Builder::assignRole() - laravel

Trying to do assignRole() to many users, but it show me Call to undefined method Illuminate\Database\Query\Builder::assignRole(). Is this spatie laravel bug?
$get_username = \App\Applicant::select('username')->whereIn('id', $ids);
$updateUser = \App\User::whereIn('username', $get_username);
$updateUser->assignRole('Applicant');
$updateUser->save();
Any solution for bulk assignRole() in this case?

$get_username = \App\Applicant::select('username')->whereIn('id', $ids);
This is an incomplete query via the QueryBuilder. As well as the second line.
When utilizing the QueryBuilder you need to finish of your queries with the get() method to get a collection to iterate over.
The correct execution would like like this:
$get_username = \App\Applicant::select('username')->whereIn('id', $ids)->get();
Without knowing the exact result of this, it looks like you'd like to retrieve a collection of usernames, that you'd like to get the User model from.
You could do something like the following (untested) for bulk updating.:
$users = User::whereIn('username', function($query) use ($ids) {
$query->select('username')
->from('applicants')->whereIn('id', $ids);
})->get();
foreach($users as $user) {
$user->assignRole('Applicant');
$user->save();
}

Related

Refactor whereHas to use via global scopes in Laravel

I've 2 models, CoreChallenge and Challenge. Below are relations between table.
I want to fetch Challenges which has core_challenges active. I tried to do putting global scope in CoreChallenge model, but when I'm getting null in relationship when Corechallenge is inactive.
I've done it this way
$challenges = Challenge::with('core_challenge')->whereHas('core_challenge', function($q){
$q->where('status', '=', 'active');
})->get();
I want to do it using global scopes
Global scope on CoreChallenge gives me null, but I want that it's parent (Challenge) should not load even, like in whereHas. Is there any way?
I have stumbled to the same approach, but when the table got bigger (core_challenges_table in your scenario), whereHas ended up being very slow (around 1min response time).
So I used a solution like this:
$ids = CoreChallenge::where('status', 'active')->pluck('id');
$challenges = Challenge::with('core_challenges')
->whereIn('core_challenge_id', $ids)
->get();
With this approach, my query reduced to 600~ms from 1min.
Which can be translated to Model scopes
class Challenge {
public function scopeActive($query) {
$activeIds = CoreChallenge::where('status', 'active')->pluck('id');
return $query->whereIn('core_challenge_id', $ids);
}
}
Challenge::with('core_challenges')->active()->get();

How to paginate and orderBy when getting data from relation model in laravel

I want to order by created_at posts in descending order
I also would like to paginate posts.
My issue is, I am not getting data directly through Post Model, I don't know how to do this.
I am using Laravel 5.7. This is what I have written inside a function in HomeController.
$id = auth()->user()->id;
$user = User::find($id);
return view('home')->with('posts',$user->posts);
Thank You.
Try this:
$posts = auth()->user()->posts()->latest()->paginate();
return view('home', compact('posts'));

How to filter a Laravel collection

I have to following eloquent db statements in my function. Two read statements on the db.
$groups = V_Member::where('groupadmin', '=', Auth::id())->get();
$members = V_Member::where([['idgroup', $groupid],['groupadmin', Auth::id()]])->get();
First I want to execude the first db statement and then I want to filter on the collection/variable.
Like this below. But I get an error. What I have to change?
$groups = V_Member::where('groupadmin', '=', Auth::id())->get();
$members = $groups::where(['idgroup', '=', $groupid])->get();
This is the error message
Non-static method Illuminate\Support\Collection::where() should not be called statically
You can just use:
$members = $groups->where('idgroup', $groupid);
Operator= is not necessary but you can use it where you want. You can also take a look at where method documentation.
If you want to filter the collection instead of running 2 queries, then you will just need to change:
$members = $groups::where(['idgroup', '=', $groupid])->get();
to:
$members = $groups->where('idgroup', '=', $groupid);
For more information you can have a look at the documentation
Since $groups is a collection and not a Model Class...
You should use ->where() instead of ::where()
Don't confuse collections with eloquent querys, it's a common mistake.
Also, don't confuse methods for building querys with eloquent, with methods for collections, another common mistake.

Laravel Eloquent: Get current id from inner function

I know, we can do this in the controller:
User::with('post')->get();
It will get every user's post from the database, based on users.id.
But the problem is, I want to do this:
User::with(['post' => function($query) {
# Throw users.id here...
}])->get();
How to do that?
You should get the users first, and then load related posts with a separate query and merge them manually.
$users = User::get();
$posts = Post::whereIn('user_id', $users->pluck('id'))->get(); // Get your additional data in this query
$users->each(function ($user) use ($posts)
{
$user->posts = $posts->where('user_id', $user->id);
});
Note: I did not test the code above. It's just an example to show you how to accomplish what you are trying to do.

Use Eloquent to attach array to query

I have a USER table, an ITEMS table, and a LIKES table. I'm randomly taking 9 items from the Items table (which is named 'categories') and I want to get an array of users which liked that item. I'm returning a JSON response so I can't use laravel's ORM relationships as far as I know. I want to be able to look through the results (with javascript) like so:
foreach item
item->price
....
foreach user
(this is how i wish to look through the users with js)
endforeach
endforeach
{{--Im trying to get an output that looks like so--}}
0: {
cost: 409
views: 0
...
user_like_id: {1,5,2,4,5}
}
EDIT: This is what I would like to attach the array to...
$likes = Category::orderByRaw("RAND()")
->leftJoin('likes', 'likes.item_id', '=', 'categories.id')
->take(9)
->get();
I'm relatively new to programming so please dont downnrate this question
Laravel provides in ways to make json from objects. The first and simplest is the eloquent toJson() method:
$likes = Category::orderByRaw("RAND()")
->leftJoin('likes', 'likes.item_id', '=', 'categories.id')
->take(9)
->get()
->toJson();
$likes is now json.
Second method:
If you return a model from a controller or a routecallback function, laravel returns it as json. So:
class MyController extends Controller{
public function getIndex(){
return Category::orderByRaw("RAND()")
->leftJoin('likes', 'likes.item_id', '=', 'categories.id')
->take(9)
->get();
}
}
This example returns a json response which is quite usefull!
I actually got exactly what I wanted with this:
$likes = Category::orderByRaw("RAND()")
->take(9)
->with(array('getLikes' => function($query)
{
$query->with('getUser');
}))
->get();
Works exactly how I want ito :) thanks for the suggestion tho

Resources