问题描述: 所以我现在已经尝试了很多东西而且我无法让它发挥作用。使用当前代码,我得到"调用成员函数thisyear()on null"在线#34;
控制器:
public function index()
{
$rows = SeatRows::all();
return view('seating.index')
->withRows($rows);
}
SeatRows模型:
class SeatRows extends Model {
protected $table = 'seat_rows';
public function seats() {
return $this->hasMany('Seats', 'row_id');
}
}
席位型号:
class Seats extends Model {
protected $table = 'seats';
protected $fillable = [
'name',
'slug',
'row_id',
];
function row() {
return $this->hasOne('SeatRows', 'id', 'row_id');
}
function reservation() {
return $this->hasOne('SeatReservation', 'seat_id', 'id');
}
}
SeatReservation模型:
class SeatReservation extends Model {
function seat() {
return $this->hasOne('Seats', 'id', 'seat_id');
}
function reservedfor() {
return $this->hasOne('User', 'id', 'reservedfor_id');
}
function reservedby() {
return $this->hasOne('User', 'id', 'reservedby_id');
}
public function scopeThisYear($query) {
return $query->where('year', '=', \Setting::get('SEATING_YEAR'));
}
}
查看:
@foreach($rows as $row)
<li class="seat-row">
<ul class="seat-row-{{$row->slug}}">
@foreach($row->seats as $seat)
<li class="seat @if($seat->reservation->thisyear()->count() > 0) @if($seat->reservation->thisyear()->status->id == 1) seat-reserved @elseif($seat->reservation->thisyear()->status->id == 2) seat-tempreserved @endif @if(Sentinel::getUser()->id == $seat->reservation->thisyear()->first()->reservedfor->id and $seat->reservation->thisyear()->first()->status->id == 1) seat-yours @endif @endif @if(Request::segment(3) == $seat->slug) active @endif ">
<p>
@if($seat->reservation->thisyear()->count() == 0)
<a href="{{ route('seating-show', $seat->slug) }}" data-toggle="tooltip" title="Available">{{ $seat->name }}</a>
@elseif(Sentinel::getUser()->id == $seat->reservation->thisyear()->reservedfor->id and $seat->reservation->thisyear()->status->id == 1)
<a href="{{ route('seating-show', $seat->slug) }}" data-toggle="tooltip" title="This seat is reserved for you!">{{ $seat->name }}</a>
@elseif($seat->reservation->thisyear()->status->id == 1)
<a href="{{ route('seating-show', $seat->slug) }}" data-toggle="tooltip" title="Reserved for: {{ User::getUsernameAndFullnameByID($seat->reservation->thisyear()->reservedfor->id) }}">{{ $seat->name }}</a>
@elseif($seat->reservation->thisyear()->status->id == 2)
<a href="{{ route('seating-show', $seat->slug) }}" data-toggle="tooltip" title="Temporary Reserved By: {{ User::getUsernameAndFullnameByID($seat->reservation->thisyear()->reservedfor->id) }}">{{ $seat->name }}</a>
@else
{{ $seat->name }}
@endif
</p>
</li>
@endforeach
</ul>
</li>
@endforeach
答案 0 :(得分:1)
在您的关系和查询定义中,某些东西看起来很不可思议。在我看来,Seats有很多SeatReservations(每年一个)而不是hasOne。
此外,您的代码可能无效,因为特定席位可能没有预订,在这种情况下,您永远不会到thisyear()
查看计数。
我认为您需要改变您对关系的称呼方式。请参阅:Laravel. Use scope() in models with relation
这样的事可能有用:
class Seats extends Model {
function reservationThisYear() {
return $this->hasMany('SeatReservation', 'seat_id', 'id')->thisYear();
}
}
另外,请勿混用->count()
和->id
。计数仅存在于返回数组中。如果hasOne不存在而不是0,则返回null。由于我们现在使用的是hasMany,您可以使用$seat->reservationThisYear->count()
,但是您需要使用$seat->reservationThisYear[0]->reservedfor->id
。< / p>
注意:如果reservedfor关系与任何行不匹配,则使用$seat->reservationThisYear[0]->reservedfor->id
将导致PHP错误。 (IE不保留座位。)