【问题标题】:Laravel Relationship not working with belongsToLaravel 关系不适用于 belongsTo
【发布时间】: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 上读取属性“已见””

【问题讨论】:

  • 两个模型都返回belongsTo realtionship。 Noticiation model 必须包含您的关系,无论它是 hasOne/hasMany/or morph...return $this-&gt;hasMany(notificationseen::class, 'notificationID');Notification model 中应该没问题documentation

标签: laravel model foreign-keys relationship relation


【解决方案1】:

好的,有些事情:

  1. 我会在notificationseen 类中使用belongsTo,除非一个notificationseen 可以有多个Notifications 属于;)

  2. Notification 是否有多个 notificationseen 引用?我不这么认为,所以在您的Notification 中将引用更改为hasOne

  3. 在您的刀片中,使用 @if($notify-&gt;notificationseen-&gt;seen == 0) 或者您在控制器中调用更好的“Notification::with('notificationseen')->get()”,然后将其传递给您的视图。

【讨论】:

    【解决方案2】:

    你可以尝试添加一个isset来避免错误:

    @if(isset($notify->Notification->seen) && $notify->Notification->seen == 0)
       (...)
    @else
       (...)
    @endif
    

    编辑:在您的代码中,您定义了 2 个belongsTo 方法,但根据official Laravel documentation,您必须定义一个hasOne 方法及其相反的belongsTo 方法。

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 2019-05-26
      • 2020-01-15
      相关资源
      最近更新 更多