class Device extends Model {
protected $table ="devices";
protected $fillable = ['name','description','serial','ip','x','y','level','map_id','status_id'];
function map(){
return $this->belongsTo('Map');
}
public function statu()
{
return $this->hasOne('AppStatus');
}
}
//型号状态
class Status extends Model
{
public function device()
{
return $this->belongsTo('AppDevice');
}
}
模型地图
class Map extends Model
{
protected $fillable= ['name','path','width','height'];
public function devices()
{
return $this->hasMany('AppDevice');
}
}
设备控制器
public function index()
{
return view('devices.index',[
'devices' => Device::all(),
'maps' => Map::all(),
'status' => Status::all()
]);
}
内刀片
<table id="table_devices" class="table table-striped table-bordered">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Description</th>
<th scope="col">Serial</th>
<th scope="col">IP</th>
<th scope="col">x</th>
<th scope="col">y</th>
<th scope="col">Level</th>
<th scope="col">Map</th>
<th scope="col">Status</th>
</tr>
</thead>
<tbody>
@foreach ($devices as $device)
<tr>
<td>{{ $device->id }}</td>
<td>{{ $device->name }}</td>
<td>{{ $statu->description ? $statu->description : '--' }}</td>
<td>{{ $device->serial }}</td>
<td>{{ $device->ip }}</td>
<td>{{ $device->x }}</td>
<td>{{ $device->y }}</td>
<td>{{ $device->level }}</td>
<td>{{ $device->map->name }}</td>
<td>{{ $device->statu->name }}</td>
</tr>
@endforeach
</tbody>
</table>
更改映射关系,'AppMap'
或Map::class
它适用于地图,但我得到了关于显示名称 statu 的新错误
SQLSTATE[42S22]:找不到列:1054 "where 子句"中的未知列"statuses.device_id"(SQL:从statuses
中选择 *statuses
.device_id
= 2 和statuses
.device_id
不是空限制 1( (查看:C:\xampp\htdocs\gel_back_end\resources\views\devices\index.blade.php(