尝试在Laravel中显示视图时出现下一个错误:
“不能使用stdClass类型的对象作为数组(视图: C:\ XAMPP \ htdocs中\ mysite的\资源\视图\ CMS \ contactus.blade.php)”
我的控制器:
public function contactus(){
ContactUS::get_content(self::$data);
return view('cms.contactus', self::$data);
}
我的模特:
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);
}
}
我的观点:
@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
答案 0 :(得分:3)
要将对象转换为数组(就像您的问题一样),请使用$array = (array) $object;
答案 1 :(得分:0)
试试这个..
$sql = "SELECT cu.*,name,email,message FROM contactus cu "
. "ORDER BY cu.created_at DESC ";
$data['contactusd'] = DB::select($sql)->get()->toArray();
答案 2 :(得分:0)
像这样使用
$data = ContactUS::get_content(self::$data)->toArray();
return view('cms.contactus', $data);
答案 3 :(得分:0)
你的刀片文件中有很多错误
@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
或
$contactusd = ContactUS::get_content(self::$data)->toArray();
return view('cms.contactus', $contactusd);
答案 4 :(得分:0)
如果您获取的数据是std类,
你可以在视图页{{ $item->name }}
如果您获取的数据是标准类,
你可以在视图页{{$item['message']}}
背面写这样的
$sql = "SELECT cu.*,name,email,message FROM contactus cu "
. "ORDER BY cu.created_at DESC ";
$data['contactusd'] = DB::select($sql)->get()->toArray();