Laravel错误:Call to a member function format() on string
某草草
某草草 2017-05-16 16:49:52
0
2
1290

The data table has a expired_at field. The data is like this:


    public function store(Request $request)
    {
        $data=[
             'expired_at'=>Carbon::now()->addDays($publishing_days)->endOfDay()
        ];
        $article=Article::create(array_merge($request->all(),$data));
        
        return redirect('/artilces');
    }

view:

{{$article->expired_at->format('Y-m-d')}}

error:

Call to a member function format() on string (View: D:\wnmp\www\laravel-5-3-dev\resources\views\artiles\index.blade.php)

what happened?

某草草
某草草

reply all(2)
PHPzhong

This error message itself has told you that expire_at is a string, and there is no format method for strings in PHP (actually there is no method, because string is not an object in PHP)

左手右手慢动作
  • First expired_at needs to be of type timestamp 或者 datetime in the database

  • Secondly, add this field to $dates

  • in Model
class Article extends Model {
    protected $dates = ['expired_at'];

}

Then you can call it as Carbon

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template