3

As an example there is this component:

resources/views/components/example.blade.php

<div>
    @if($foo === "bar")
        Bar
    @else
        Foo
    @endif
</div>

that I render like

@php
   $foo = "bar";
@endphp
<x-example :foo="$foo" />

what is working. But to have my code looking cleaner, I want to pass the string directly to the component, but the following I tried are not working:

<x-example :foo="bar" />
<x-example :foo='bar' />
<x-example :foo="\'bar\'" />
<x-example :foo="'bar'" />
<x-example :foo=""bar"" />
3
  • 1
    Hard coded values don't require the : before the parameter name; <x-example foo="bar" />. Commented Sep 15, 2022 at 9:49
  • That's it - thank you. If you could add that as an answer, I'd vote it as resolved Commented Sep 15, 2022 at 9:51
  • @JanBoehmer you should take a look at how to pass data to Blade Components which provides some useful examples that should get you started. Commented Sep 15, 2022 at 9:53

1 Answer 1

7

Easy mistake to make, as mentioned in the Laravel documentation on passing data to components;

You may pass data to Blade components using HTML attributes. Hard-coded, primitive values may be passed to the component using simple HTML attribute strings. PHP expressions and variables should be passed to the component via attributes that use the : character as a prefix

So update how you use the Blade component as below:

<x-example foo="bar" />
Sign up to request clarification or add additional context in comments.

3 Comments

just a suggestion, adding a link to the Laravel Blade Components Docs would make your answer more powerful and helpful.
@ths Indeeed. I wasn't going to as you'd already linked to the docs in your comment. I'll add a link regardless as you're correct, it is helpful regardless. [:
yes indeed, thank you for having my suggestion considered and making the required changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.