Shaungbhone
Shaungbhone

Reputation: 11

Pass data to component in livewire

This is my welcome.blade.php

@extends('layouts.app')
@section('content')
<x-frontpage></x-frontpage>
@endsection

This is my frontpage.blade.php

<h1 class="text-center">{{ $product->name }}</h1>

This is my error

$product is not passed to welcome.blade.php

Upvotes: 1

Views: 13288

Answers (1)

Lars Klopstra
Lars Klopstra

Reputation: 31

You should add a props to your x-frontpage blade component, otherwise it wouldn't be able to receive it.

Here's an example:

<!-- frontpage.blade.php -->

@props(['product'])

<h1 class="text-center">{{ $product->name }}</h1>
<!-- welcome.blade.php -->

@extends('layouts.app')
@section('content')
<x-frontpage :product="$product"></x-frontpage>
@endsection

Also make sure your welcome.blade.php has the $product variable.

Here are more resources:

https://laravel.com/docs/7.x/blade

https://laravel-livewire.com/docs/rendering-components#render-method

Upvotes: 3

Related Questions