So, i have a Builder instance of "products" with Product::orderBy('purchase_date', 'desc') , and each of my products have a 'client_id' field, now i just want to get all products where the client name is like something, how can i do it?
That is the (ugly) way i implemented it, is this correct or there is a better way to do it?
// Here i get all products
$products = Product::orderBy('purchase_date', 'desc');
// Here i get all id of the clients, remove duplicates with "unique" and sort.
$clients = $products->pluck('client_id')->unique()->sort();
// Now i find all clients passing a array of IDs
$clients = Client::find($clients);
// Here i create a new collection of Clients
$clients = (new Client)->fill($clients->toArray());
// The method getBySearch basically return a query search
// like this: $clients->where($table, 'like', '%' . $query['search'] . '%');
// where i get all clients that matches the search
$clients = $this->getBySearch($request->query(), $clients);
// Now i return all products with the client ids
$products = Product::where('client_id', $clients->pluck('id'));
So, this code gives me all products of a client that have, for example, 'Lu' in his name. There is a better way to do that?