【发布时间】:2021-12-21 15:04:02
【问题描述】:
大家好,我只是将我的查询传递给通知刀片,但它给出了错误。我不知道我在下面的代码中做错了什么。如果你们解决了这个问题,我会很高兴。提前致谢
Notification seen model
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class notificationseen extends Model
{
use HasFactory;
protected $table = 'notificationseens';
public function Notification()
{
return $this->belongsTo(Notification::class, 'notificationID');
}
}
通知模型
<?php
namespace App\Models\Backend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
use HasFactory;
protected $table = 'notifications';
public function notificationseen()
{
return $this->belongsTo(notificationseen::class, 'notificationID');
}
}
查看刀片
@foreach( $notification as $notify )
@if($notify->Notification->seen == 0)
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}" id="notifysee" data-id="{{ $notify->id }}">
<div class="alert unread custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
@else
<!-- Single Notification --><a href="{{ route('singlenotify', $notify->id) }}">
<div class="alert custom-alert-3 alert-primary" role="alert"><i class="bi bi-bell mt-0"></i>
<div class="alert-text w-75">
<h6 class="text-truncate">{{ $notify->name }}</h6><span class="text-truncate">{{ $notify->description }}</span>
</div>
</div></a>
@endif
@endforeach
表结构
$table->increments('id');
$table->integer('userid');
$table->integer('notificationID');
$table->integer('seen')->default('0')->comment("0 for unseen 1 for seen");
$table->timestamps();
你能帮帮我吗?我看不到任何问题,但我的错误是“尝试在 null 上读取属性“已见””
【问题讨论】:
-
两个模型都返回
belongsTorealtionship。Noticiation model必须包含您的关系,无论它是hasOne/hasMany/or morph...。return $this->hasMany(notificationseen::class, 'notificationID');在Notification model中应该没问题documentation
标签: laravel model foreign-keys relationship relation