使用Carbon格式化created_at datetime并在laravel blade中显示结果

时间:2017-03-22 15:54:45

标签: vue.js laravel-5.3 php-carbon

我正尝试在我的laravel刀片文件中使用a minute agoCarbon日期时间显示为vue.js。我使用public function data() { $projects = Project::get(); return [ 'created' => $projects->created_at->diffForHumans() ]; return \Response::json($projects); } 来显示数据但不起作用。

控制器:

<tr v-for="Project in Projects">
     <td>@{{ Project.id }}</td>
     <td>@{{ Project.thumb }}</td>
     <td>@{{ Project.name }}</td>
     <td>@{{ Project.active }}</td>
     <td>@{{ Project.created_at }}</td>
</tr>

有vue的刀片:

foreach ($projects as $project) {
     return [
          'created' => $project->created_at->diffForHumans()
     ];
}

错误:

  

未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ created_at

我也试过了:

public class OperatorOnFilter
{
    Dictionary<int, Tuple<int, object[]>> Operations = new Dictionary<int, Tuple<int, object[]>>();
    List<Delegate> lstOperations = new List<Delegate>();

    public OperatorOnFilter() { }

    public void AddOrReplace(int numTraitement, Delegate Action, params object[] Arguments)
    {
        if (!lstOperations.Contains(Action))
            lstOperations.Add(Action);

        if (Operations.ContainsKey(numTraitement))
            Operations[numTraitement] = new Tuple<int, object[]>(lstOperations.IndexOf(Action), Arguments);
        else
            Operations.Add(numTraitement, new Tuple<int, object[]>(lstOperations.IndexOf(Action), Arguments));
    }

    public void ApplyOperations(FilterManager Filter)
    {
        for (int i = 0; i < Filter.Values.Count(); i++)
            if (Operations.ContainsKey(i) && Filter.Values[i])
                lstOperations[Operations[i].First].DynamicInvoke(Operations[i].Second);
    }

}

public class FilterManager
{
    public bool[] Values;
    public FilterManager(int filtre)
    {
        List<bool> tmpList = new List<bool>();
        int i = 0;
        while (filtre >> i > 0)
        {
            tmpList.Add((filtre & (1 << i)) == (1 << i));
            i++;
        }
        Values = tmpList.ToArray();
    }
}

public class Tuple<T1, T2>
{
    public T1 First { get; private set; }
    public T2 Second { get; private set; }
    internal Tuple(T1 first, T2 second)
    {
        First = first;
        Second = second;
    }
}

public static class Tuple
{
    public static Tuple<T1, T2> New<T1, T2>(T1 first, T2 second)
    {
        var tuple = new Tuple<T1, T2>(first, second);
        return tuple;
    }
}

它会显示我要查找的内容,但它只显示一个数据。

3 个答案:

答案 0 :(得分:1)

您直接在刀片中书写以获得人类可读的格式。

{{\Carbon\Carbon::createFromTimeStamp(strtotime($Project.created_at))->diffForHumans()}}

答案 1 :(得分:1)

我得到了可以在任何具有铸造属性的地方使用的解决方案。检查下面的代码

<?php

namespace App\Casts;

use Carbon\Carbon;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;

class CreateAtCast implements CastsAttributes
{
    /**
     * Transform the attribute from the underlying model values.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  string  $key
     * @param  mixed  $value
     * @param  array  $attributes
     * @return mixed
     */
    public function get($model, string $key, $value, array $attributes)
    {
        $date = Carbon::parse($value)->diffForHumans();
        return $date;
    }

    /**
     * Transform the attribute to its underlying model values.
     *
     * @param  \Illuminate\Database\Eloquent\Model  $model
     * @param  string  $key
     * @param  mixed  $value
     * @param  array  $attributes
     * @return array
     */
    public function set($model, string $key, $value, array $attributes)
    {
        return $value;
    }
}

模态


<?php

namespace App;

use App\Casts\CreateAtCast;

class Project extends Model
{

    protected $casts = [
        'created_at'        => CreateAtCast::class,
    ];

    protected $dates = ['created_at', 'updated_at'];
}


然后直接使用project.create_at

答案 2 :(得分:1)

我不希望自己格式化create_at本身,而是在模型中创建它的副本,并随时随地使用它。

因此,在您的User.php模型中:

protected $appends = ['created_date'];

创建属性更改器:

public function getCreatedDateAttribute()
{
    return $this->created_at->diffForHumans();
}

样本输出

10 seconds ago