简体   繁体   中英

Laravel Insert into Pivot Table

I am trying to write data to the pivot table but i get "Call to a member function guests() on null"

this is my code where am i going wrong?

I have tried this and i'm confused on whats wrong

Event Model

    <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Event extends Model
{

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'events';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['headline', 'description', 'address', 'zip', 'longitude', 'latitude',
        'position', 'country_id', 'cat_id', 'start_date', 'end_date'];

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    /**
     * The products that belong to the shop.
     */
    public function guests()
    {
        return $this->belongsToMany('App\Models\Guest', 'event_guest', 'guest_id', 'event_id');
    }
}

Guest Model

     <?php

namespace App\Models;

    use Illuminate\Database\Eloquent\Model;
class Guest extends Model
{

 protected $fillable = ['first_name', 'last_name', 'email'];
public function events()
{
    return $this->belongsToMany('App\Models\Event', 'event_guest', 'event_id', 'guest_id');
}
}

Controller

 //RSVP events
public function rsvpCheck()
{
    $check = Guest::find(5);
    //$guestCheck = Event::where('id',5)->first();

    $check->events()->attach(2);

    return $check->events;
}

Try to do it this way. Add this below your create_events_migration

   Schema::create('event_guest', function (Blueprint $table) {
       $table->integer('event_id')->unsigned()->index();
       $table->foreign('event_id')->references('id')->on('events');
       $table->integer('guest_id')->unsigned()->index();
       $table->foreign('guest_id')->references('id')->on('guests');
    });

In your Event model use this relationship

public function guests()
    {
        return $this->belongsToMany(Guest::class); // include at the top:  use App\Models\Guest;
    }

In your Guest model

public function events()
        {
            return $this->belongsToMany(Event::class); // include at the top:  use App\Models\Event;
        }

Add some dummy data in your database ( relations betwen events and guests) and paste this code in your routes

Route::get('/testguest', function(){

            $guest= Guest::first();
            dd($company->events);

        });

and viceversa

Route::get('/testevents', function(){

            $event= Event::first();
            dd($event->guests);

        });

Let me know if you got it working so far and got your dummy data

Change your relationship methods as suggested by @Mike.

Change your guests() method in Event Model as below:

public function guests()
    {
        return $this->belongsToMany('App\Models\Guest');
    }

Change your events() method in Guest Model as below:

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

Now, to insert data into pivot table you have two cases:
1) No pivot data

public function rsvpCheck()
{
    $check = Guest::findOrFail(5);
    $check->events()->attach(2);
    return $check->events();
}

2) Extra data needs to be inserted in pivot table

public function rsvpCheck()
{
    $data = ['attribute'=>'value'];//replace this with the data you want to insert 
    $check = Guest::findOrFail(5);
    $check->events()->attach(2,$data);
    return $check->events();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM