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

Pio*_*017 2 php laravel laravel-5 laravel-5.3 laravel-5.4

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

“不能将stdClass类型的对象用作数组(视图:C:\ xampp \ htdocs \ mysite \ resources \ views \ cms \ contactus.blade.php)”。

我的控制器:

public function contactus(){ 

    ContactUS::get_content(self::$data);
    return view('cms.contactus', self::$data); 

} 
Run Code Online (Sandbox Code Playgroud)

我的模特:

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

     }

} 
Run Code Online (Sandbox Code Playgroud)

我的看法:

@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
Run Code Online (Sandbox Code Playgroud)

Que*_*ler 6

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