SQLSTATE [42S22]:未找到列:1054 未知列与数据表

SQLSTATE[42S22]: Column not found: 1054 Unknown column with datatables

我正在使用 laravel 5.3

我有 2 tables(用户和 jadwals),当我想显示 table 带有数据的 jadwals 时 tables 我收到此错误消息:

SQLSTATE[42S22]:未找到列:1054 'on clause' 中的未知列 'jadwals.users_id'(SQL:select * 来自 jadwals left join users on jadwals.users_id = users.id order by users.name asc limit 10 offset 0)

table 用户

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->bigInteger('nimnip')->unique();
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

table 贾德瓦尔斯

Schema::create('jadwals', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('kegiatan');
        $table->integer('ruangan_id')->unsigned();
        $table->integer('jam_mulai_id')->unsigned();
        $table->integer('jam_selesai_id')->unsigned();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
        $table->foreign('ruangan_id')->references('id')->on('ruangans')->onUpdate('cascade')->onDelete('cascade');
        $table->foreign('jam_mulai_id')->references('id')->on('jams')->onUpdate('cascade')->onDelete('cascade');
        $table->foreign('jam_selesai_id')->references('id')->on('jams')->onUpdate('cascade')->onDelete('cascade');

模范用户

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'nimnip', 'email', 'password',
];

public function jadwals()
{
    return $this->hasMany('App\Jadwal');
}

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

模特贾德瓦尔

public function users()
{
    return $this->belongsTo('App\User');
}

public function ruangans()
{
    return $this->belongsTo('App\Ruangan');
}

public function jams()
{
    return $this->belongsTo('App\Jam');
}

JadwalsController

    class JadwalsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request, Builder $htmlBuilder)
    {
        if($request->ajax())
        {
            $jadwals = Jadwal::with('users', 'ruangans', 'jams');
            return Datatables::of($jadwals)
                ->addColumn('action', function($jadwal){
                    return view('datatable._action', [
                        'model'             =>  $jadwal,
                        'form_url'          =>  route('jadwals.destroy', $jadwal->id),
                        'edit_url'          =>  route('jadwals.edit', $jadwal->id),
                        'confirm_message'   =>  'yakin mau menghapus ' . $jadwal->kegiatan . '?'
                        ]);
                })->make(true);
        }

        $html = $htmlBuilder
            ->addColumn(['data' => 'users.name',        'name' => 'users.name',         'title' => 'Nama Peminjam'])
            ->addColumn(['data' => 'kegiatan',          'name' => 'kegiatan',           'title' => 'Kegiatan'])
            ->addColumn(['data' => 'ruangans.ruangan',  'name' => 'ruangan.ruangan',   'title' => 'Ruangan'])
            ->addColumn(['data' => 'jams.jam',          'name' => 'jam.jam',           'title' => 'Jam Mulai'])
            ->addColumn(['data' => 'jams.jam',          'name' => 'jam.jam',           'title' => 'Jam Selesai'])
            ->addColumn(['data' => 'action', 'name' => 'action', 'title'=>'', 'orderable'=>false, 'searchable'=>false]);

            return view('jadwals.index')->with(compact('html'));
    }

这里有什么问题?

这些行表明 table 包含列名 user_id

$table->integer('user_id')->unsigned();
                 ^^^^^^^

$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
                 ^^^^^^^

正在执行的 SQL 语句引用的列名称 users_id 未出现在 table 定义中:

select *
  from `jadwals`
  left
  join `users`
    on `jadwals`.`users_id` = `users`.`id`
                  ^^^^^^^^
 order by `users`.`name` asc 

如前所述,您的 jadwals table 没有 users 列,它有 user 列。所以这应该可以正常工作:

SELECT * FROM jadwals 
LEFT JOIN users 
  ON jadwals.user_id = users.id 
ORDER BY users.name ASC 
LIMIT 10 
OFFSET  0

您的用户有一个名字 'users'。 Laravel 搜索名称为 users_id 的列,因为 table 名称为 'users'。 更改此功能:Jawals 模型上的用户和用户模型上的 jadwals。 有:

在用户模型上:

public function jadwals()
{
    return $this->hasMany('App\Jadwal', 'user_id', 'id');
}

在 Jadwals 模型上:

public function users()
{
    return $this->belongsTo('App\User', 'user_id', 'id');
}