php - 如何在 Laravel 中将 stdClass 的对象转换为数组

标签 php laravel laravel-5 laravel-5.3 laravel-5.4

尝试在 Laravel 中显示 View 时出现下一个错误:

"Cannot use object of type stdClass as array (View: C:\xampp\htdocs\mysite\resources\views\cms\contactus.blade.php)".

我的 Controller :

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

最佳答案

要将对象转换为数组(这是您的问题),请使用 $array = (array) $object;

关于php - 如何在 Laravel 中将 stdClass 的对象转换为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46096318/

相关文章:

php - 通知 : Undefined index: image - unable to find the error

laravel - 发送前预览 Laravel Mail

laravel - 如何从 Laravel 将路由参数传递给 Vue.js

本地和生产的 Laravel 环境文件可能无法按预期工作?

c# - 来自 Unity WWW 的 Laravel 5 登录 401 请求未经授权的 JWT(在浏览器和 Postman 中工作)

php - 如何GroupBy上组的最新记录

php - 将字符串转换为 UNIX 时间戳

PhP 和 MySQL WHERE 语法

php - 将时间字符串转换为小数小时 PHP

laravel - laravel 的路由是否足以抵御文件遍历攻击?