Laravel:对象或视图的其他结构(数组,json ..)? [英] Laravel: object or other structures (array, json..) to the view?

查看:94
本文介绍了Laravel:对象或视图的其他结构(数组,json ..)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几种方法可以将数据传递到Laravel Blade视图.

There are several ways you may pass data to a Laravel Blade view.

在这个精妙的讨论中, Laravel隐藏属性.例如密码-安全性 安东尼奥·卡洛斯·里贝罗声明(我同意):

In this savvy discussion Laravel hidden attributes. e.g. Password - security Antonio Carlos Ribeiro states (and i agree) that:

您不应该向视图发送对象.在MVC模式中,视图应该接收相对于它们的数据,处理后的数据,而不是对象,因为它们不必了解您的任何信息.业务逻辑."

我正在学习Laravel,到处都是我经常看到的例子,例如:

I am learning Laravel and everywhere i look i often see examples like:

$users = User::all();
return View::make('users')->with('users', $users);

这个特别来自官方文档.

应该理想地使用哪种方法?
在将对象发送到视图之前,是否先将其转换为数组或其他格式?
您是否有选择地从所有不必要的值中清除数据,然后再将其传递给模板引擎?
除了在学术上可能是错误的之外,将对象传递到视图的潜在风险是什么?

What method should be ideally used?
Do you transform your objects in array or other formats prior to send them to the view?
Do you selectively clean your data from all the unnecessary values prior of pass it to the template engine?
Apart being probably academically wrong, what are the potential risks for passing the object to the view?

推荐答案

如果使用以下方法

$users = User::all();
return View::make('users')->with('users', $users);

您将在模型中得到User个对象的集合,并且可以使用loop打印所有User个对象,这很好,这样做有什么风险,它在您身上,所以您应该知道应该怎么做,但是如果您不想传递集合对象,那么也可以使用以下方法仅传递一个数组数组:

You will get a collection of User objects in your model and can use a loop to print out all the User objects and it's fine, what risk could be doing this, it's upon you, so you should know what should do but if you don't want to pass a collection object then it's also possible to pass only an array of arrays using:

$users = User::all()->toArray();
return View::make('users')->with('users', $users);

因此,您将在视图中获得一个数组数组,其中每个子数组将包含特定用户的详细信息.数组可能看起来像这样:

So, you'll get an array of arrays in the view where each child array will contain a perticular user's details. The array may look something like this:

array (size=2)
  0 => 
    array (size=5)
      'id' => int 1
      'username' => string 'heera' (length=5)
      'email' => string 'heerasheikh@ymail.com' (length=21)
      'created_at' => string '2014-01-20 06:10:53' (length=19)
      'updated_at' => string '2014-01-23 10:23:50' (length=19)
  1 => 
    array (size=5)
    'id' => int 2
    'username' => string 'usman' (length=5)
    'email' => string 'mdusyl@yahoo.com' (length=16)
    'created_at' => string '2014-01-20 06:10:53' (length=19)
    'updated_at' => string '2014-01-20 09:06:23' (length=19)

但是,您可以使用Laravel的传统方式,完全没有风险.不要盲目地跟随某些事物,用自己的感觉问自己,这可能给您带来什么风险.您只需要循环收集,别无其他.现在,选择是您的选择,如果您通过集合,则可以使用对象表示法,即$user->username,但是如果您通过数组,则必须使用类似$user['username']的东西.

But, you can use the Laravel's traditional way and there is no risk at all. Don't follow something blindly, use your sense and ask yourself, what risk it may rise for you. You are only about to loop the collection, nothing else. Now, the choice is your's, if you pass the collection then you can use object notation, i.e. $user->username but if you pass an array then you have to use something like $user['username'], that's it.

这篇关于Laravel:对象或视图的其他结构(数组,json ..)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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