I get some values from the database and I passed those values into view from the controller<\/strong> . now I want to use that data with some carbon function<\/code> in Laravel view<\/strong> .
foreach($customer as $time){
$create= $time->created_at;
$update= $time->updated_at;
$create_at_difference=\Carbon\Carbon::createFromTimestamp(strtotime($create))->diff(\Carbon\Carbon::now())->days;
}
它与我的 view.blade.php 的全局命名空间一起使用
{{ \Carbon\Carbon::parse($row->posted_at)->diffForHumans() }}
If you want to use the namespaced class, you don't need the first slash:
$create_at_difference=Carbon\Carbon::createFromTimestamp(strtotime($create))->diff(\Carbon\Carbon::now())->days;
You should just write Carbon\\Carbon instead of \\Carbon\\Carbon.
That is a quick solution. But, using classes directly in your views is a bad idea. You can add more functionality to your model by creating a function that will return current created at difference.
lets say you have Customer model, you can go that way:
use Carbon\Carbon;
class Customer extends Eloquent
{
public function created_at_difference()
{
return Carbon::createFromTimestamp(strtotime($this->created_at))->diff(Carbon::now())->days;
}
}
then in the view you can access this like:
@foreach($customers as $customer)
{{$customer->created_at_difference()}}
@endforeach
Another option, i think it's better put this line of code on top of your class:
namespace App\Http\Controllers
use Carbon\Carbon;
class MyController {
...
}
Faced same issue and this is what worked for me
$create = $time->created_at;
$create_at_difference = Carbon\Carbon::createFromTimestamp(strtotime($create))
->diff(\Carbon\Carbon::now())->days;
Use blade injections for the cleanest result. Example:
@inject('carbon', 'Carbon\Carbon')
...
<span>{{ $carbon::parse($model->created_at) }}</span>
不要重复\\Carbon\\Carbon
,只需尝试
\\Carbon::createFromTimestamp(strtotime($create))->diff(\\Carbon::now())->days
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.