HaxxanRaxa
4 months ago
340
1
Laravel

Error in using Laravel 7 component function as variable.

Posted 4 months ago by HaxxanRaxa

I am using Laravel 7 components and inside the component's class, I create a function to which I used as a variable in the component's view (blade) file. Here is my ClientSideBar class

class ClientSidebar extends Component {
    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct() {
        //
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return View|string
     */
    public function render() {
        return view('components.client-sidebar');
    }

    public function client(){
        $client = auth('client')->user()->format();
        return $client;
    }
}

And inside client-sidebar blade file I am trying to use client() function as variable like this:

$client['name']

But I get this error:

Cannot use object of type Illuminate\View\InvokableComponentVariable as array (View: D:\FiverrProjects\Laravel\KeySolutionsTestsPlatform\resources\views\components\client-sidebar.blade.php)

The annoying thing is, I did the same in my previous project last month and everything is working ok there but here :D

Please sign in or create an account to participate in this conversation.