SQLSTATE [42S22]:找不到列:1054带有数据表的未知列

时间:2018-05-07 13:25:12

标签: mysql datatables laravel-5.3

我正在使用laravel 5.3

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

SQLSTATE [42S22]:未找到列:1054未知列' jadwals.users_id'在' on条款' (SQL:从jadwals选择*从users左侧加入jadwalsusers_id = usersidusers排序。 name asc limit 10 offset 0)

表用户

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();

表jadwals

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',
];

模特jadwal

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'));
    }

这里有什么问题?

3 个答案:

答案 0 :(得分:0)

这些行表示该表包含列名user_id

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

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

正在执行的SQL语句引用的列名users_id不会出现在表定义中:

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

答案 1 :(得分:0)

如前所述,您的jadwals表没有列用户 s ,它有列用户。所以这应该可行:

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

答案 2 :(得分:0)

您的用户有一个名字'用户'。 Laravel搜索具有users_id名称的列,因为表名是' 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');
}