How to pass params from laravel to ReactJS?

My application uses Lavarel and I have certain ReactJS components. Routing is done by Laravel. For a route /Users/<userid>, if the controller is UserController.php and view is userview.blade.php, how do I get the <userid> from the URL in my react component loaded in a div on userview.blade.php?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can try out this library from Laracasts which allows you to pass server-side data (string/array/collection) to your JavaScript via a Facade in your Controller.

This may look something like this (haven’t tested it, but you get the gist).

public function UserController($userid)
{
    JavaScript::put([
        'userid' => $userid,
    ]);

    return View::make('yourview');
}

The docs further show how to access it from your view.

https://github.com/laracasts/PHP-Vars-To-Js-Transformer

Method 2

UserController.php
you can access the userid and send it to view like this:

public function getUser($userid) {
    return view('userview')->with('userid', $userid);
}

userview.blade.php
you can grab it (in some input) like this:
<input type="text" value="{{ $userid }}" />

This is, how I understand your problem. If it is not so, kindly correct me!!!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x