如何检查模型是否连接到表

时间:2017-08-15 16:57:16

标签: php laravel laravel-5 model blade

我有一个用php artisan make:model生成的自定义模型。 我想知道我的自定义模型是否已连接到我的数据库中的表,因为我尝试使用Datatables显示该字段,但它没有显示该字段,并且我没有收到任何错误。

我的模特

<?php

namespace App\Modules\Hr\Models;

use App\Models\Model;
use DB;

class Employee extends Model
{
    protected $table = "employee";

    protected $primaryKey = "nik";

    public $incrementing = false;

    public $timestamps = false;

    protected $fillable = [
        "nik",
        "employee_name",
        "idaddress",
        "dom_address",
        "hpno",
        "email",
        "gender",
        "npwp",
        "birthdate",
        "identity_no",
        "join_date",
        "blood_type",
        "is_active",
        "end_date",
        "birthplace",
        "postalcode",
        "districts",
        "propcode",
        "citycode",
        "religioncode",
        "statuscode",
        "doc_npwp",
        "photo",
        "doc_id",
        //
    ];
}

我的控制器

public function index(){
    return view([
        "model" => new Employee,
    ]);
}

public function data(){
    return Datatables::of(Employee::select("*"))->make(true);
}

我的观点

<table class="table table-bordered" datatable="{{ url("hr/employee/data") }}">
  <thead>
    <tr>
      <th dt-field="nik"> {{ $model->label("nik") }} </th>
        {{-- <th dt-field="unit_kind"> {{ $model->label("unit_kind") }} </th> --}}
      <th dt-field="employee_name"> {{ $model->label("employee_name") }} </th>
      <th dt-field="idaddress"> {{ $model->label("idaddress") }} </th>
      <th dt-field="dom_address"> {{ $model->label("dom_address") }} </th>
      <th dt-col="#dt-action" sort="false" search="false"> </th>
    </tr>
  </thead>
</table>

有没有办法检查我的模型是否已连接到桌面?

1 个答案:

答案 0 :(得分:0)

Laravel默认会尝试查找一个名为snake case的表格,模型类的复数名称。

例如: - 对于模型“Flight”,它将查找表“flight”

但是,您可以通过在模型

中指定table_name来阻止此默认方法

例如: - protected $ table_name =“my_flights”

希望这会有所帮助