小编Rak*_*mah的帖子

如何修复它在laravel 5.3中调用字符串上的成员函数diffForHumans()

为什么当我使用查询生成器而不是函数diffForHumans()时出现错误,但如果我使用ELoquent ROM但没有错误,有办法克服它?(我该怎么办呢)谢谢

diffForHumans()

这是ArticlesController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Article;
use Carbon\Carbon;
use Auth;
use DB;

class ArticlesController extends Controller
{

    public function index()
    {
        $articles =DB::table('articles')->get();
        $articles = ['articles'=>$articles];
        return view('articles.index',$articles);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是模型Article.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;

class Article extends Model
{
    //
    use SoftDeletes ;

    protected $fillable = [
        'user_id','content','live','post_on',
    ];

    protected $dates =[
        'post_on','deleted_at','created_at','updated_at',
    ];

    public function setLiveAttribute($value)
    {
        $this->attributes['live'] = (boolean)($value);
    }

    public function getShortContentAttribute()
    {
        return substr($this->content,0,random_int(60,150))."..."; …
Run Code Online (Sandbox Code Playgroud)

php mutators laravel laravel-5.3

5
推荐指数
2
解决办法
7848
查看次数

标签 统计

laravel ×1

laravel-5.3 ×1

mutators ×1

php ×1