Can I use the function where() or some equivalent after the model has already been retrieved? For example:
$head = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)
->whereHas('item', function ($q) {
$q->where('type_id', 1);
})->where('wear', 1)->first();
$torso = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->where('type_id', 2);
})->where('wear', 1)->first();
if ($torso == null)
$torso = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->where('type_id', 3);
})->where('wear', 1)->first();
$hands = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->where('type_id', 4);
})->where('wear', 1)->first();
$waist = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->where('type_id', 5);
})->where('wear', 1)->first();
$feet = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->where('type_id', 6);
})->where('wear', 1)->first();
$amulet = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->where('type_id', 8);
})->where('wear', 1)->first();
$first_ring = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->where('type_id', 9);
})->where('wear', 1)->first();
$secound_ring = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->where('type_id', 9);
})->where('wear', 2)->first();
$right_hand = Equipment::where('user_id', $fighter->id)->where('lvl_after_reduction', '<=', $fighter->attributes->lvl)->where('durability', '>', 0)->whereHas('item', function ($q) {
$q->whereIn('type_id', [10, 11]);
})->where('wear', 1)->first();
Can I combine these queries into one? Because I have a lot of queries to database per page.
Sorry for my english.
Please combine your where clause in one function like this
$torso = Equipment::where([['user_id', $fighter->id], ['wear', 1]])->first();
It is much cleaner.
Yes it is possible to use where
function after running a query as long as it returns collection.
eg You can do
Equipment::all();
Then apply where
clause to the collection it returns. Query like this return an eloquent collection
. Go to this documentation page https://laravel.com/docs/5.5/eloquent-collections to see all the functions available for eloquent collection.
However using where
clause to return value of query that end with first()
is not possible as it does not return a collection.