dongtiaobeng7901 2016-09-20 07:17
浏览 13
已采纳

Laravel听起来像魔术

I just recently started experimenting with laravel, lovely. One thing l dont understand though is how laravel knows my table l just added a model and the model isnt the exact table name, but just how does it manage to get my table,

  • 写回答

3条回答 默认 最新

  • doulierong0334 2016-09-20 15:57
    关注

    Laravel follows a Naming convention for Eloquent Classes and Tables.

    From Laravel Website | Eloquent: Getting Started

    Note that we did not tell Eloquent which table to use for our Flight model. By convention, the "snake case", plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the Flight model stores records in the flights table.

    Eg.

    Class User will by default refers to Mysql Table users (Camel Case to Snake Case and Plural).

    Class NotificationsLog will by default refers to Mysql Table notifications_logs (Camel Case to Snake Case and Plural).

    But if you don't want to follow the convention then you can Mention the table name explicitly

    Eg. If I want my Class Plane should refers to flights table in Database then following code will work

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Plane extends Model
    {
         /**
         * The table associated with the model.
         *
         * @var string
         */
        protected $table = 'flights';
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数