How to put HTML inside a <td> in Laravel Blade? - php

I want to put Bootstrap Icon inside of my blade..But the problem is i couldn't get the actual output..Means here is the sample code:
#if (($row->is_management) === 1)
<td> <span class="glyphicon glyphicon-ok"></span> </td>
#else
<td><span class="glyphicon glyphicon-remove"></span></td>
#endif
Means if status is 1 then green checked will come and in another condition red cross.
Can anyone suggest me what the actual mistake here?

It will work on bootstrap css with glyphicons fonts. Please make sure that u have bootstrap css called and have glyphicons fonts

#if (($row->is_management) == 1)
<td> <span class="glyphicon glyphicon-ok"></span> </td>
#else
<td><span class="glyphicon glyphicon-remove"></span></td>
#endif
Try this, use == instead of ===
Updated Code:
#if (($row->is_management) == 1)
{{ <td> <span class="glyphicon glyphicon-ok"></span> </td> }}
#else
{{ <td><span class="glyphicon glyphicon-remove"></span></td> }}
#endif

Related

load colors from the database and list with color in the background

enter #foreach ($colors as $color)
<tr>
<th scope="row">{{ $color->code }}</th>
<td>{{ $color->name }}</td>
<td>
<div class="col-md-6 cor"></div>
<style>
.cor {
background-color: {{ $color->code }};
}
</style>
</td>
<td><span class="badge badge-success">Active</span></td>
<td>
<a href="{{ URL::route('edit.customer', array('customer'=>$color->id_color)) }}" class="text-success mr-2">
<button class="nav-icon i-Pen-2 font-weight-bold btn btn-warning m-0" type="button" data-toggle="tooltip" data-placement="top" title="Editar Cliente"></button>
</a>
<a href="{{ URL::route('delete.customer', array('customer'=>$color->id_color)) }}" class="text-danger mr-2">
<button class="nav-icon i-Close-Window font-weight-bold btn btn-danger m-0" type="button" data-toggle="tooltip" data-placement="top" title="Eliminar Cliente"></button>
</a>
</td>
</tr>
#endforeach here
I want to load color codes from the database, I want the color that is recorded to appear to the user, this foreach loads me the last color in all
You're generating the CSS rule
.cor {
background-color: {{ $color->code }};
}
multiple times, but with a different colour each time. So whatever the last definition is, it just overrides all the previous versions. Think about it - if you declare something with the same name multiple times, how do you expect the browser to tell them apart or know which one to apply to which element? It can't do that. It can only use one version, so it just uses the last one you created.
Since the colour will be unique to each copy of the div, you'd be better off just writing
<div class="col-md-6" style="background-color: {{ $color->code }}"></div>
instead.

(2/2) ErrorException Trying to get property of non-object

In course table have department field where the value is number and when i get data form this table i don't want to show number.I want to show department name but i can not do it i want expert help.I have attached picture and codethis is course table.
Controller code :
public function index()
{
$allCourse = Course::paginate(10);
return view ('Admin.course.index',['allCourse'=> $allCourse]);
}
index.blade.php
#foreach($allCourse as $course)
<tr class="info">
<td class="success">{{$course->id}}</td>
<td class="success">{{$course->code}}</td>
<td class="success">{{$course->name}}</td>
<td class="success">{{$course->credit}}</td>
<td class="success">{{$course->description}}</td>
<td class="success">{{$course->department->$department}}</td>
<td class="success">{{$course->semester}}</td>
<td class="success">
<div class="btn btn-success">
<i class="fa fa-pencil" aria-hidden="true"></i>
</div>
<div class="btn btn-danger">
<a class="delete_link" href="{{ url('/Admin/course',[$course->id,'delete']) }}"><i class="fa fa-trash" aria-hidden="true"></i></a>
</div>
{{-- {!! Form::open(['url' => 'foo/bar']) !!}
<div>
{{ Form::button('<i class="fa fa-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger '] ) }}
</div>
{!! Form::close() !!}--}}
</td>
</tr>
#endforeach
</table>
{{ $allCourse->links() }}
</div>
</div>
</div>
#endsection
error page
Make sure you have Relation between course and department
You can find the documentation here
Secondly, you have a typo in your code. Change this to
<td class="success">{{$course->department->$department}}</td>
this
<td class="success">{{$course->department->department}}</td>
Hope this helps.
Eloquent just fetch everything inside your specified table. If you want department name (i assume you have department table), you need to define the relationship. Eloquent do have method for defining relationship.
For me, just use fluent 'join'.
Course::join('department_table','Course.department','=','department_table.department_id')->paginate(10);
Once you change your query to this, change from
{{$course->department->$department}}
to
{{$course->department_name}}
This is the easier way, but if you are willing to study, have a look here https://laravel.com/docs/5.5/eloquent-relationships

laravel - if else loop not working proper

I'm trying to write an if-else loop: if there is an embed saved in the database it should display a calendar, if not it should display a button saying "add new calendar/add new embed", but it's not working. I can't figure out why. When there is an embed saved in the database it displays the embed but, if there isn't an embed it doesn't display the button to add a new calendar/embed:
Blade view:
#if (empty($calendars))
#else
<h4>No calendar settings</h4><br>
<a href="{{ route('calendarsettings.create')}}">
<button class="btn btn-success btn-cons m-b-10" type="button">
<i class="fa fa-cloud-upload"></i>
<span class="bold">Add embed</span>
</button>
</a>
#endif
#foreach($calendars as $calendar)
{{ $calendar->embed }}
#endforeach
Controller:
public function kalendarz() {
$calendars = Calendar::with('users')->where('user_id', Auth::user()->id)->get();
return view('kalendarz',['calendars'=>$calendars]);
}
In Laravel 5.4, I would probably use the #unless blade directive, that will show the block, unless a condition is met. What the following code would acheive is "show the entries if we have some, and show the button unless we have some entries".
#if($calendar)
// show entries
#endif
#unless(empty($calendars))
// show a button to add a new one
#endunless
It kind of is the same "logic" as with an else, but somehow I think it's more clear and easy to read than an #else statement, and make it separate from the if.
The correct syntax is:
#if(empty($calendars))
<h4>No calendar settings</h4></br>
<a href="{{ route('calendarsettings.create')}}">
<button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span></button>
</a>
#else
#foreach($calendars as $calendar)
{{ $calendar->embed }}
#endforeach
#endif
empty() functions empty the variable $calendars, so try using count()
#if(count($calendars) > 0)
<h4>No calendar settings</h4></br>
<a href="{{ route('calendarsettings.create')}}">
<button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span></button>
</a>
#else
#foreach($calendars as $calendar)
{{ $calendar->embed }}
#endforeach
#endif
When using Eloquent to get results from a database you get an instance of Illuminate\Database\Eloquent\Collection. To check if that collection is empty you use $collection->isEmpty() or $collection->isNotEmpty() (L5.4).
#if ($calendars->isNotEmpty())
// Have entries.
#else
<h4>No calendar settings</h4></br>
<a href="{{ route('calendarsettings.create')}}"><button class="btn btn-success btn-cons m-b-10" type="button"><i class="fa fa-cloud-upload"></i> <span class="bold">Add embed</span>
</button></a>
#endif
#foreach($calendars as $calendar)
{{$calendar->embed}}
#endforeach

if else condition in blade file (laravel 5.3)

I want to check if/else condition in my blade file. I want to check the condition $user->status =='waiting' as the code given below. Output returns correctly as I expected. But along with my output I caught curly braces {} printed. I want to remove curly braces in result. Is there anything wrong in my if condition ?
#if($user->status =='waiting')
{
<td><a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">Approve/Reject<a></td>
}
#else{
<td>{{ $user->status }}</td>
}
#endif
No curly braces required you can directly write
#if($user->status =='waiting')
<td><a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">Approve/Reject<a></td>
#else
<td>{{ $user->status }}</td>
#endif
I think you are putting one too many curly brackets. Try this
#if($user->status=='waiting')
<td>Approve/Reject </td>
#else
<td>{!! $user->status !!}</td>
#endif
You don't have to use braces in blade conditions, so your code would be like this:
#if($user->status =='waiting')
<td>
<a href="#" class="viewPopLink btn btn-default1" role="button" data-id="{{ $user->travel_id }}" data-toggle="modal" data-target="#myModal">
Approve/Reject
<a>
</td>
#else
<td>
{{ $user->status }}
</td>
#endif
If wants to assign value on basis of if else inside blade.
#if($shipping_fee >= 1)
#php
$shipping_fee = $any_variable1
#endphp
#else
#php
$shipping_fee = $any_variable2
#endphp
#endif

Laravel if/else syntax

How do i set different states of the icon? I want it to change between a liked/not liked icon. Can i do that? I don't completely understand the syntax. The btn-success class is for the liked icon, and btn-danger is for the unliked button.
#if(Auth::check())
#if( !$unblock )
<span data-id="{{ $key->id }}" data-like="Like" data-like-active="Remove Like" title="#if(!empty($likeUser)) Remove Like #else Like #endif" class="btn btn-xs pull-left #if(!empty($likeUser)) btn-success #else btn-danger #endif btn-like likeButton #if(!empty($likeUser)) active #endif">
<i class="Like-icon"></i>
</span>
#else
You're almost right just complete the condition after #else
Here is the place to learn more about blades and templates
It is as simple like regular PHP if..else condition
#if (Auth::check())
Authorized
#else
Nope
#endif
If you want to have additional conditions inside then
#if (Auth::check())
Authorized
//your additional conditions for authorized users
#else
Nope
#endif
#if (count($records) === 1)
I have one record!
#elseif (count($records) > 1)
I have multiple records!
#else
I don't have any records!
#endif

Categories