I can take the list using
$specialities = Speciality::pluck('name','id')
Why isn't the following code working? What could be an alternative?
I am returning this array by ajax to form a select box. So I thought pluck (list in laravel 4+) would be the right choice.
$specialities = Speciality::pluck('name','id')->where('role_id',$request->roleid);
I found the mistake. I should use pluck with where condition like below.
$specialities = Speciality::where('role_id',$request->roleid)->pluck('name','id');
Pluck won't filter anything, but it gives only what needed. So filtering has to be done before that.
Related
$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.
I have added the condition like this #if(!empty($param)) but not working.
assuming $param is a collection here, you can use isEmpty() or isNotEmpty() method like this:
#if($param->isNotEmpty())
Or
#unless($param->isEmpty())
You can use isempty method of laravel collection to check whether a collection is empty or not. For more detail you can check it in their document.
Laravel Collection: isempty Method
You can use the count() method and do something like this :
#if($collection->count())
More details here.
This also works:
#if(count($param) > 0)
I have some properties and i want to get the object for each property, currently, I am using the eloquent in the foreach loop like this as shown in the image that will describe the best..
but it is really not a good approach because if I have 100 published property I will making 100 calls to the DB... and that is not correct?
I need a suggestion and a proper solution to this query?
Thanks in advance
Before foreach you can get all the RentalProperty items from db like this:
$allRentalProperties = RentalProperty::all();
and in foreach loop you can get those items without connecting database like this:
$propertyObj = $allRenatalProperties -> where('id', $property['id']) -> first();
Also, you can use array shorthand.
$newArray = [];
it's much simple and readable.
You can do array pluck before loop:
$propertyIds = Arr::pluck($published_properties, 'id');
Then you can make a whereIn query to get only those data which are in $published_properties object
$propertyObj = RentalProperty::whereIn('id', $propertyIds);
Then you can access that object with id if you change array key with record id.
How I can query using Laravel Eloquent the following:
->where('items.type',"shirt")
->where('items.type',"glass")
I need to get the items with the type of shirt and glass
The above works with 1 Where on the same column. If I place the 2nd Where (glass), nothing is returned.
My desired query, as SQL:
WHERE items.type == "shirt" || items.type == "gass"
Use the orWhere method.
->where('items.type', 'shirt')->orWhere('items.type', 'glass')
It's documented on https://laravel.com/docs/5.2/queries#where-clauses.
You may chain where constraints together, as well as add or clauses to the query. The orWhere method accepts the same arguments as the where method:
You can also use the whereIn method.
->whereIn('items.type', ['shirt', 'glass'])
The whereIn method verifies that a given column's value is contained within the given array:
If you want a OR condition, you should wrap it within a where!
->where(function($query){
$query->where('items.type',"shirt")->whereOr('items.type',"glass");
});
OR
->whereIn('items.type', ['shirt', 'glass'])
The 'lists' method surely does retrieve a list of column's values, but I want the method to retrieve the values but without duplication, how to achieve that?
$types = DB::table('events')->lists('type');
If you need to get just distinct values, you should do:
$types = DB::table('lists')->distinct()->lists('type');