Home > Enterprise >  Laravel Relationship not working with belongsTo
Laravel Relationship not working with belongsTo

Time:11-09

Hello Guys, I am just passing my query to notification blade, but its gave error. I dont know what i did wrong with bellow code. If you guys fix this issue i will be very glad. Thanks in advance

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');
    }
}

Notification Model

<?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');
    }
}

View Blade

 @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 structure

 $table->increments('id');
            $table->integer('userid');
            $table->integer('notificationID');
            $table->integer('seen')->default('0')->comment("0 for unseen 1 for seen");
            $table->timestamps();

Can you please help me out. I cant see any issue but its me error "Attempt to read property "seen" on null"

CodePudding user response:

Ok, some things:

  1. I would use belongsTo in the notificationseen class unless one notificationseen could have more than one Notifications to belong to ;)

  2. Do a Notification have more than one notificationseen references? I do not think so, so in your Notification change the reference to hasOne.

  3. In your blade, use @if($notify->notificationseen->seen == 0) or you call better "Notification::with('notificationseen')->get()" in your controller and then pass it to your view.

CodePudding user response:

You can try to add an isset to avoid the error :

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

EDIT : in your code, you defined 2 belongsTo methods, but according to the official Laravel documentation, you must define a hasOne method and the inverse of it, the belongsTo method.

  • Related