why orWhere Operator not working in laravel - laravel

In my code i want to search by product_name and merchant_name but when i use orWhere operator i get this error
BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::orWhere does not exist. in file /Users/admin/Desktop/GSM Source Code/API/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php on line 113
public function search($name)
{
$brand = Brand::join('product_categories','product_categories.id','=','brands.category')
->join('products','products.id','=','brands.product')
->join('merchants','merchants.id','=','brands.merchant')
->join('new_shops','new_shops.id','=','brands.shop')
->get([
'brands.id',
'brands.image',
'product_categories.category',
'products.product_name',
'merchants.merchant_name',
'new_shops.shop_name',
'brands.activation',
'brands.created_at',
'brands.updated_at'
])->where('product_name',$name)
->orWhere('merchant_name',$name);
return new BrandResource($brand);
}
Also when i use LIKE operator in the where clause it doesnot not give any result

you must use where and orWhere after get, like this:
$brand = Brand::join('product_categories','product_categories.id','=','brands.category')
->join('products','products.id','=','brands.product')
->join('merchants','merchants.id','=','brands.merchant')
->join('new_shops','new_shops.id','=','brands.shop')
->where('product_name',$name)
->orWhere('merchant_name',$name)
->get();

orWhere is a QueryBuilder method. If you use get you are returning a collection which means you cannot use it. Try putting them before the get and should work well

Related

Method Illuminate\Database\Eloquent\Collection::orderby does not exist

$posts = Post::all()->orderby('created_at','desc')->where('usr_id','=',session('LoggedUser'))->get();
return view('admin.profile',compact('userInfo' , 'posts'));
i am making a custom auth for a journal activity but i cant sort the content i shows this error
"Method Illuminate\Database\Eloquent\Collection::orderby does not exist. "
$posts = Post::where('usr_id','=',session('LoggedUser'))->orderby('created_at','desc')->get();
True query like that. When you take all() already query done.
Change it to:
$posts = Post::where('usr_id','=',session('LoggedUser'))->orderby('created_at','desc')->get();
you cant use all() and orderBy because all() does not allow the modification of the query.
I believe this might be because you typed orderby instead of orderBy (notice the uppercase). See laravel orderBy documentation if needed.
Plus, as mentionned by other, don't use all() if you need to do other thing (where clause, order by, etc) in you query.
Change the orderby to orderBy. This could be the reason you are getting the error.
$posts = Post::all()->orderBy('created_at', 'DESC')->where('usr_id','=',session('LoggedUser'))->get();
return view('admin.profile',compact('userInfo' , 'posts'));
Or...
If you want to get specific number of posts you can do it this way to avoid using the Post::all
$posts = Post::orderBy('created_at', 'DESC')->where('usr_id','=',session('LoggedUser'))->paginate(5);
return view('admin.profile',compact('userInfo' , 'posts'));
Yeah this is pretty confusing and just got me as well.
The actual problem isn't the capitilization typo (orderby versus orderBy) but rather the fact that you're using ->all() instead of just Model::orderBy()->...
The moment you use ->all() the object is transformed to another type of collection object and the normal methods one would expect do not exist.
In this case you should rather use sortBy().
See here.

when to use get() in query laravel 5

I have a basic query set up in the show method of a laravel resource
public function show($id){
$results = Student::find($id);
$drives= Drive:: where('student_id', $id);
}
The query for $results works perfectly. The query for $drives does not work unless I do ->get() at the end of it. Why is this? what's the difference between the two queries so that one requires the ->get() and the other does not? Solving this problem took me like 5 hrs and i'm just curious as to the functionality behind it so i can avoid this headache in the future.
Some eloquent expressions have a get implicitly. Those ones who are made by a Query Builder will need a ->get() call, find(), findOne()... won't need a get().
https://laravel.com/docs/5.6/eloquent#retrieving-models
https://laravel.com/docs/5.6/queries
use get to execute a builder query. unless you run the get() query wont be executed. get will return a collection.
1 - Use query builder to build queries however you want.
$drives= Drive:: where('student_id', $id);
dd($drives); // will return a query builder, you can use it to build query by chaining
2 - when you are ready to execute the query call get()
$drives= Drive:: where('student_id', $id);
$result = $drives->get()
dd($result); // will return a database query result set as a collection object
If you want to get a single object by id use find, to get a single object
$results = Student::find($id);
dd($result); will return a single model
Using the function find() on a model gets a query result based on the primary key of the model, id in this case.
When using where(), it gets a collection (an object of all query results), so if you only want the first result you must call $drives=Drive::where('student_id', $id)->first();
Here is a more in-depth explanation: the difference of find and get in Eloquent

Laravel i will get integer in DB query

after returning $query from this:
$query = DB::table('pets')->select('id')->where('id', '=', $pet->id)->where('user_id', '=', Auth::id())->get();
for example i get this result : [{"id":"66"}]
how can i get only 66 as integer?
Thanks!
Instead of get() which returns the entire collection of selected data, use value('id') to get the first value of the id field. You also wouldn't need select('id') if you use this method.

laravel where Clauses on related eloquent model

Hey why i cant use a where clause with 3 params on related eloquent models??
simple example (i want it for a <=)
$user->roles->where('active',1);
//is working
$user->roles->where('active','=',1);
//is not working
Can i use it with 3 params only in:
DB::table('users')->where('votes', '=', 100)->get();
and not in:
$xy->users->where('votes', '=', 100);
Thanks,
$user->roles is returning a Collection, which has a method where on it that accepts exactly two parameters:
$user->roles->where("key", "value");
As seen in the documentation: https://laravel.com/docs/5.4/collections#method-where
If you want to use three, you'll need to return a Query Builder instance:
$user->roles()->where("key", "operator", "value")->get();

How to add dynamic 'where' to eloquent ORM with() relation

I'm, trying to do something like this:
$verfuegbarkeiten = Verfuegbarkeit::verfuegbar()->with('product', 'producer', 'producer.user', 'producer.user.ort')->where('product.kategorie_id', '=', $_GET['kat'])->get()->toArray();
I want to use a 'where' clause so i can only get the results where the 'product's attribute 'kategorie_id' matches the GET parameter. How can I 'where' on a relation? It's really important to me that this value can be dynamic, so writing a fix function in the related Model won't be a good solution.
Every hint appreciated
You want whereHas(), documentation here: https://laravel.com/docs/5.2/eloquent-relationships#querying-relations
For your example, that would mean something like this:
$kat = $_GET['kat'];
$verfuegbarkeiten = Verfuegbarkeit::verfuegbar()
->with('product', 'producer', 'producer.user', 'producer.user.ort')
->whereHas('product', function($query) use ($kat) {
$query->where('kat', $kat);
})
->get()
->toArray();
Although I might add that using $_GET is generally discouraged in Laravel.

Resources