everybody, My code here is returning tasks that have more than 1 Tag and task tags are in $TagArray everything is working good in
return $TaskData;
but When I pass TaskData into view I get different results even get Tasks with one tag
return view ('task', compact('TaskData'));
My Code
$TaskData= Path::with(['pathtags' => function ($q) use ($TagArray) {
$q->with(['Tasks' => function($q) use ($TagArray) {
$q->has('tasktags', '=' , 2)->whereDoesntHave('tasktags',
function ($query) use ($TagArray) {
$query->whereNotIn('name', $TagArray);
}
)->with('tasktags');
}]);
}])->first();
Related
Just wondering where to position the Laravel's pagination() here in my query() in order for it to work. I tried adding it at the end of $all_procedures:
public function fetchprocedure(Request $search_procedure) {
$filter_procedure = $search_procedure->input('search_procedure');
$all_procedures = HmsBbrKnowledgebaseProcedure::query()
->when(is_null($filter_procedure), function ($query) {
$query->where('bbr_procedure_id', '>=', 1);
})
//iLike is case insensitive
->when(!empty($filter_procedure), function ($query) use ($filter_procedure) {
$query->where('procedure_name', 'iLIKE', "%$filter_procedure%");
})
->orderBy('bbr_procedure_id', 'ASC')
->get()
->paginate(10);
return response()->json([
'all_procedures' => $all_procedures,
]);
}
UPDATE:
tried a solution given by an answer. Got rid of get(); but my error this time is:
Just instead of having:
->get()
->paginate(10);
Do
->paginate(10);
That will automatically get 10 results and paginate it taking into account the current page.
I have a function that is supposed to use a combination of filters set by the user to fetch information from a table (or no filters at all).
Keep in mind I'm not trying to override filters here, the query should dynamically add or remove filters depending on the ones set client-side by the user.
The filters work well individually, but when setting multiple of them, the query doesn't fetch the correct data at all. I'm still unable to figure out what the criteria for the incorrect data being fetched is.
The only filters that seem to work in combination with others are date_from and date_to.
This is the function:
public function getReport(Request $request)
{
// Convertir fechas a formato correcto
$request->date_from = date('Y-m-d', strtotime(str_replace('-', '/', $request->date_from)));
$request->date_to = date('Y-m-d', strtotime(str_replace('-', '/', $request->date_to)));
$query = Ticket::all();
$query->when(isset($request->date_from), function ($q) use ($request) {
return $q->where('date_created', '>=', $request->date_from);
});
$query->when(isset($request->date_to), function ($q) use ($request) {
return $q->where('date_created', '<=', $request->date_to);
});
$query->when(isset($request->user), function ($q) use ($request) {
return $q->where('user', $request->user);
});
$query->when(isset($request->paid), function ($q) use ($request) {
if ($request->paid === true) {
return $q->whereNotNull('paid');
} else {
return $q->whereNull('paid');
}
});
$query->when(isset($request->informed), function ($q) use ($request) {
return $q->where('informed', $request->informed);
});
$query->when(isset($request->endorsement), function ($q) use ($request) {
if ($request->endorsement === true) {
$query->where('policy', '<', 1);
} else {
$query->where('policy', '>', 1);
}
});
$query->when(isset($request->duplicate), function ($q) use ($request) {
return $q->where('duplicate', $request->duplicate);
});
$query->when(isset($request->active), function ($q) use ($request) {
return $q->where('active', $request->active);
});
return response()->json($query, $this->success_status);
}
I've tested the $request payload multiple times and there's no issues with the information coming client-side.
What am I doing wrong?
You can try given solution for you problem.
$query = Ticket::all();
if ($request->filled('user')) {
$user = $request->get('user');
$query->where(function ($query) use ($user) {
$query->where('user', $user);
});
}
Note: if it is working perfect then you can add all request parameter according.
First note:
$query = Ticket::all();
In this case $query is not Eloquent query but Laravel Collection.
Another note:
$request is not regular php object with properties, so you have to use ->has(), ->filled() instead of empty() or isset()
Your ->data_to and ->data_from are working, cause you have overwritten those values in the beginning, so they become regular object properties.
Also, I would simplify some parts of your code:
$query->when(isset($request->duplicate), function ($q) use ($request) {
return $q->where('duplicate', $request->duplicate);
});
I would use this construction instead to save some lines:
$query->where('duplicate', $request->get('duplicate', false));
Second param of ->get is default value. If it is not present in request you will get false anyway. So you don't have to care about param existence.
You can apply same logic to all other filters as well.
PS: I don't know your requirements, but this part seems suspicious to me:
$query->when(isset($request->endorsement), function ($q) use ($request) {
if ($request->endorsement === true) {
$query->where('policy', '<', 1);
} else {
$query->where('policy', '>', 1);
}
});
This code will never give 'policy' === 1;
I'm trying to filter products based on query string. My goal is to get products from a collection if it's given, otherwise get every product. Could someone help me what's wrong with the following code?
$products = \App\Product::where([
'collection' => (request()->has('collection')) ? request('collection') : '[a-z]+',
'type' => (request()->has('type')) ? request('type') : '[a-z]+'
])->get();
PS.: I've also tried with 'regex:/[a-z]+', it's not working...
$products = \App\Product::where(['collection' => (request()->has('collection')) ? request('collection') : 'regex:/[a-z]+'])->get();
What you can do is use when eloquent clause, so your where clause for collections will be triggered only when the request('collection') exists, same logis applie to type as well.
$products = \App\Product::
when(request()->has('collection'), function ($q) {
return $q->where('collection', request('collection'));
});
->when(request()->has('type'), function ($q) {
return $q->where('type', request('type'));
})
->get();
Or another way if you have your request values assigned to a variable something like:
$collection = request('collection');
$type= request('type');
$products = \App\Product::
when(!empty($collection), function ($q) use ($collection) {
return $q->where('collection', $collection);
});
->when(!empty($type), function ($q) use ($type) {
return $q->where('type', $type);
})
->get();
I am trying to create the filters and I just want to attach the query to relation by using some conditionals. I don't want to put those conditional in the first block of the code. So how can I get the query instance so I can attach the new queries to the existing relation?
$query = Category::query();
$query->where('category_type', 'xyz')
->with(['products' => function ($query) {
$query->where('condition1', 'value')
->where('condition2', 'value');
}]);
if (isset($queryParams['param1'])) {
$query->with(['products' => function ($query) use ($queryParams) {
$query->getQuery()->where('type', $queryParams['param1']);
}]);
}
Currently, it just overwrites the first query relation condition.
This should work :
$query = Category::query();
$query->where('category_type', 'xyz')
->with(['products' => function ($query) use ($queryParams) {
$query->where('condition1', 'value')
->where('condition2', 'value');
if (isset($queryParams['param1'])) {
$query->where('type', $queryParams['param1']);
}
}]);
So I'm making a fliter in laravel for a project and me and my teacher are braking our head on a the following thing.
In the code below the general search for a player works but the other statements won't add to it if they are defined and in the POST request;
Controller:
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class FilterController extends Controller
{
public function filter(Request $request)
{
$player = new \App\Player;
$filters = $player->newQuery();
$query = Input::get('q');
// Search for a player based on their status.
if ($request->has('status')) {
$filters->orwhere('status', $request->input('status'));
}
// Search for a player player on their club.
if ($request->has('club')) {
$filters->orwhere('Club', $request->input('club'));
}
// Search for a player player on their team category .
if ($request->has('Category')) {
$filters->orwhere('Category', $request->input('Category'));
}
// Search for a player player if he is flagged as removed.
if ($request->has('remove')) {
$filters->orwhere('remove', $request->input('remove'));
}
// Search for a player player on their size.
if ($request->has('size')) {
$filters->orwhere('Size', $request->input('size'));
}
// General search for a player
if($request->has('q')){
$filters->orwhere('first_name','LIKE','%'.$query.'%')
->orWhere('last_name','LIKE','%'.$query.'%')
->orWhere('mobile','LIKE','%'.$query.'%')
->orWhere('street_name_nummer','LIKE','%'.$query.'%')
->orWhere('city','LIKE','%'.$query.'%');
}
// Get the results and return them.
$results = $filters->get();
if(count($results) > 0){
return view('lists/ekick')->withDetails($results,$query);
} else return view ('lists/ekick')->with('No Details found. Try to search again !');
}
}
route:
Route::any('lists/ekick', 'FilterController#filter');
output view:
img from view
To pick up on Aaron Sarays answer, you are most likely filtering the wrong way. Normally additional filters are additional conditions each record has to meet in order to be part of the result. If you consider an Excel table and you filter one column, you won't even have all options to filter for in the second column because you already limited the result and you can only limit it further.
Combine this knowledge with an improved way of filtering and you receive a query like this:
public function filter(Request $request)
{
$query = $request->input('q');
$results = \App\Player::query()
->when($request->input('status'), function ($query, $status) {
$query->where('status', $status);
})
->when($request->input('club'), function ($query, $club) {
$query->where('club', $club);
})
->when($request->input('category'), function ($query, $category) {
$query->where('category', $category);
})
->when($request->input('remove'), function ($query, $remove) {
$query->where('remove', $remove);
})
->when($request->input('size'), function ($query, $size) {
$query->where('size', $size);
})
->when($query, function ($query, $q) {
$query->where(function ($query) use ($q) {
$query->where('first_name', 'LIKE', "%$q%")
->orWhere('last_name', 'LIKE', "%$q%")
->orWhere('mobile', 'LIKE', "%$q%")
->orWhere('street_name_number', 'LIKE', "%$q%")
->orWhere('city', 'LIKE', "%$q%");
});
})
->get();
if ($results->isNotEmpty()) {
return view('lists/ekick')->withDetails($results, $query);
} else {
return view ('lists/ekick')->with('No Details found. Try to search again !');
}
}
The function when($condition, $callback) as used in the query above is used to dynamically build queries. You can consider the following two statements equivalent:
// option 1: conditional query (preferred!)
$results = Player::query()
->when($request->input('q'), function ($query, $q) {
$query->where('name', 'LIKE', "%$q%");
})
->get();
// option 2: plain php query building... (not very clean code)
$query = Player::query();
if ($request->input('q')) {
$query->where('name', 'LIKE', '%'.$request->input('q').'%');
}
$results = $query->get();
In order to do what you're doing, I think you want to not use or with your queries. You're basically saying
Give me the Player where status is something or size is something
I think what you mean to say is
Give me the Player where status is something and size is something
Depending on if the requirements exist or not in the filter.
So, you'd want to alter your code using the following as an example:
if ($request->has('status')) {
$filters->where('status', $request->input('status'));
}
// Search for a player player on their club.
if ($request->has('club')) {
$filters->where('Club', $request->input('club'));
}
You can also bypass one step by using this:
$query = \App\Player::getQuery();