为什么当我使用查询生成器而不是函数diffForHumans()时出现错误,但是如果我使用偶尔ROM但没有错误,如何克服它? (如何解决它)谢谢如何修复它调用成员函数diffForHumans()在laravel 5.3中的字符串
这是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);
}
}
这是模型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))."...";
}
public function setPostOnAttribute($value)
{
$this->attributes['post_on'] = Carbon::parse($value);
}
public function setCreatedAtAttribute($value)
{
$this->attributes['post_on'] = Carbon::parse($value);
}
}
,这是我的代码,我该如何修复它? 谢谢
ouh谢谢你,这是很好的建议,当我尝试你的建议,它的作品 –