Laravel assumes the database table is the plural form of the model name

前端 未结 4 1492
南笙
南笙 2021-01-11 14:09

By default, Laravel is assuming that the database table is the plural form of the model name. But what if my table name is \"news\" and i still want to use this feature? sho

4条回答
  •  感情败类
    2021-01-11 14:57

    If you have a model ending with the letter 's', it will keep the table name the same. In your case, your model will name your table news by default.

    If you want to use another name though, you can use:

    protected $table = 'tablename';
    

    inside of your model.

    EDIT: I tested this in my application. I made a model named News. Then I made a new instance of News and retrieved the table name:

    $news = new News();
    dd($news->getTable());
    

    It returns: news

提交回复
热议问题