在Laravel阵列和对象之间的困惑 [英] Confused between array and objects in Laravel

查看:106
本文介绍了在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

收集类本身,仅仅是对象的数组的包装,但有一堆的其他有趣的方法来帮助您采摘项目从数组中。

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.

现在回来我的困惑。我用不同的方法,如所有()第()方法来获取结果。但有时当我用箭头( - > )从对象(包含在数组中)获取使用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使用的集合类?

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?

:混乱而使用这个code开始:

: 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

推荐答案

其实,收藏类是返回对象的集合包装对象。例如,如果你有例如型号,用户,那么你可以通过各种方式使用它,让你可以使用用户所有记录::所有()键,让你可以使用一个记录用户::发现(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.

如果您使用所有()的get()方法,那么你会得到一个集合对象,它意味着用户的集合模型时,您使用用户模型这些方法和记所有( )的get()总是返回的模型的集合,即使只有一个在其模型中,因此检查这个examaple:

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

或者直接刚:

$user = User::first();

您也可以使用末页来获取从集合中最后一个项目/型号。您也可以使用的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);

现在在你的的意见/用户/ 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

这是到没什么两样那么重要,所有()的get()方法返回一个收集和第()找到(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 }}

您可以用使用对象 - &GT; 例如 $用户自&GT;名称和阵列使用 $用户['名'] 但在这种情况下,你可以使用这两个语法,因为 Laravel的 锋/型号工具 ArrayAccess接口(连同其他)接口,这样每一个扩展模型可以同时使用数组和对象语法访问属性来使用。因此,以下是可能的:

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

为了更好地理解收藏类,它的方法检查源$ C ​​$ C,你可以在供应商/ laravel /框架找到它/src/Illuminate/Database/Eloquent/Collection.php 的本地安装和它扩展照亮/支持/ 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阵列和对象之间的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆