search

Home  >  Q&A  >  body text

Laravel错误:Call to a member function format() on string

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?

某草草某草草2822 days ago1302

reply all(2)I'll reply

  • PHPz

    PHPz2017-05-16 16:51:52

    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)

    reply
    0
  • ringa_lee

    ringa_lee2017-05-16 16:51:52

    • 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

    reply
    0
  • Cancelreply