【问题标题】:Laravel: view->with passing an objectLaravel:视图-> 传递一个对象
【发布时间】:2018-06-30 02:51:46
【问题描述】:
$somedata = Indicator::all();
$indicators = [];
// ... (re-structure the data; rows to columns)
$indicators[] = ['a'=>'2016', 'b'=>'2017', 'c'=>'2018'];
$indicators[] = ['a'=>'1232', 'b'=>'3242', 'c'=>'5467'];
$indicators[] = ['a'=>'1232', 'b'=>'3242', 'c'=>'5467'];
$indicators[] = ['a'=>'closed', 'b'=>'closed', 'c'=>'open'];> 

// ??? How to form a valid object to send ???
return view('indicators.index')->with(['indicators'=> $indicators]);

我选择数据。我改变了显示它的结构;但找不到正确的结构来传递我的回复。

视图抛出错误“尝试获取非对象的属性(视图:”

(我查看了 Indicator::all(); 的转储,想知道我的方法是否正确/错误)

// 菜鸟

【问题讨论】:

  • 请显示indicators/index.blade.php视图

标签: php arrays laravel object response


【解决方案1】:

您正在返回一个数组,我认为您可能正在尝试使用对象表示法访问索引,例如:

$val->prop

什么时候应该:

$val['prop']

Indicator::all() 返回您未在视图中使用的对象集合。

顺便说一句,Laravel 集合有一些方便的辅助函数来处理结果集。您可能对以下内容感兴趣:

https://laravel.com/docs/5.5/collections#method-map

【讨论】:

  • 这有助于我走上正轨... PHP 自 1999 年以来发生了变化!所以...我想传递对象以在视图上保持一致。是否有必要创建一个小对象类?我可以用某种形式构建它 $rows[] = (object) [a->$month_0, b->$month_1, b->$month_1 ... ];最佳做法是什么?
  • 已解决:$rows[] = (object) array('a'=>'3453', 'b'=>'5435', 'c'=>'345'); 非常感谢 StackOverFlow 的每一个人!
【解决方案2】:

由于$indicators 是一个数组,因此您必须使用[''] 而不是-> 来访问其数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 2017-03-31
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2018-07-05
    • 2015-10-27
    相关资源
    最近更新 更多