In my home.blade.php file I have several div-s.And if the {{$user[0]->d1}} is 1 then the div must be hidden.But I have no idea how to do that.
{{$user[0]->d1}} //I want to have "if $user[0]->d1 == 1 ,then disable the following div
<div>Text 1</div>
Is it even possible(I don't want to use gates or cans)?
Update: You can actually also use the #unless directive which better translates your logic so you don't have to reverse the logic...
#unless ($user[0] == 1)
<div>Text 1</div>
#endunless
Other solution with #if directive:
You can simply use the #if directive like this:
#if ($user[0]->d1 != 1)
<div>Text 1</div>
#endif
Or even better (in case $user[0] might not exist):
#if (!isset($user[0]->d1) || $user[0]->d1 != 1)
<div>Text 1</div>
#endif
Related
We are new to sage9 / Blade framework and we are trying to create a template logic with ACF. Everything was working well with 1 block but when we add more block the first 1 echoes 2-3 times.
Here's how we did it:
We use the default layouts.app from sage9:
web/app/themes/[theme-name]/resources/views/layouts/app.blade.php
...
<div class="vy_main uk-offcanvas-content">
<header>
#yield('dispatch_banner')
</header>
<div class="wrap container" role="document">
<div class="content">
<main class="main">
#yield('dispatch') //The one we currently working on
</main>
...
In the layout we are calling #yield('dispatch'). Inside page.blade.php we are extending the layouts and we add the dispatch section.
web/app/themes/[theme-name]/resources/views/page.blade.php
#extends('layouts.app')
#section('dispatch_banner')
#layouts('banners')
{!! App::banners() !!}
#endlayouts
#endsection
#section('dispatch')
#layouts('sections')
{!! App::sections() !!} //This call a controller where we can select the correct section to display.
#endlayouts
#endsection
Inside the controller,
web/app/themes/[theme-name]/app/Controllers/App.php we return a template to use and passing configurations/variables to use. :
public static function sections(){
...
$return .= \App\template('sections.'.$sections, $config);
}
return $return;
...
}
We create a standard block. This block include by the dispatcher :
web/app/themes/[theme-name]/resources/views/sections/std.blade.php
Inside this template, we created a new layouts "base", because all sections will have the same base structure, we extend this base inside the template and put a section content in it like so:
web/app/themes/[theme-name]/resources/views/sections/std.blade.php
#extends('layouts.base')
#section('section-content')
#if ($image)
<div class="uk-grid uk-flex-middle" data-uk-grid>
<div class="{!! $class_image !!}">
<img src="{!! $image['url'] !!}" alt="{!! $image['alt'] !!}">
</div>
<div class="{!! $class_content !!}">
#endif
<div class="{!! $content_class_content !!}">
#layouts('content')
{!! App::content() !!}
#endlayouts
</div>
#if ($image)
</div>
</div>
#endif
#endsection
And here the layout
web/app/themes/[theme-name]/resources/views/layouts/base.blade.php
<section {{ (( $section_id )?'id='.$section_id:'') }} class="{!! $class_section !!}">
#if($has_container)
<div class="uk-container uk-container-{!! $container !!}" data-uk-scrollspy="cls: uk-animation-fade;">
#yield('section-content')
</div>
#else
#yield('section-content')
#endif
</section>
As I said, everything was working fine with 1 block but as soon as we add a second block the data just keep repeating BUT only the #yield('section-content') from the base, the variables use inside the layout aren't repeating.
Here what we have in the html from :
<section {{ (( $section_id )?'id='.$section_id:'') }} class="{!! $class_section !!}">
We get :
<section class="uk-section vy_std uk-section-primary uk-section-xlarge">
<section class="uk-section vy_accordion uk-section-transparant">
<section class="uk-section vy_std uk-section-transparant">
Where is the problem with our logic and we the content from #yield('section-content') keep repeating instead of using the right data send from the controller?
If this can help I can send all the code from the controller, it's not big but to me it's wasn't where the problem is so I cut this part out.
Thank for your time!
I manage to work this out by using components/slots instead of layouts. Now I get the base from a component like so:
#component('layouts.base', $config)
/*Section code*/
#endcomponent
And everything is working again!
I'm trying to display the delete image button only if the logged user is the one who has uploaded the image. Unfortunately, when I do that, the delete image button disappears even though the logged user is the creator of the images. I do not see why would that not work correctly.
<div class="wrapper">
<div class='imageContainer'>
<h1 class='imageTitle'>{{$image->name}}</h1>
<div class="stickyImageContainer">
<img class='uploadedRealImage' src='/storage/images/uploaded/{{$image->file_name}}' alt='Random image'/>
#if (Auth::user() == $image->user_id)
<div class='deleteImageButton'></div>
<a class='deleteImageA' href='{{ route('deleteImage', ['image_id' => $image->id]) }}'>X</a>
#endif
</div>
</div>
</div>
You're trying to compare User object with integer. So, change it to:
#if (Auth::id() === $image->user_id)
Also, you should use policies for this:
#can('delete', $image)
I have an a tag that works as a delete button for an image, however, in my blade template I display the button only to the uploader of the image or the admins of the website. Unfortunately, when a user is not logged, I get an error because Auth::user()->hasRole('Admin') can not check if the user has role of admin. Any idea how am I supposed to avoid this problem?
The error:
"Call to a member function hasRole() on null"
My code
<div class="wrapper">
<div class='imageContainer'>
<h1 class='imageTitle'>{{$image->name}}</h1>
<div class="stickyImageContainer">
<img class='uploadedRealImage' src='/storage/images/uploaded/{{$image->file_name}}' alt='Random image'/>
#if (Auth::id() === $image->user_id || Auth::user()->hasRole('Admin'))
<div class='deleteImageButton'></div>
<a class='deleteImageA' href='{{ route('deleteImage', ['image_id' => $image->id]) }}'>X</a>
#endif
</div>
</div>
</div>
You need to check if Auth::user() returned a model or just null first:
#if (Auth::id() === $image->user_id || (Auth::user() && Auth::user()->hasRole('Admin')))
It's probably cleaner to break these into two nested :
#if(Auth::user())
#if(Auth::id() === $image->user_id || Auth::user()->hasRole('Admin'))
or even better, use Laravel's authorization gates to encompass this logic, which will allow you to do:
#can('delete', $image)
Use auth()->check() prior to attempting to access the user's role.
#if(auth()->check() && Auth::user()->hasRole('Admin'))
....
#endif
Laravel also offers the can and cannot blade directives. See: https://laravel.com/docs/5.5/authorization#via-blade-templates
I have a <h1> tag in my template blade, its duplicate in all pages of my website. I want to do a condition if the page is Homepage then use this <h1> else don't write it.
This is my header code
<div class="col-sm-7">
<div class="slogan">
<h1 id="h1" class="tlt">my h1 Tag</h1>
<span id="span" class="tlt">" {{$slogan[0]->text}} "</span>
</div>
</div>
Thanks
You can use Request::is('/'), given that the "homepage" is on this url. The is() method can also accept wildcards which can be useful for stuff like Request::is('admin/*');.
If you have a route name for your routes, you can do like this in your view:
Route:
Route::get('/', ['as'=>'home', 'uses'=>'HomeController#index']);
In view:
#if(request()->route()->getName() == 'home')
// <h1> Text for home page </h1>
#else
//text for other pages.
#endif
There are two type of packages that a customer can choose from: Phone packages or Broadband & Phone Packages.
When user ready to place an order, it will show a summary view with package name information and the cost. Some Summary information does not need to show if they select Phone package or Broadband & Phone Package.
Is there a better way to improve the readability and maintainable? For example
order-sidebar.blade.php
Note: This is small example of block of code. In the real application it is quite large with a lot of if statement broadbandphone or phone
#if ($summary['service'] == "phone" || $summary['service'] == "broadbandphone")
<div class="x5">
<h4>Phone Line x <span class="summary-lines">{{$summary['lines']}}</span></h4>
<ul class="clearfix">
#if ($summary['service'] == "phone")
<li>
<p class="x5-details">
#if ($summary['line_type'] == "newline")
New Line(s)
#endif
#if ($summary['line_type'] == "switch")
Switch line(s)
#endif
</p>
</li>
#endif
#if ($summary['service'] == "phone" && $summary['lines'] > 1)
<li>
<p class="x5-details">{{$summary['linesWithMulitpleNumbers']}}</p>
</li>
#endif
<li>
<p class="x5-details">{{$summary['package']->name}}</p>
<p class="x5-price">£{{$summary['monthlyLinesCost']}}</p>
</li>
</ul>
</div>
#endif
Or should I do two separate files for summary view like order-sidebar-phone.blade.php and order-sidebar-broadbandphone.blade.php
So it would be something like this:
#if ($summary['service'] == "phone")
#include('sections.order-sidebar-phone')
#end
#if ($summary['service'] == "broadbandphone")
#include('sections.order-sidebar-broadbandphone')
#end
You code looks okay to me. However here are some tips that might help reducing if's or making your code more readable.
Shorthand ternary if
Sometimes a shorthand if looks cleaner and more readable:
#if($summary['service'] == "phone")
Foo
#else
Bar
#endif
Can be written as:
{{ ($summary['service'] == "phone" ? "Foo" : "Bar") }}
It's especially useful for just little pieces of text that change depending on a condition.
Indentation
I shouldn't even need to say that. Indent your code right (not saying you didn't in your question...) And your if statements will be less confusing ;)
Partials
While it is an option to split it up completely like you suggested, this can cause a lot of duplicate code...
But you can also split your files so they are not as big and don't contain so many if statements (even if the total is still the same it's better structured and clearer)
For example instead of:
{{-- ... --}
</div>
#if ($summary['newSetup'] == false)
<div class="installation">
<h4>Phone Line x <span class="summary-lines">{{$summary['lines']}}</span></h4>
<ul class="clearfix">
<li>
<p class="installation-details">Installation</p>
<p class="installation-price">£{{$summary['installationCharge']}}</p>
</li>
</ul>
</div>
#endif
<div class="off-costs bg-gray">
<div class="off-costs-header clearfix">
{{-- ... --}}
Put the installation part in it's own file and include it:
{{-- ... --}
</div>
#if ($summary['newSetup'] == false)
#include('installation')
#endif
<div class="off-costs bg-gray">
<div class="off-costs-header clearfix">
{{-- ... --}}