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

How to check if a Model connected to the table

我有一个用 php artisan make:model 生成的自定义模型。 我想知道我的自定义模型是否已连接到数据库中的 table,因为我尝试使用 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>

有什么方法可以检查我的模型是否连接到 table?

Laravel 默认情况下会尝试查找 table 其名称为蛇形复数模型名称 class..

例如:- 对于模型 "Flight" ,它将查找 table "flights"

但是您可以通过在模型

中指定 table_name 来阻止这种默认方法

例如:- protected $table_name = "my_flights"

希望这对您有所帮助