2

Deep down in my components tree I have a select tag, which need to be populated by data from the backend. This should happen initially when the page loads.

Is there a way to pass data using with in my controller and then into some auxillary js file?

return view('greeting')->with('name', 'Victoria');

An option is doing an API call after component mount but I rather avoid that. What is the best practice here?

1
  • 2
    You can always set a variable in the blade file. I assume you're using something like an index.blade.php, then in a script or on an element do: let myData = '{{ $name }}' or <input type="hidden" name="name" value="{{ $name }}" /> Commented Feb 10, 2018 at 8:50

1 Answer 1

1

Thanks @btl! Works perfect, just adding example here.

Route::get('/', function () {
    return view('welcome')->with('name', 'Victoria'); });

In welcome.blade.php

<!doctype html>
<html lang="{{ app()->getLocale() }}">
    <head>
    ...
    </head>
    <body>        
        <div id="main"></div>
        <script>
            let myData = '{{ $name }}'
        </script>
        <script src="{{asset('js/app.js')}}" ></script>                
    </body>
</html>

Finally in some arbitary jsx file (to be compiled into app.js)

<h1>{myData}</h1>
Sign up to request clarification or add additional context in comments.

Comments

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.