Request in Laravel for my function index() - laravel

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

Related

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()

orWhereHas not existing laravel Eloquent php 7.4

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()"

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. '%');

Need to adapt my function index() in Laravel

I need to adapt a function but I am stuck...
My function index() is the following: (It works, but I have to change)
public function index(Request $req)
{
if ($req->search == "") {
$formers = Former::paginate(5);
$formersIdsDown = Course::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();
return view('admin.formers.index', compact('formers', 'formersIdsDown'));
} else {
$validated = $req->validate([
'search' => 'alpha',
]);
$formers = Former::where('nom', 'LIKE', '%' . $validated['search'] . '%')->paginate(5);
$formers->appends($req->only('search'));
}
}
My problem is that I don't know how to adapt this line below on my new function;
formersIdsDown = Course::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();
return view('admin.formers.index', compact('formers', 'formersIdsDown'));
Here is, my new code...
public function index(Request $request)
{
$user = $request->user();
$formers = Former::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('nom', 'like', '%'.$request->input('search').'%');
})
->paginate(5);
return view('admin.formers.index', compact('formers'))
->with('display_search', $user->hasRole('admin'));
}
I adapted, except this line below... Do know you where I have to integrate?
formersIdsDown = Course::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();
return view('admin.formers.index', compact('formers', 'formersIdsDown'));
Thank you
It's OK
public function index(Request $request)
{
$user = $request->user();
$formers = Former::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('nom', 'like', '%'.$request->input('search').'%');
})
->paginate(5);
$formersIdsDown = Course::where('date_seance', "<=" , Carbon::now())->pluck('fk_former')->toArray();
return view('admin.formers.index', compact('formers', 'formersIdsDown'))
->with('display_search', $user->hasRole('admin'));
}

Get var from controller to model

I got one variable in controller ($id), I want to pass it to the model:
CONTROLLER:
public function user_load_more($id, $friendly_url)
{
$user = User::where('friendly_url', '=', $friendly_url)
->with('shares_load_more.links.tag', 'userProfile.get_following')
->with(['shares_load_more.links.page' => function ($query) {
$query->select('id', 'name', 'friendly_url');
}])->orderBy('id', 'desc')->first();
return view("site.list.user.links", compact("user"));
}
MODEL (User.php): (?)
public function shares_load_more($id) //--- put the id here?
{
return $this->hasMany(Share::class, 'user_id', 'id')
->select('id', 'link_id', 'user_id', 'shared_in', 'content', 'created_at')
->take(2)
->orderBy('id', 'desc')
->where('id', '<', $id)
->where('type', '=', 0);
}
You don't really need to pass variable in a model, there is an alternate method to do it.
public function user_load_more($id, $friendly_url)
{
$user = User::where('friendly_url', '=', $friendly_url)
->with('shares_load_more.links.tag', 'userProfile.get_following')
->with(['shares_load_more' => function($query) use($id) {
$query->where('id', '<', $id);
}, 'shares_load_more.links.page' => function ($query) {
$query->select('id', 'name', 'friendly_url');
}])->orderBy('id', 'desc')->first();
return view("site.list.user.links", compact("user"));
}
Model:
public function shares_load_more($id) //--- put the id here?
{
return $this->hasMany(Share::class, 'user_id', 'id')
->select('id', 'link_id', 'user_id', 'shared_in', 'content', 'created_at')
->take(2)
->orderBy('id', 'desc')
->where('type', '=', 0);
}
It will append to the relation query

Resources