jeudi 7 septembre 2017

How to convert object of stdClass to array in Laravel

I am getting the next error when trying to display a view in Laravel : "Cannot use object of type stdClass as array (View: 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></td> 
            <td>
                <ul>

                    <li> Email: , Massage: </li>
                    @endforeach
                </ul>
            </td> 
            <td></td> 
        </tr>

    </table> 
    @else 
    <p style="font-size: 18px">No information...</p>
    @endif
</div> 

@endsection



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire