Diff for humans not working in laravel blade - laravel

Hi am trying to use diffForHumans in laravel blade but its not working.please help`enter code here
Heres my code
#foreach($students as #student)
$time=$student->created_at->diffForHumans()
#php echo $time;#endphp
#endforeach

$student->created_at is a string, hence you can't call diffForHumans() function on it. Instead, you should parse it via Carbon instead. Here's how to do it
#foreach($students as #student)
#php
$time= \Carbon::parse($student->created_at)->diffForHumans()
#endphp
//Now do what you need to do
#endforeach
Also, you can't just write php code inside blade. If you do that, you need to contain the code inside #php tags as shown above. Although, it's not really clean to write php code in blade. With that being said, if you just want to display the time, here's how I would do it
#foreach($students as #student)
<p>Created At : {{ \Carbon::parse($student->created_at)->diffForHumans() }}</p>
#endforeach

All You need is to parse the date using Carbon like this
{{ Carbon\Carbon::parse($student->created_at)->diffForHumans() }}

Related

Create anchor tag with route contain under #php script in blade template

Route is not working. How can I write an anchor tag based on some condition? If the condition is true then the anchor tag will be printed with the route. I wrote the code below, but I get an error.
In my blade template:
#php
if (SOMECONDATION) {
echo 'Approve';
}
#endphp
{{ }} is echo-ing. You can use this :
#if($p->name == 0)
Approve
#endif
No need to use echo or anything. Directly use #if #endif and for route use {{ }}
#if($condition)
Approve
#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]) }}

How to check if used paginate in laravel

I have a custom view and in some functions, I used paginate and other functions I don't use paginate. now how can I check if I used paginate or not ?
#if($products->links())
{{$products->links()}}
#endif // not work
of course that I know I can use a variable as true false to will check it, But is there any native function to check it ?
This works perfectly. Check if $products is an instance of Illuminate\Pagination\LengthAwarePaginator then display the pagination links.
#if($products instanceof \Illuminate\Pagination\LengthAwarePaginator )
{{$products->links()}}
#endif
#if($threads->hasPages())
{{ $threads->links() }}
#endif
Simple one!
Try like this
#if($products instanceof \Illuminate\Pagination\AbstractPaginator)
{{$products->links()}}
#endif
You need to check wheater the $products is instance of Illuminate\Pagination\AbstractPaginator. It can be an array or Laravel's Collection as well.
As of laravel 7 you can now do this:
#if( $vehicles->hasPages() )
{{ $vehicles->links() }}
#endif
The beautiful way:
#if ($products->hasMorePages())
{{ $products->links() }}
#endif
Click here to see the official documentation
Don't use a check on the base class of the variable. This could lead to problems with changing base classes in future Laravel versions. Simply check whether the method links exists:
#if(method_exists($products, 'links'))
{{ $products->links() }}
#endif
Corrected code (add "isset"):
#if(isset($products->links()))
{{$products->links()}}
#endif
Shorter version:
{{$products->links() ?? ''}}
It will work for paginate, simplePaginate and when there is no pagination. The solution with "$products->hasMorePages()" will not display pagination links on the last page.
Another way:
#if (class_basename($products) !== 'Collection')
{{ $products->links() }}
#endif
You can use PHP function: get_class($products) - to get full class name.
Laravel should have some function to check ->paginate() is in use.
just write paginate(0) instead of get()
Blade Template: simply use {{$products->links}}. no #if #endif needed.
laravel paginate have 2 type :
simplePaginate() will return \Illuminate\Pagination\Paginator
paginate() will return Illuminate\Pagination\LengthAwarePaginator
Based on the above conditions, you can try this solution :
#if(
$products instanceof \Illuminate\Pagination\Paginator ||
$products instanceof Illuminate\Pagination\LengthAwarePaginator
)
{{ $products->links() }}
#endif
Juse use below format
#if($products instanceof \Illuminate\Pagination\LengthAwarePaginator )
{{$products->links()}}
#endif
#if($products->currentPage() > 1)
{{$products->links()}}
#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) }}

How to add a tag (span, small) to Laravel Form::label?

In Laravel 4.1, I want to echo:
<label for="name">Name <small>required</small></label>
This doesn't work:
{{ Form::label('name','Name <small>required</small>') }}
are automatically converted to code text…
Is there a way or do I have to skip Form::label and do it manually?
If you want to use a Html tag inside another by Laravel one you should use HTML::decode.
Correct code :
{{ HTML::decode(Form::label('name','Name <small>required</small>')) }}
Try this:
{!! Html::decode(Form::label('name','Name <small>required</small>')) !!}

Resources