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

Rak*_*mah 5 php mutators laravel laravel-5.3

为什么当我使用查询生成器而不是函数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))."...";
    }

    public function setPostOnAttribute($value)
    {
        $this->attributes['post_on'] = Carbon::parse($value);
    }

    public function setCreatedAtAttribute($value)
    {
        $this->attributes['post_on'] = Carbon::parse($value);
    }



}
Run Code Online (Sandbox Code Playgroud)

这是我的代码,我怎么能解决它?谢谢

yoe*_*nes 8

我注意到你的代码中有几件事,

  • 不需要演员created_atupdated_at日期,他们已经被铸造并且是实例Carbon
  • 你可以使用该属性$casts来构建简单的attributslive
  • 无需为post_on日期添加mutator,因为您将其添加到$dates
  • 你也设置了一个mutator created_at而不是post_on,你用SetCreatedAttribute而不是setPostOnAttribute
  • 而不是substr($this->content,0,random_int(60,150))."..."你可以使用str_limitLaravel的辅助函数,也random_int改为rand=>int rand ( int $min , int $max )

查询构建器返回dates为字符串,您需要在使用它们之前进行解析,否则您将收到类似这样的错误PHP error: Call to a member function on string,只需这样做:

\Carbon\Carbon::parse(\DB::table('articles')->first()->post_on)->diffForHumans()
Run Code Online (Sandbox Code Playgroud)

你可以让你的模型更简单:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
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'
    ];

    protected $casts = [
        'live'  => 'boolean'
    ];

    public function getShortContentAttribute()
    {
        return str_limit($this->content, rand(60,150));
    }
}
Run Code Online (Sandbox Code Playgroud)

你也可以index像这样简化你的方法:

public function index()
{
    $articles = DB::table('articles')->get();

    return view('articles.index', compact('articles'));
}
Run Code Online (Sandbox Code Playgroud)


Saz*_*man 5

默认情况下,eloquent将date/time字段作为Carbon实例返回,而查询构建器则不返回.因此,如果要Carbon在查询生成器的返回属性上使用,则必须使用Carbon::parse()方法包装该属性.

例如,我可以在雄辩的结果上使用其中一种Carbon方法toCookieString(),例如

echo App\User::find(1)->created_at->toCookieString();
Run Code Online (Sandbox Code Playgroud)

这将给出这样的回应Thursday, 10-Aug-2017 17:59:53 UTC.但是如果我使用查询构建器而不是

echo DB::table('users')->find(1)->created_at->toCookieString();
Run Code Online (Sandbox Code Playgroud)

那么它会给出一个错误

在字符串上调用成员函数toCookieString()

Carbon在这里使用我用Carbonparse()方法包装属性.

echo Carbon\Carbon::parse(DB::table('users')->find(1)->created_at)->toCookieString();
Run Code Online (Sandbox Code Playgroud)

这将以雄辩的方式给出理想的结果.