沃梦达 / 编程问答 / php问题 / 正文

Laravel中的数组和对象之间的混淆

Confused between array and objects in Laravel(Laravel中的数组和对象之间的混淆)

本文介绍了Laravel中的数组和对象之间的混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Laravel,它使用 OOPS 概念.现在我发现很难理解数组和对象之间的真正区别.我其实知道什么是数组和对象.

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.

数组可以保存多个变量,其中对象是一个独立的实体,它有自己的参数和方法.我们通常使用 foreach 循环来循环它们.

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.

在 laravel 中,数据以模型实例的形式作为对象返回.当查询响应有多个结果时,数据以包含对象的数组的形式返回.我试图了解 laravel 中使用的集合类.

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 参考说

Codebright reference says

Collection 类本身只是一个对象数组的包装器,但还有许多其他有趣的方法可以帮助您从数组中提取项目.

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.

现在回到我的困惑.我使用了不同的方法,例如 all()first() 方法来获取结果.但有时当我使用箭头 (->) 使用 foreach 循环从一个对象(包含在一个数组中)中获取数据时,它会显示一个错误,说它是一个非对象.然后我用了方括号,数据就显示出来了.

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.

我知道我们使用 [] 从数组中获取数据,我们使用 -> 从对象中获取数据.但我仍然对 Laravel 感到困惑.有人可以参考 Laravel 中使用的 Collection 类清楚地说明它们之间的区别吗?

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?

: 使用此代码时开始出现混乱:

: 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'];

我期待箭头在这里工作

推荐答案

其实 Collection 类是一个包装对象,它返回一个对象的集合.例如,如果您有一个模型,例如 User,那么您可以以各种方式使用它,以获取您可以使用 User::all() 的所有记录并获取一条记录,您可以使用 User::find(1) 并且还有其他方法.

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.

如果你使用 all()get() 方法,那么你会得到一个集合对象,这意味着 User 的集合当您在 User 模型上使用这些方法并记住 all()get() 始终返回模型的集合时,即使只有一个模型,所以检查这个例子:

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

你可以像这样使用 Collection 对象的 first() 方法:

You may use first() method of Collection object like this:

$users = User::all();
$users->first();

或者直接:

$user = User::first();

您也可以使用 last 从集合中获取最后一个项目/模型.你也可以像这样使用 get():

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

现在在您的 views/users/index.blade.php 视图中,您可以使用这样的循环:

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

重要的是要知道,all()get() 方法返回一个集合,而 first()find(id) 返回一个单独的模型对象,所以如果你有一个单独的模型,那么你可以像这样直接使用它:

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 }}

{{ $user->email }}

您可以使用 -> 使用对象,例如 $user->name 和使用 $user['name'] 的数组code> 但在这种情况下,您可以同时使用这两种语法,因为 LaravelEloquent/Model 实现了 ArrayAccess(以及其他)接口,因此每个模型扩展 Eloquent 可以使用数组和对象语法来访问属性.因此,以下是可能的:

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

视图中你可以使用:

{{ $user->name }}
{{ $user['name'] }}

为了更好地理解 Collection 类及其方法,请查看源代码,您可以在 vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php 找到它 您的本地安装,它扩展了 Illuminate/Support/Collection.php 类.检查两个类.你也可以阅读这篇文章,它会帮助你更多.

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.

这篇关于Laravel中的数组和对象之间的混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel中的数组和对象之间的混淆

基础教程推荐