How to include an HTML button inside Laravel code - php

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>

Related

Laravel if statement in form generator to select checkbox

I want to automatically select a checkbox when the field in the database is filled in.
I am trying to use #if statement in the Form generator but it is not checking the checkbox.
Here is the code I am using:
{!! Form::checkbox('offer_made', 'offer_made', #if(empty($phase_2->offer_made)) 1 #endif) !!}
Im sending this over to the view in my Controller:
public function show(Order $order)
{
$order = Order::where('id', $order->id)->first();
$current_phase = $order->current_phase;
if($current_phase == 1) {
$phase_2 = Order_Phase_2::where('order_id', $order->id)->first();
return view('orders.phase-2', compact('order', 'phase_2'));
}
}
When I echo $phase_2->offer_made in the view it shows 1 so the value is coming through but the if statement is not working inside the Form builder.
Anyone knows how to fix this?
Thanks already!
You might be checking for the value incorrectly:
#if(empty($phase_2->offer_made)) 1 #endif)
This outputs a 1 if the value is empty. You should be checking for !empty() if I understand the field correctly.
So you might have success with this:
#if(!empty($phase_2->offer_made)) 1 #else 0 #endif)
Is the value 1/0 itself? Use $phase_2->offer_made directly as the third parameter.
use ternary operator as you cannot use blade syntax in php code. Try something like this:
{!! Form::checkbox('offer_made', 'offer_made',(!empty($phase_2->offer_made)) ? 'checked' : '') !!}

How to use if statement to compare value from a database in laravel?

I want to change the look of a label when the result from the database is equal a certain word. for instance. In my application when full payment is made, i want to change the appearance using bootstrap class. similarly, i want to change it when the user is owing.
This is what i did but it seem not to be working.
This is the error am getting syntax error, unexpected '<'
#if( {{$data -> status}} == "Full Payment" )
<label class="btn btn-primary">{{$data -> status}}</label>
#endif
#if( {{$data -> status}} == "owing" )
<label class="btn btn-danger">{{$data -> status}}</label>
#endif
how do i change the appearance depending on the results from the database
You don't need {{}} braces when you use blade tags (#), since PHP is already parsed inside them. This way when your code gets to first brackets ( {{ ) it renders them as <?php, and you get the syntax error. Change your conditions to this:
#if($data->status == "Full Payment" )
<label class="btn btn-primary">{{$data->status}}</label>
#endif
#if($data->status == "owing" )
<label class="btn btn-danger">{{$data->status}}</label>
#endif
What i would recommend doing if those are the only 2 possible outcomes is:
<label class="btn {{ $data->status == 'Full Payment' ? 'btn-primary' : 'btn-danger' }}">{{$data -> status}}</label>
You have to use like this
#if($data -> status == "Full Payment")
And
#if($data -> status == "owing")

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

if else statement from database record laravel 5.2

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

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