if else statement from database record laravel 5.2 - php

i want to create if else from database record.
for example : i've record "1" in "status" row.
"if status = "1"
echo Show Button
else
don't show button"
what code in controller and view?

Just put in the view your if
#foreach ($itens as $item)
#if ($item->status == 1)
<input type="button"/>
#endif
#endforeach
In your controller you don't need to care about it

You can use Laravel Blade Template Control Structures for this
i.e.
#if($status == 1)
...
<!-- HTML Code to generate button -->
...
#endif
Read more here

Related

Return data to the view in LARAVEL

So I do have a table "words" and it has 2 columns "word" and "countofletters"
When I get them in the controller, how can I return them to the view based on the count, basically the view should contain something like:
4 letter words:
1- climb
2- Play
3 letter words:
1- put
2- cut
And so on..
Return from controller
You can send data to the view with compact instruction :
$yourDataFetchedFromDB = "";
return view('myviews.view', compact('yourDataFetchedFromDB'));
In your view
You can now access the value of $yourDataFetchedFromDB in your view as if you were in your controller.
Now you can use something like :
<ul>
#foreach($yourDataFetchedFromDB as $row)
<li>$row->word</li>
#endforeach
</ul>
And to use count columns you can use :
$yourDataFetchedFromDB[0]->countofletters
Full view code
<h2>{{ $yourDataFetchedFromDB[0]->countofletters }}</h2>
<ul>
#foreach($yourDataFetchedFromDB as $row)
<li>$row->word</li>
#endforeach
</ul>
Note : Obviously you should check if $yourDataFetchedFromDB[0] is not empty before using it

Prevent foreach from loop html tag multiple time in blade laravel

I'm trying to loop data using foreach and I want to skip the html tag after the first item being looped.
I've tried like the code bellow, but the html tag <p> still being looped multiple time. What I want is the <p> tags only looped once
#foreach ($store_icon as $key => $icon)
#if ($key < 1)
<p class="available-at">Also available at:</p>
#endif
#endforeach
Result example:
What I want is like this:
Also Available at
- Product 1
- Product 2
But using the code above it resulted like this:
Also Available at
- Product 1
Also Available at
- Product 2
Thanks
I am not sure why it is not working, It must work and working the same code for as well, you can try --
#foreach ($store_icon as $key => $icon)
#if ($key == 0)
<p class="available-at">Also available at:</p>
#endif
#endforeach
OR you can use like this
#if(!empty($store_icon))
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach
#endif
Whats problem? Just take it out from loop.
<p class="available-at">Also available at:</p>
#foreach ($store_icon as $key => $icon)
#endforeach

How to include an HTML button inside Laravel code

I want to show a button if a condition is not true. In the code below I need to show the "follow group" button if the output is not true. It is working fine, but I don't know the syntax to put the button inside the Laravel code.
{{ Auth::user()->grpusers()->where('group_id', $group->id)->first() == True ? 'You are following this group' : Follow Group }}
You can simply use if else
<p>
#if( Auth::user()->grpusers()->where('group_id', $group->id)->first() == True)
You are following this group
#else
Follow Group
#endif
</p>

How can I check auth user role in laravel?

I have role column in users table, and I want to check the value like this in the blade file :
#if ( {{Auth::user()->role }} == '1')
// do something
#endif
Is it possible ?
In blade files, you need to write plain PHP into the #if and others blade statements. So you would need to remove the {{ }}:
#if ( auth()->user()->role == 1)
// do something
#endif
I think you can extend a blade.
https://laravel.com/docs/5.3/blade#extending-blade
It's cool and convenient.
Latest version of Laravel will work with like this. You don't need to use {{}} here.
#if ( Auth::user()->role == 1)
// do something
#endif
#if(\Illuminate\Support\Facades\Auth::user()->hasRole('Admin') == 'Admin')
// do something
#endif

Using Laravel 4, how do I mark a radio button as checked if a session key is a specified value?

I have a multi-page form with two radio buttons with the same name attribute. When I select one and click the next step button, I save the value of that radio button into a session array with the form field name and chosen value. If the user comes back to the page, I want the previously chosen radiobutton to be checked.
This is what I came up with:
View: choose-listing-type.blade.php
<div class="form-group">
<?php $checked_status = Session::get('listing_form_data.type') === 'property' ? true : false; ?>
{{ Form::radio('type', 'property', $checked_status) }} Create Property Listing
</div>
<div class="form-group">
<?php $checked_status = Session::get('listing_form_data.type') === 'room' ? true : false; ?>
{{ Form::radio('type', 'room', $checked_status) }} Create Room Listing
</div>
This works, but it seems sloppy. First off, I don't think the if statement that checks the session value should be in the view, and I would love to find a way to do this in blade.
Using Laravel 4, what is the best practice to mark a radiobutton as checked depending on the value of a specified session key?
Since you mentioned you wanted to do it in the controller:
$type = Session::get('listing_form_data.type');
return View::make('view')->with('type', $type);
View:
{{ Form::radio('type', 'property', $type === 'property') }} Create Property Listing
{{ Form::radio('type', 'room', $type === 'room') }} Create Room Listing
Or even:
$type = Session::get('listing_form_data.type');
$isProperty = ($type === 'property');
$isRoom = ($type === 'room');
return View::make('view')->with(compact('isProperty', 'isRoom'));
View:
{{ Form::radio('type', 'property', $isProperty) }} Create Property Listing
{{ Form::radio('type', 'room', $isRoom) }} Create Room Listing
Why don't you just put the conditional right inline with the Form helper, like this:
<div class="form-group">
{{ Form::radio('type', 'room', (Session::get('listing_form_data.type') === 'room') ? true : false) }} Create Room Listing
</div>
Personally I don't see anything wrong with checking a session setting from the view...

Categories