Laravel插入数据透视表

时间:2016-09-06 18:40:40

标签: php laravel

我正在尝试将数据写入数据透视表,但我得到了 "在null"

上调用成员函数guests()

这是我的代码我哪里出错?

我已经尝试了这个并且我弄错了什么错误

活动模型

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

来宾模式

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

控制器

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

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

    return $check->events;
}

2 个答案:

答案 0 :(得分:0)

尝试这样做。在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');
    });

事件模型中使用此关系

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

访客模型中

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

在数据库中添加一些虚拟数据(事件和来宾之间的关系)并将此代码粘贴到路径

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

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

        });

反之亦然

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

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

        });

如果你到目前为止工作并获得你的虚拟数据,请告诉我

答案 1 :(得分:0)

根据@Mike的建议更改您的关系方法。

在事件模型中更改您的guests()方法,如下所示:

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

更改访客模型中的events()方法,如下所示:

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

现在,要将数据插入数据透视表,您有两种情况:
1)没有透视数据

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

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