orWhereHas not existing laravel Eloquent php 7.4 - laravel

Here is my Macro for relation search in Laravel Eloquent.
Bellow is my Macro in AppServiceProvider.php
Builder::macro('whereLike', function ($attributes, string $searchTerm) {
$this->where(function (Builder $query) use ($attributes, $searchTerm) {
foreach (Arr::wrap($attributes) as $attribute) {
$query->when(
str_contains($attribute, '.'),
function (Builder $query) use ($attribute, $searchTerm) {
[$relationName, $relationAttribute] = explode('.', $attribute);
$query->orWhereHas($relationName, function (Builder $query) use
($relationAttribute, $searchTerm) {
$query->where($relationAttribute, 'LIKE', "%{$searchTerm}%");
});
},
function (Builder $query) use ($attribute, $searchTerm) {
$query->orWhere($attribute, 'LIKE', "%{$searchTerm}%");
}
);
}
});
return $this;
});
Bellow is my search Query in MemberController.php
public function getMembers(request $request){
$members = Member::with('products')->where('groupId', 1)->whereLike(['firstname',
'lastname', 'products.name'], $request->searchValue)->paginate(10);
dd($members);
}
The "whereLike" is invoked from the appServiceProvider.php as a macro in there thus where the error occur if i remove the members.name the macro executes perfectly.
I get an Error with orWhereHas
"Call to undefined method Illuminate\Database\Query\Builder::orWhereHas()"

Related

Pagination links is not show when using paginate in laravel

So I have some problem in here, the links is not shown on my output data, I have try made query like this :
$getData = Inventory::select('id_inventory', 'inventory_code', 'inventory_id_location', 'inventory_id_category', 'date_of_buy', 'price_of_item', 'condition_of_item', 'description', 'created_by', 'edited_by')
->selectRaw('group_concat(inventory_id_category) as inventory_id_categories')
->selectRaw('group_concat(id_inventory) as id_inventories')
->with(['categories' => function ($query) {
$query->select('id_category', 'category_code', 'category_name');
}])
->with(['locations' => function ($query) {
$query->select('id_location', 'location_name');
}])
->with(['delivery_inventories' => function ($query) {
$query->select('id_delivery', 'delivery_id_inventory', 'delivery_id_location', 'delivery_date', 'deliver_by', 'description')
->with(['location_deliveries' => function ($query) {
$query->select('id_location', 'location_name');
}]);
}])
->whereHas('categories', function ($query) use ($search) {
$query->orWhere('category_code', 'like', "%{$search}%")
->orWhere('category_name', 'like', "%{$search}%");
})
->whereHas('locations', function ($query) use ($search) {
$query->orWhere('location_name', 'like', "%{$search}%");
})
->whereHas('delivery_inventories', function ($query) use ($search) {
$query->orWhere('delivery_date', 'like', "%{$search}%")
->orWhere('deliver_by', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%");
})
->where('inventory_code', 'like', "%{$search}%")
->orWhere('price_of_item', 'like', "%{$search}%")
->orWhere('condition_of_item', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->groupBy('inventory_code')
->orderBy('inventory_code')
->paginate(10);
return response()->json([
'get_data' => $getData
], 200);
but on my console the result like this :
I wanna ask any wrong or something uncompleted query so that's my links doesn't show up on my console? Any suggest? Thank you.
my project before (but I can't use that in my current project) :
Did you update your AppServiceProvider?
if not go to app/providers/AppServiceProvider.php
and add paginator in there.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Pagination\Paginator;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Paginator::useBootstrap();
}
}

Laravel where of Relationship from Relationship

I have easy relationships like this
Project Model:
public function milestones()
{
return $this->hasMany('App\Models\Milestone');
}
Milestone Model:
public function milestones()
{
return $this->hasMany('App\Models\Milestone');
}
Now I try to get Todo with id = 3. And it should only show Milestone where my Todo is inside. This is my solution at the moment (shows all Milestones inside project)
$query = Project::whereHas('milestones',function($query) use ($UserId){
$query->whereHas('todo', function ($query) use ($UserId){
$query->where('id',3);
});
})->with('milestones.todo',function($query){
$query->where('id',3);
})
->get();
How do I limit milestones to that one, where todo with id 3 is inside?
You'll need to be explicit with your eager load:
$query = Project
::with('milestones', function ($query) use ($UserId) {
$query
->with([
'todos' => function ($query) use ($UserId) {
$query->where('id', $UserId);
},
])
->whereHas('todos', function ($query) use ($UserId) {
$query->where('id', $UserId);
});
})
->whereHas('milestones', function ($query) use ($UserId) {
$query->whereHas('todo', function ($query) use ($UserId) {
$query->where('id', $UserId);
});
})
->get()

Hasrole with 2 tables

I have a model named Student with the field name , here is the function, which is correct.
public function index(Request $request)
{
$user = $request->user();
$students = Student::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->where('name', 'like', '%'.$request->input('search').'%');
})
->paginate(5);
return view('admin.students.index', compact('students'))
->with('display_search', $user->hasRole('admin'));
}
Now, in my model Payment I have a request with 2 tables (Payment & Student).
Here is my function index()
public function index(Request $req)
{
if ($req->search == "") {
$payments = Payment::paginate(5);
return view('admin.payments.index', compact('payments'));
} else {
$validated = $req->validate([
'search' => 'alpha',
]);
$payments = payment::whereHas('students', function($query) use($req) {
$query->where('name', 'like', '%' . $req->search . '%');
})->paginate(5);
return view('admin.payments.index', compact('payments'));
}
}
My problem is I want to adapt my function for connect me with an user.
I have this for now ???
public function index(Request $request)
{
$user = $request->user();
$payments = Payment::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
how to include this:
$payments = payment :: whereHas ('students', function ($ query) use ($ req) {
$query-> where ('name', 'like', '%'. $ req-> search. '%');

Request in Laravel for my function index()

I have a problem with my function index() concerning a request. In fact, I have to adapt my old function with a recent.
For information, my function index() (old) below was written like this and it works !!
public function index(Request $req)
{
if ($req->search == "") {
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
return view('admin.retours.index', compact('retours'));
} else {
$validated = $req->validate([
'search' => 'alpha',
]);
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->where('eleves.nom','like', '%' . $req->search . '%')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
return view('admin.retours.index', compact('retours'));
}
}
Now, I have to change a party of code and I must to adapt my request:
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->where('eleves.nom','like', '%' . $req->search . '%')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
Here is an idea of my new code, my problem is that the search bar doesn't filter ? ^^
public function index(Request $request)
{
$user = $request->user();
$retours = Retour::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*');
})
->paginate(5);
return view('admin.retours.index', compact('retours'))
->with('display_search', $user->hasRole('admin'));
}
My problem is perhaps here ?
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*');
})
->paginate(5);
Do have you an idea please?
Thank you

Laravel - Undefined variable: request

My http://localhost:8888/VGL/public/category/18?sty=3
When dd($request->sty); equal 3
however I put $request->sty in the whereHas is
Undefined variable: request
public function show(Request $request, $id)
{
$products = Product::with('features')
->whereHas('features', function ($q) {
return $q->where('id', $request->sty);
})
->where('category_id',17)
->get();
}
Try this
If you want to use any variable inside the where closure then you have to pass that variable inside the use($variable)
public function show(Request $request, $id)
{
$products = Product::with('features')
->whereHas('features', function ($q) use($request) {
return $q->where('id', $request->sty);
})
->where('category_id',17)
->get();
}

Resources