我用的是拉维5.3
我有两个表(users和jadwals),当我想用datatables显示table jadwals时,得到了以下错误消息:
SQLSTATE[42S22]:未找到列:1054“on子句”中的未知列“jadwals.users\u id”(SQL:select*fromjadwals
left joinusers
onjadwals
users_id
users
id
order byusers
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();
爪牙
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');
}
Jadwals控制器
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'));
}
这里有什么问题?
最佳答案
您的用户有一个名称“users”。Laravel搜索具有用户id名称的列,因为表名是“users”。
更改此函数:Jawals模型上的用户和users模型上的jadwals。
使用:
在用户模型上:
public function jadwals()
{
return $this->hasMany('App\Jadwal', 'user_id', 'id');
}
在Jadwals模型上:
public function users()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
关于mysql - SQLSTATE [42S22]:找不到列:1054带有数据表的未知列,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50215370/