Call to a member function latest() on string in laravel - laravel

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

Related

Or with And Statement in Eloquent livewire

I am using Laravel Livewire dataTables and while searching I am getting an error.
Object of class Illuminate\Database\Eloquent\Builder could not be
converted to string
my method for searching is
public static function search($QUERY)
{
return empty($QUERY) ? static::QUERY()
: static::WHERE(function ($QUERY) {
$QUERY->WHERE('name', 'like', '%' . $QUERY . '%')
->orWhere('email', 'like', '%' . $QUERY . '%');
});
}
Livewire render method
public function render()
{
//$this->roles = ROLE::WHERE('company_id', SESSION('company_id'))->paginate(5);
return VIEW('livewire.users', [
'users' => USER::search($this->search)
->WITH(['role', 'company'])
->WHERE('company_id', SESSION('company_id'))
->orderBy($this->sortField, $this->sortAsc ? 'asc' : 'desc')
->paginate($this->perPage),
]);
}
Instead of having the actual method search(), you can make a scope, which will be rendered the same, but will also have some Laravel-magic helping you.
Replace your search() method on your User model with this scopeSearch() method. You do not change the query where its being used.
/**
* Scope a query to search for users name and email
*
* #param \Illuminate\Database\Eloquent\Builder $query
* #return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSearch(Builder $query, string $search)
{
return $query->where('name', 'like', '%'.$search.'%')
->orWhere('email', 'like', '%'.$search.'%');
}
And import that Builder class, by adding the following line to the top of your User model class,
use Illuminate\Database\Eloquent\Builder;
The trick here is the scope prefix, with a capital S in "search". See the official documentation for more details.

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 :search form where '<='

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]);
}

Laravel Search Relations whereHas

I am trying to do a search on products but it does not return any results. I suspect it has something to do with the whereHas function but I just cannot figure it out. Here is my code:
My Relations:
Categories Model:
class Categories extends Eloquent {
public function product()
{
return $this->hasMany('Products')->orderBy('name');
}
}
Products Model:
class Products extends Eloquent {
public function category()
{
return $this->belongsTo('Category')->orderBy('name');
}
}
My search function:
public function search()
{
$search = Input::get('search');
$result = Products::where('name', 'LIKE', '%'. $search .'%')->get();
if ($result->first()) {
return View::make('groceries.index')
->with('categories', Categories::with('product')->orderBy('name', 'asc')
->whereHas('product', function($query) use ($search)
{
$query->where('name', 'LIKE', '%'.$search.'%');
}));
}
}
In the view:
#foreach ($categories as $category)
//do stuff
#foreach ($category->product as $products)
//Show results
#endforeach
#endforeach
You have to call get() to get the data.
And I think this will be more readable code.
public function search()
{
$search = Input::get('search');
$result = Products::where('name', 'LIKE', '%'. $search .'%')->get();
if ($result->first()) {
$categories = Categories::with('product')->orderBy('name', 'asc')
->whereHas('product', function($query) use ($search){
$query->where('name', 'LIKE', '%'.$search.'%');
})->get();
return View::make('groceries.index')
->with('categories', $categories);
}
}

Resources