Remove #include depending on the route - laravel

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.

Related

Show content on blade template depending the url

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

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

Laravel blade creating url

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?
Try this:
{{ url('website/' . $website->name) }}
This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}

Laravel extends - only send parameter when condition

I want to check for $house->category.
Depending on the category i want to send parameters with the #extends() functions.
I can't get it to work. When I use something like:
#if($house->category == 1)
#extends('templates.main')
#else
#extends('templates.main', ['logo' => 'img/logo/logo_red_white_text.png', 'favicon' => 'img/favicon/favicon_red.ico'])
#endif
The template is injected twice.
use "#include" instead of extends, and if it's being injected two times you can prevent that by handling that logic on the controller, make a method "checkHouseCategory" or something like that, to check if $house->category == 1, then just send a boolean to the template.
The problem is that you cannot pass parameters in #extends function.
What you can do is to move the view partial to separated file and then #include it in i.e. main file with the above condition. So this will looks like:
Partial view with #extends I will name: example.blade.php
Then in the main view partial:
#if($house->category == 1)
#include('example')
#else
#extends('example', ['logo' => 'img/logo/logo_red_white_text.png', 'favicon' => 'img/favicon/favicon_red.ico'])
#endif

Get image name from database in laravel

I have database, with columns image and alttag. I want to use them in laravel blade view. I try something like this:
{{ HTML::image('images/{{ $item->image }}', $alt="{{ $item->alttag }}") }}
But syntax isn't correct. If i just echo image and alttag like this:
<h1>{{ $item->alttag }}</h1>
then they are correct. I wonder what is wrong in my code.
You used blade syntax in a PHP string. Watch the compiled blade templates to see where you did go wrong.
In short. Try this:
{{ HTML::image('images/'. $item->image, $alt = $item->alttag) }}
or equally short:
{{ HTML::image("images/{$item->image}", $alt = $item->alttag) }}

Resources