Laravel:如何将 obj std 类传递给我的视图并调用它以及与传递简单数组的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19331438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:33:04  来源:igfitidea点击:

Laravel: how do I pass obj std class to my view and call it and the difference with passing a simple array?

laravelstdclass

提问by unloki9

I would like to ask on how will I display this type of array in laravel blade view?

我想问一下如何在laravel刀片视图中显示这种类型的数组?

Array(
[0] => stdClass Object
    (
        [id] => 2
        [email] => [email protected]
        [username] => dodie
        [firstname] => Jhon
        [lastname] => David
        [password] => y$x12gVo54SsUW0U1aHTHaXu7VbgHFG8So9.fd/DfczT7KOyk4J1nLS
        [location] => {'long':47.061469, 'lat': 8.321743}
        [photo] => 1.jpg
        [hometown] => England
        [Updated] => 1
        [description] => "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
        [role] => client
        [deleted_at] => 
        [created_at] => 2013-10-10 06:00:18
        [updated_at] => 2013-10-10 07:24:22
        [reviewer_id] => 1
        [company_id] => 2
        [title] => Very Bad Ass
        [content] => badass
        [review_rating] => 1
        [helpful] => 0
        [logo] => 2.jpg
        [contactperson] => Jhon Doe
        [companyname] => Hardy Contractor
        [address] => makati Philippines
        [type] => Heating and Airconditioning
        [stars] => 2
        [phone] => 554-9266
        [fax] => Hardy Contractor
        [website] => www.hardycontractors.com
    )

This is how I pass this In my Controller:

这就是我在我的控制器中传递它的方式:

$data = DB::select($query); //fetching data using Query Builder
return View::make('home')->with('users', $data);

this is how I call it on my blade view:

这就是我在刀片视图中的称呼:

$users->website

Every time I call this on my blade can you please enlighten me? :) I want to understand more on how I can handle this kind of problem.

每次我在我的刀片上调用这个时,你能指教我吗?:) 我想更多地了解我如何处理此类问题。

回答by Glad To Help

What you have is an array of std class object.

你所拥有的是一个 std 类对象的数组。

In your view you can simply loop through them:

在您看来,您可以简单地遍历它们:

@foreach($users as $user) 
 {{ $user->website }}
@endforeach

In case you have one and only one object in the array, you might want to consider passing $users[0] to the view and then work with it as a single object.

如果数组中只有一个对象,您可能需要考虑将 $users[0] 传递给视图,然后将其作为单个对象使用。