【发布时间】:2016-09-30 18:19:45
【问题描述】:
我是 laravel 的新手,有人可以帮忙吗?
表格结构
1) rooms
id |roomno |roomtype_id |luxurytype |status
2) roomtypes
id| code| description
型号代码
1) 房型模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class roomtype extends Model
{
public function rooms(){
return $this->hasMany('App\room');
}
}
2)房间模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class room extends Model
{
public function roomtypes(){
return $this->belongsTo('App\roomtype');
}
}
控制器
public function index()
{
$roomtype = roomtype::all();
$rooms = room::all();
return view('home', array('roomtype'=>$roomtype , 'rooms'=>$rooms));
}
查看
@foreach ($rooms as $rooms)
<tr>
<td>{{ $rooms->roomno }}</td>
<td>{{ $rooms->luxurytype }}</td>
<td>{{ $rooms->roomtypes->description }}</td>
<td>{{ $rooms->status }}</td>
<td></td>
</tr>
@endforeach
错误
如果我使用这条线{{ $rooms->roomtypes->description }},那么我会得到:
试图获取非对象的属性
(查看:C:\xampp\htdocs\hms\resources\views\home.blade.php)
如何显示带有房间类型描述的房间?
【问题讨论】:
标签: laravel-5.3