Home Pass variable into Laravel {{ route }} helper
Reply: 3

Pass variable into Laravel {{ route }} helper

Steven Grant
Steven Grant Published in 2018-02-13 02:47:41Z

I have routes for /admin/login and also for /user/login

I'm looking for both of the views to share the same layout file and so looking to pass the first segment (admin or user) into the route helper.

Is that possible?

So effectively, I'd be looking to do something like:

{{ route($thisIsDynamic.'.login') }}

where $thisIsDynamic would either be admin or user depending on the URL.

Kasnady Reply to 2018-02-13 02:55:38Z

route only can contain of route name. If you want to pass link, you should use URL::

{{ URL::to($thisIsDynamic.'login') }}

Refer this: Laravel blade templates, foreach variable inside URL::to?

Jonjie Reply to 2018-02-13 02:57:45Z

Based on my understanding, you may try using a wild card and passing the data into the URL like so:


Route::get('/login/{type}', function($type){
  return view('login', compact('type'));

You can now have access on the $type variable on the view.

Now, if you want to access the route, just add it a name then pass the $type on it.

Ciarán Reply to 2018-02-13 05:47:34Z

I don't recommend redirection based on route names when they are dynamic. But if you really want to do that, Here is how you do it.


And, whenever you want to pass by name, you can do this {{ route("$thisIsDynamic-login") }}. But, As I said, this is not the right way to do this. Here is how you can do it the right way.


    return view('login')->with('type',$type);

login.blade.php (Just an example of how you do it)


    @if($type == 'user')
        // User login form
        // Admin login form

I assume this is what you want to do. Let me know if I misunderstood anything or if you have any more queries.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.300575 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO