Show content on blade template depending the url - laravel

I'm trying to show some content depending on the url on ablade template but it's not working.
#if(Request::url() === 'myurl')
// code
#endif
I cannot see Request::url() content I tried wir dd, #dd, {{Request::url() }}, but always I get the literal text.
what I have to do?

Instead of matching urls, use request pattern matching. In blade you can do.
#if(request()->is('user/yourroute'))
// code
#endif

#if(Request::is(''))
// code
#endif

Related

How do I get data from the Route and show it in a html page?

I am trying get a value from the URL for example : websitename.com/dash/namehere
I have route :
Route::get('/dash/{id}', 'DashController#index' , function($id) {
return 'dash'.$id;
});
Then I have a section on the page I am trying to display as a title. I am currently using this:
#if (\Request::is('dash/1'))
<div class="pagetitle">[clientname]</div>
#endif
This works but I am thinking there is a better way to approach this. I am very new to this.
I want to get the id from the url / site.com/dash/ {id} and use it in the HTML page as a Title header.
Use {{ request()->route('id') }} in your blade to get the "id" parameter from the dash route.

How to get route parameter from blade to vue

I have a route:
Route::get('/{my_parameter}/home', function($my_parameter) {
return view('home_view')->with('my_parameter', $my_parameter);
}
In my home_view.blade.php file:
<div>
{{$my_parameter}} {{-- displays properly --}}
<vue-component #click="doSomething( {{$my_parameter}} )">Click me</vue-component> {{-- does not work--}}
</div>
I've tried numerous variants as suggested in my searches, including #{{$my_parameter}}. When I use a hard-coded string, #click=doSomething('my_value'), the function works properly.
How do I successfully get a route parameter from blade for use in a vue component? Thank you.
I think you just forgot to add quotes around the parameter. If you have doSomething({{ $my_parameter }}) and the parameter is broccoli, it's going to turn into doSomething(broccoli) rather than doSomething('broccoli').

Laravel hide code if requested Path was

Theres any Solution to hide an Code in Laravel for one Page?
With this i can display only one requested page ,Example:
#if (Request::path() == 'message/send')
#endif
Theres an opposite to hide an code for an path?
I have an URL also with after send an Username:
message/send/username
I have try: #if (Request::path() == 'message/send/') and #if (Request::path() == 'message/send/{{$username}}') but dont work.
Thanks
Use request()->is() for this:
#if (request()->is('message/send*'))
you can use segment method in laravel
#php $member = Request::segment(1);#endphp
#if($member =='member')
<style type="text/css"> .test{color: #1a393a !important;</style>
#endif
#endif
Here my first segment is member
The controller is the proper place to handle request specifics. However, if you still wish to do this in your blade view:
#if (Request::path() == 'message/send/' . $username)
You do not use template syntax {{ }} inside expressions. When the blade template is compiled, #if (expr) is converted to <?php if (expr): ?>, so standard PHP applies within the expression.
Route path comparison doesn't work that way. For example:
message/send/jwz104 will never be equal to message/send/{{$username}}
You could compare the route name:
#if(\Request::route()->getName() == 'message.send')
#endif
This also isn't a nice solution. You should handle this in your controller or by view composers.
with core php-
#if (strpos($_SERVER['REQUEST_URI'], "message/send") !== false)
#endif

Change interpolation brackets - Angular2

I want to use Laravel Blade and AngularJS.
Is some way to change interpolate sintax, change {{text}} for [{text}] or somthing like that?
I already change all components.ts files adding the line:
interpolation: ["{[", "]}"],
but, where I write blade, app breaks...
Thanks a lot everybody ;)
You can define your Blade content tags in your routes.php file:
Blade::setContentTags('<%', '%>');
Blade::setEscapedContentTags('<%%', '%%>');
EDIT: You can add # in front of the brackets, so Blade won't render it.
#{ var }
or you can pass the directive for blade not to render part of the HTML using #verbatim keyword.
#verbatim
<div>
{{ var }}
</div>
#endverbatim

Remove #include depending on the route

I have a menu partial, that includes a input search bar. But i don't want the search bar to be visibel on all pages, only for two specific uriĀ“s. Is there a way to remove that include in blade?
Currently it looks like this:
all
nes
snes
#include('partials._search')
I was thinking something like
all
nes
snes
#if($url)
#include('partials._search')
#endif
Use is() method:
#if (request()->is($url))
#include('partials._search')
#endif
Or if you know route name:
#if (request()->route()->getName() === $routeName)
#include('partials._search')
#endif
To retrieve URL from the blade template, use {{ Request::url() }}.
This will output something like http://example.dev/articles
To retrieve path, use {{ Request::path() }}
Using the above example, this will output articles.

Resources