Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>{{ $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
like image 258
Pioneer2017 Avatar asked Jan 01 '26 15:01

Pioneer2017


1 Answers

To convert an object to array (as is your question) use $array = (array) $object;

like image 163
Quezler Avatar answered Jan 03 '26 05:01

Quezler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!