如何在 Laravel 中将 stdClass 的对象转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46096318/
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
How to convert object of stdClass to array in Laravel
提问by Pioneer2017
I am getting the next error when trying to display a view in Laravel :
尝试在 Laravel 中显示视图时出现下一个错误:
"Cannot use object of type stdClass as array (View: C:\xampp\htdocs\mysite\resources\views\cms\contactus.blade.php)".
“不能使用 stdClass 类型的对象作为数组(视图:C:\xampp\htdocs\mysite\resources\views\cms\contactus.blade.php)”。
My Controller:
我的控制器:
public function contactus(){
ContactUS::get_content(self::$data);
return view('cms.contactus', self::$data);
}
My Model:
我的型号:
class ContactUS extends Model
{
public $table = 'contactus';
public $fillable = ['name','email','message'];
static public function get_content(&$data){
$sql = "SELECT cu.*,name,email,message FROM contactus cu "
. "ORDER BY cu.created_at DESC ";
$data['contactusd'] = DB::select($sql);
}
}
My view:
我的看法:
@extends ('cms.cms_master')
@section('cms_content')
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<h1 class="page-header">Contact us form information</h1>
@if($contactusd)
<br><br>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Message</th>
</tr>
</thead>
@foreach($contactusd as $item)
<tr>
<td>{{ $item['name']}}</td>
<td>
<ul>
<li> Email: {{ $item['email']}}, Massage: {{$item['message'] }}</li>
@endforeach
</ul>
</td>
<td>{{ $item[created_at]}}</td>
</tr>
</table>
@else
<p style="font-size: 18px">No information...</p>
@endif
</div>
@endsection
采纳答案by Exprator
you have hell lot of mistake in your blade file
你的刀片文件有很多错误
@if($contactusd)
<br><br>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Message</th>
</tr>
</thead>
@foreach($contactusd as $item)
<tr>
<td>{{ $item->name }}</td>
<td>
<ul>
<li> Email: {{ $item->email}}, Massage: {{$item->message }}</li>
</ul>
</td>
<td>{{ $item->created_at}}</td>
</tr>
@endforeach
</table>
@else
<p style="font-size: 18px">No information...</p>
@endif
or
或者
$contactusd = ContactUS::get_content(self::$data)->toArray();
return view('cms.contactus', $contactusd);
回答by Quezler
To convert an object to array (as is your question) use $array = (array) $object;
要将对象转换为数组(如您的问题),请使用 $array = (array) $object;
回答by Rahul Dhande
Try this..
尝试这个..
$sql = "SELECT cu.*,name,email,message FROM contactus cu "
. "ORDER BY cu.created_at DESC ";
$data['contactusd'] = DB::select($sql)->get()->toArray();
回答by Kalyan Tullimelli
If you getting data is in std class ,
you can write like this in view page {{ $item->name }}
如果你获取的数据是在 std 类中,你可以在视图页面中这样写 {{ $item->name }}
If you getting data is in std class,
you can write like this in view page {{$item['message']}}
如果你获取的数据是在std 类中,你可以在视图页面中这样写{{$item['message']}}
backside write like this
背面这样写
$sql = "SELECT cu.*,name,email,message FROM contactus cu "
. "ORDER BY cu.created_at DESC ";
$data['contactusd'] = DB::select($sql)->get()->toArray();
回答by Kalyan Tullimelli
use like this
像这样使用
$data = ContactUS::get_content(self::$data)->toArray();
return view('cms.contactus', $data);