Home > Enterprise >  Laravel - unable to establish connection between two tables in database
Laravel - unable to establish connection between two tables in database

Time:11-24

so I'm try to "connect" two tables using belongsTo and hasMany but it doesn't seem to be working... I'll link my code down below, if there is anything else you might need to fully understand the problem, please feel free to ask!

pages.notification

<section >
            @each('partials.notification', $sent_notifications, 'notification')
            @each('partials.notification', $received_notifications, 'notification')
        </section>

partials.notification

<div >
    <div >
        <a href="/event/{{ $notification->event->id }}">
            {{ $notification->event->name }}
        </a>
    </div>
</div>

SQL

CREATE TABLE event (
   id SERIAL PRIMARY KEY,
   owner_id INT NOT NULL REFERENCES users(id),
   name TEXT NOT NULL,
   description TEXT NOT NULL,
   tag TEXT,
   start_datetime TIMESTAMP NOT NULL CHECK (start_datetime >= now()),
   end_datetime TIMESTAMP NOT NULL CHECK (end_datetime > start_datetime),
   price FLOAT DEFAULT 0.0,
   max_capacity INT DEFAULT NULL,
   attendee_counter INT DEFAULT 0 CHECK (attendee_counter <= max_capacity AND attendee_counter >= 0),
   location TEXT NOT NULL,
   image TEXT NOT NULL,
   is_private BOOLEAN DEFAULT false,
   is_full BOOLEAN DEFAULT false
);

CREATE TABLE notification (
   id SERIAL PRIMARY KEY,
   event_id INT NOT NULL REFERENCES event(id),
   sent_users_id INT NOT NULL REFERENCES users(id) CHECK (sent_users_id != receiver_users_id),
   receiver_users_id INT NOT NULL REFERENCES users(id),
   status notification_status NOT NULL
);

NotificationController.php

public function list($id)
{
  if (!Auth::check()) return redirect('/login');

  $sent_notifications = DB::table('notification')->where('sent_users_id', $id)->orderBy('id')->get();
  $received_notifications = DB::table('notification')->where('receiver_users_id', $id)->orderBy('id')->get();
  return view('pages.notifications', ['sent_notifications' => $sent_notifications, 'received_notifications' => $received_notifications]);
}

Event.php

public function notifications() {
    return $this->hasMany('App\Models\Notification');
}

public function notification() {
    return $this->hasMany(Notification::class);
}

Notification.php

public function event() {
    return $this->belongsTo('App\Models\Event');
}

public function events() {
    return $this->belongsTo(Event::class);
}

The error I'm getting is Undefined property: stdClass::$event and I can't seem to get around it... please let me know if there's an easy fix.

Thanks alot for your time

CodePudding user response:

When you use DB::table() you get instances of StdClass as results (or array of StdClass) so you cant use methods declared in your model Notification class.

You need to query starting from the model

$sent_notifications = Notification::where('sent_users_id', $id)->orderBy('id')->get();
$received_notifications = Notification::where('receiver_users_id', $id)->orderBy('id')->get();

This will get you a collection of Notification::class instances and you can use declared relations as well as any method declared in your model class.

CodePudding user response:

Always use best code practices and always use code that helps you, not code that is an obstacle.

If you have relationship there 1:n like $event has many $notifications, use those singulars and plurals in names

class Event extends Model
{
    public function notifications()
    {
        return $this->hasMany(Notification::class);
    }
}

class Notification extends Model
{
    public function event()
    {
        return $this->belongsTo(Event::class);
    }
}

Remove those duplicated methods. What I wrote here is what is considered for good practice. More good advices you can also find here.

  • Related