简体   繁体   中英

Laravel: view->with passing an object

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

I select data. I change the structure for displaying it; but cannot find the correct structure to then pass in my response.

The view throws the error "Trying to get property of non-object (View: "

(I looked at the dump of Indicator::all(); and wonder if I have the right/wrong approach)

// noob

You're returning an array, I image you are probably trying to access an index using object notation like:

$val->prop

when it should be:

$val['prop']

Indicator::all() returns a collection of objects which you're not using in your view.

As an aside, Laravel collections have some handy helper functions to work with result sets. You may be interested in:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM