I'm learning Laravel and it uses OOPS concepts. Now I'm finding it hard to understand the real difference between array and objects. I actually know what an array and object is.
Array can hold more than one variable where as an object is an independent entity which has its own arguments and methods. We usually use foreach loop to loop through them.
In laravel, data is returned in the form of model instance as object. When the query response has multiple results, then data is returned in the form of an array which contains objects. I was trying to understand Collection Class used in laravel.
Codebright reference says
The Collection class itself, is merely a wrapper for an array of objects, but has a bunch of other interesting methods to help you pluck items out of the array.
Now coming back to my confusion. I was using different methods like all() and first() methods to fetch the result. But sometimes when i used arrow (->) to fetch the data using a foreach loop, from an object (contained in an array), it showed an error that says something like it is a non object. Then I used square brackets and the data was displayed.
I know we use [] to fetch data from arrays and we use -> to fetch data from objects. But I'm still confused about Laravel. Can someone clearly state the difference between them in reference to Collection class used in Laravel?
Edit:: The confusion began while using this code:
foreach($self_conversations as $self_conversations_fetch){
//fetching each conversation id
$conversation_id = Conversation::find($self_conversations_fetch->conversation_id);
$user_id = array();
//fetching each conversation member's id
foreach($conversation_id->conversationsMember as $conversationmembers)
$user_id[] = $conversationmembers->user_id;
$self_id = User::where('email', Session::get('email'))->first()->id;
$self_id_array = array($self_id);
$friend_id_array = array_diff($user_id, $self_id_array);
foreach($friend_id_array as $friend_id) array_push($friend_ids, $friend_id);
$conversations_reply_obj = ConversationReply::where('conversation_id', $self_conversations_fetch->conversation_id)->orderBy('updated_at', 'desc')->first();
$conversations_reply[] = $conversations_reply_obj['reply'];
}
As you can see, i have used square brackets to fetch the data(in the last line).
$conversations_reply[] = $conversations_reply_obj['reply'];
i was expecting arrow to work here
Actually the Collection class is a wrapper object which returns a collection of objects. For example, if you have a Model for example, User then you may use it in various ways, to get all records you may use User::all() and to get a single record you may use User::find(1) and there are other ways as well.
If you use all(), get() methods then you'll get a collection object, it means a collection of User models when you use these methods on User model and remember all() and get() always returns a collection of models even if there is only one model in it, so check this examaple:
$users = User::all(); // returns a collection
You may use first() method of Collection object like this:
$users = User::all();
$users->first();
Or directly just:
$user = User::first();
You may also use last to get the last item/model from the collection. You may also use get() like this:
$users = User::all();
$users = User::get(0) // to get first item/model
$users = User::get(1) // to get second item/model
You may also use a loop like this:
$users = User::get(); // same as all
// pass the collection to the view
return View::make('users.index')->with('users', $users);
Now in your views/users/index.blade.php view you may use a loop like this:
#foreach($users as $user)
{{ $user->username }}<br />
{{ $user->email }}<br />
#endforeach
It's important to knoe that, all() and get() methods returns a collection and first() and find(id) returns a single model object, so if you have a single model then you may directly use it like this:
$user = user::find(1); // 1 is id for example
return View::make('users.index')->with('user', $user);
In your view you may use:
{{ $user->email }}
You may use an object using -> for example $user->name and an array using $user['name'] but in this case you may use both syntax because Laravel's Eloquent/Model implements ArrayAccess (along with others) interface so every model that extends Eloquent could be used using both array and object syntax to access properties. So, following is possible:
$user = User::where('username', 'me')->get();
return View::make('users.index')->with('user', $user);
In the view you may use:
{{ $user->name }}
{{ $user['name'] }}
For better understanding of the Collection class and it's methods check the source code, you may find it at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php of your local installation and it extends Illuminate/Support/Collection.php class. Check both classes. You may also read this article, it'll help you more.
I think I know the source of your problem, as I've been having to work around it. If you are using the arrow notation on a model object and there is no record in the relationship, then you'll get the "non-object" error. For example, if you have the line
$student->school->school_name
and a given $student doesn't have a school object, you'll get the non-object error. I typically add checks for empty($student->school) or empty($student->school_id) or the like to avoid this when I run into it (or heaven forbid forsee the problem when doing my initial coding pass).
EDIT: So, to clarify, Laravel doesn't say "oh, there's a relationship there, so school is an object which happens to be empty, so I'll return null for school_name", it says "there's no school, so ->school_name is an invalid call to an object property"
Related
ErrorException:
stripos() expects parameter 1 to be string, object given
For the groupBy() call in the with() method
$user = User::with([
'pricelists' => function($query) {
$query->groupBy(function($var) {
return Carbon::parse($var->pivot->created_at)->format('m');
});
}
])->where('id', $id)->get();
I already saw a few posts talking about how to manage this problem and that it shall not be possible to use groupBy() in eloquent but I do not really understand why...
To be clear:
User and Pricelist model got a many-to-many relationship with the default timestamps() method. I am trying to get the downloaded pricelists grouped by their months they were downloaded from the current user.
After a few attempts I just deleted the above shown => function($query... statement from the with() method and just left the with(['pricelist']) to fetch all datasets and tried this:
$user->pricelists = $user->pricelists->groupBy(function($var) {
return Carbon::parse($var->pivot->created_at)->format('m');
});
return $user->pricelists;
And it works fine and returns an array with multiple arrays for each month... But returning it like this:
return $user;
returns just 1 array with all entries... I do not really get the sense behind it right now...
The two groupBy() method that you are using in the two code you provide are totally different methods.
The first groupBy() where you use it in the callback is actually being called by $query which is a query builder object. The groupBy() here is used to add SQL GROUP BY Statement into the query. And as per the documentation, it only take string variables as parameter.
The groupBy() in your second code is being called by $user->pricelists which is a laravel eloquent collection. The groupBy() method here is actually from the base collection class and is used to group the items inside the collection into multiple collections under the different key defined by the parameter passed to the function. Please read the documentation here.
For your case, the second groupBy() is the one you should be using since you plan to use a callback and will allow you to use more complicated logic.
I need to get an element from the database, but I can not get it by the FIND method, since FIND only finds it by the primaryKey and what I need is not by my primaryKey. So I did like this:
$user = Pac::find($request->pac_id);
$element = query()->where('med_cart', $user->pac_id)->get();
$element->med_obs = $request->med_obs;
$element->save(); // error
Now I need to save this element, however, I can not use the SAVE method, as I believe it is fully connected with FIND and FINDORFAIL (if anyone knows, explain to me which methods I can use the SAVE method).
How can I save them the way I did? Or is there some other way to do it?
Because I need to get the element with a data other than the primaryKey and then save it, then I can not use FIND or FINDORFAIL, I think.
The function ->find() returns an Eloquent Model instance and you can then call ->save() on the model instance.
You're using ->get() which returns a Collection.
To update your query (that may target one or more entries) just perform the update statement directly from the QueryBuilder by replacing ->get() with ->update(['med_obs' => $request->med_obs]).
Be aware that when doing this you are now using Fluent queries, instead of eloquent. This means that any logic you may have defined in the boot function of your model is not evaluated.
If you are certain that you only have a single result you can append ->first() to your query, which will return a Model of the first result that matches your ->where clause. You can then call ->save() on it:
$user = Pac::find($request->pac_id);
$element = query()->where('med_cart', $user->pac_id)->first();
$element->med_obs = $request->med_obs;
$element->save();
I need to understand when/not to use get(); in Laravel 5.
PHP warning: Missing argument 1 for Illuminate\Support\Collection::get()
Google shows me answers to their issue but no one really explains when you should/not use it.
Example:
App\User::first()->timesheets->where('is_completed', true)->get(); // error
App\Timesheet::where('is_completed', true)->get(); // no error
Fix:
App\User::first()->timesheets()->where('is_completed', true)->get(); // no error
Noticed the timesheets() and not timesheets? Could I have a detail explanation for what is going on, please?
I'm coming from a Ruby background and my code is failing as I do not know when to use () or not.
I'll try to describe this as best I can, this () notation after a property returns an instance of a builder, let's take an example on relationships,
Say you have a User model that has a one-to-many relationship with Posts,
If you did it like this:
$user = App\User::first();
$user->posts();
This here will return a relationship instance because you appended the (), now when should you append the ()? you should do it whenever you want to chain other methods on it, for example:
$user->posts()->where('some query here')->first();
Now I will have a the one item I wanted.
And if I needed say all posts I can do this:
$user->posts;
or this
$user->posts()->latest()->get();
$user->posts()->all()->get();
So the key thing here is, whenever you want to chain methods onto an eloquent query use the (), if you just want to retrieve records or access properties directly on those records then do it like this:
$user->posts->title;
Well, ->timesheet returns a collection, where ->timesheet() returns a builder.
On a Collection you can use ->where(), and ->get('fieldname'), but no ->get().
The ->get() method can be used on a builder though, but this will return a collection based on the builder.
Hope this helps.
The 'problem' you are facing is due to the feature of being able to query relations
When accessing a relation like a property, ->timesheets, the query defined in the relationship is executed and the result (in the form of a Collection) is returned to you.
When accessing it like a method, ->timesheets(), the query builder is returned instead of the resulting collection, allowing you to modify the query if you desire. Since it is then a Builder object, you need to call get() to get the actual result, which is not needed in the first case.
When you use ->timesheets you are accessing a variable, which returns the value of it (in this case an instance of Collection).
When you use ->timesheets() you are invoking whatever is assigned to the variable, which in this case returns an instance of Builder.
whilst pascalvgemert's answer does answer your problem regarding Laravel, it does not explain the difference between accessing or invoking a variable.
In simple term
$user = App\User::get();
is used to fetch multiple data from database
rather
$user = App\User::first();
is used to fetch single record from database
I am using laravel-permission for managing roles and displaying content. Per the docs you can retrieve a users roles by using $roles = $user->roles()->pluck('name'). My problem is that the data returned is ["admin"] rather than just admin. I was reviewing the collections methods and it looked like get('name') would return what I was looking for. When I try to use the following command Auth::user()->roles()->get('name') I get
1/1
ErrorException in BelongsToMany.php line 360:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\BelongsToMany::getSelectColumns() must be of the type array, string given
It seems to me like the get() method is expecting an array however, I'm trying to reference an item in the array. The raw output of Auth::user()->roles()->get() is [{"id":1,"name":"admin","created_at":"2016-03-10 06:24:47","updated_at":"2016-03-10 06:24:47","pivot":{"user_id":1,"role_id":1}}]
I have found a workaround for pulling the correct content, but it is using regex for removing the unwanted characters that are included in the pluck() method.
preg_replace('/\W/i','', Auth::user()->roles()->pluck('name'))
It seems like I'm missing something or approaching using the get() method incorrectly. Any advice is appreciated.
I think pluck() will return the value of the given column for each model in the collection, which would explain the array. In your case it looks like the user has only one role, so you get an array with only one item. If the user had multiple roles, you would likely get an array with multiple items in it.
On the other hand, the get() method is used to execute a query against the database after a query is built. The results of the query are what is returned. To return a collection of models with only a single value you will need to pass an array with just the one column you want, but that will just select models, which does not appear to be what you ultimately need.
You can try this instead: $roles = $user->roles()->first()->name
The call to first() will grab the first model in the collection returned by roles(), and then you can grab the name of the role from that model.
I typically throw some error checking around this:
$role = $user->roles()->first();
if (is_null($role)) {
//Handle what happens if no role comes back
}
$role_name = $role->name;
That's because an user can have many roles, so roles is a collection.
If you are 100% sure that a user will have only one role, you can easily do
Auth::user()->roles()->first()->name
That will get the first item of that collection (the role) and then its name.
I'm getting confused with the various functions in Laravel's Eloquent ORM and what they return. I have a Post model for a blog - if I write any of the following then I get back an array of all the posts:
$posts = Post::all();
$posts = Post::get();
$posts = Post::with('user')->all();
However, if I chain some other methods, it doesn't work. For example this gives an Unhandled Exception error: "Method [all] is not defined on the Query class."
$posts = Post::with('user')->order_by('updated_at', 'desc')->all();
And if I use the paginate function, I don't get an array of results at all.
$posts = Post::with('user')->order_by('updated_at', 'desc')->paginate(5);
// index.blade.php
#foreach ($posts as $post)
{{ $post->id }}
#endforeach
I get the error: "Trying to get property of non-object". Turns out the data is in $posts->results, not $posts.
This is all very confusing! How do I get my head around this? What do each of these functions return and how do I chain them correctly?
Without going through every single method available the simple answer is to figure out what data type the output is.
dd($whatever);
If it's an object then find the class that the object is an instance of and understand how that class works.
If it's a query object, you can chain it, if it's an array or null you can't. If it's an instance of a model class then you CAN chain it, but you can only call methods that exist in the model class or in your derivative class.
Here's a brief article that I write about Eloquent and Fluent that may be helpful as well: http://laravel.io/topic/17/what-are-fluent-and-eloquent
When I run this:
$user = User::where('email','=','test#example.com');
dd($user); # object(Illuminate\Database\Eloquent\Builder)
// After:
dd($user->get()); # object(Illuminate\Database\Eloquent\Collection)
// And finally, to get model object:
dd($user->get()->first()); # object(App\User)
By peeking into the source code, it's not that badly documented:
https://github.com/laravel/laravel/blob/master/laravel/database/eloquent/query.php#L98
Every method has #return on their phpdoc so you immediately know what should they return. I suggest you read the docs as well.