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?
-
Not sure if the same applies in React as Vue JS, but just add a window.userID JS variable on the page and have the React data model use it.Rob Fonseca– Rob Fonseca2017-12-02 01:41:56 +00:00Commented Dec 2, 2017 at 1:41
-
so generally speaking, server side variables and client side variables are evaluated in completely different domains. are you rendering react on the server or the client?azium– azium2017-12-02 01:51:23 +00:00Commented Dec 2, 2017 at 1:51
Add a comment
|
2 Answers
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.
1 Comment
Hmerman6006
Great answer! This still works with Laravel 8 and inside Svelte component.
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!!!