我有一个包含秒的表,我在其中插入在线时间(以秒为单位),
Carbon::parse($seconds)->forHumans();不允许我这样做,有一种方法可以解析秒数并将其传输给人类阅读吗?比如1小时还是2周?
发布于 2015-11-11 14:57:47
这应该会返回您想要的结果:
Carbon::now()->subSeconds($seconds)->diffForHumans();发布于 2015-11-11 15:10:54
试试这个:
以人类可读格式表示的碳时间
// $sec will be the value from your seconds table
echo Carbon::now()->addSeconds($sec)->diffForHumans();
// OR
echo Carbon::now()->subSeconds($sec)->diffForHumans();输出
// if $sec = 5
5 seconds from now我找到了这个有用的文档Carbon
希望这能对你有所帮助。
发布于 2019-12-12 04:41:01
1) php artisan make:中间件LastActivityUser
2)将此代码添加到中间件LastActivityUser中
<?php
namespace App\Http\Middleware;
use Closure;
use Auth;
use Carbon\Carbon;
use Cache;
class LastActivityUser
{
/**
* The authentication factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check()) {
$expiresAt = Carbon::now()->addSeconds(10);
Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
}
return $next($request);
}
}3)在您的用户模型中添加此函数
public function is_online() {
return Cache::has('user-is-online-' . $this->id);
}4)在(app\Http\Kernel.php)中声明
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\LastActivityUser::class, //Add this Line
]5)在您的模板刀片中
@if($user->is_online())
<span>On</span>
@else
<span>Off</span>
@endifhttps://stackoverflow.com/questions/33645372
复制相似问题