简体   繁体   English

Laravel插入数据透视表

[英]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" 我正在尝试将数据写入数据透视表,但是得到“在null上调用成员函数guest()的成员”

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 将此添加到您的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(){ 路线:: 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. 按照@Mike的建议更改您的关系方法。

Change your guests() method in Event Model as below: 如下更改事件模型中的guests()方法:

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

Change your events() method in Guest Model as below: 如下更改客户模型中的events()方法:

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

Now, to insert data into pivot table you have two cases: 现在,要将数据插入数据透视表,您有两种情况:
1) No pivot data 1)没有数据

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

2) Extra data needs to be inserted in pivot table 2)需要在数据透视表中插入额外的数据

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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