Cast Array to an Object in Controller and then pass to view in laravel

Created at 21-Aug-2021 , By samar

Cast Array to an Object in Controller and then pass to view in laravel

In this tutorial, we will try to find the solution to "Cast Array to an Object in Controller and then pass to view in laravel" through programming.

You can change the array to an object using the (object) method in your controller and then you can pass the value in the view file. You can display the view file using $array->title syntax.
  • Change array to object in controller and access in view file in laravel

    //routes\web.php
    Route::get('/array-to-object', function(){
        $array = array('title' => 'hello world');
        $object = (object)$array;
        return view('index')->with(compact('object'));
    });
    
    //resources\views\index.blade.php
    {{ $object->title }}
    

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.