when I use 'like' it works , but whene I use '<=' it dosen'T WORK
public function search(Request $request)
{
//
$adultes=$request->get('adultes');
$dispos= Disponible::where('adultes', '<=','%'.$adultes. '%' )
->paginate(5);
return view('rooms_dispo',['dispos'=>$dispos]);
}
public function search(Request $request)
{
$adultes = $request->get('adultes');
$dispos = Disponible::where('adultes', '<=', $adultes)->paginate(5);
return view('rooms_dispo', ['dispos' => $dispos]);
}
Related
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. '%');
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
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'));
}
I have this product model:
public function categories() {
return $this->belongsToMany('App\Category', 'product_categories');
}
And in my controller:
public function search(Request $request) {
return Product::with([
'categories' => function($categories) use ($request) {
// ...
}
]);
}
If I try to use $request in the categories function it's search only in categories, but shows all products.
How do I show only that products whitch has defined categories in $request->category_id?
you may use the whereHas keyword in laravel:
public function search(Request $request) {
return Product::with('categories')
->whereHas('categories', function ($query) use ($request){
$query->where('category_id', $request->category_id);
})->get();
}
Here is the docs
You can search it in following way:
public function search(Request $request) {
return Product::with('categories')
->whereHas('categories', function ($q) use (request) {
$q->where('id', $request->category_id);
});
}
What is this error.
Call to a member function latest() on string
Controller
public function search()
{
$keyword = request('search');
$articles = Article::search($keyword)->latest()->get();
return $articles;
}
Model
public function scopeSearch($query , $keyword)
{
$query->where('title', 'LIKE', "%".$keyword."%");
return $keyword;
}
You shouldn't return a string from the local scope. So, change it to:
public function scopeSearch($query , $keyword)
{
return $query->where('title', 'like', '%' . $keyword . '%');
}