Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel can not pass variable from controller to component

I can not see my variable in component

Controller

class DigitalContentController extends Controller
{
    public function productsList(){
        $contents = DigitalContent::all();
        return view('pages.digitalContents', compact(['contents']));
    }
}

digitalContents

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@extends('layouts.base')

@section('body')

<x-content-card></x-content-card>

@endsection

Component

@foreach ( $contents ?? [] as $item )
    {{ $item->name }}
@endforeach

Even when I echo variable before return view I can see the variable.

class DigitalContentController extends Controller
{
    public function productsList(){
        $contents = DigitalContent::all();
        echo $contents
        return view('pages.digitalContents', compact(['contents']));
    }
}

>Solution :

Because you’ve to parse the variable to the component. So it should be:

<x-content-card :contents="$contents"></x-content-card>

Don’t forget to add contents to your component class

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading