2016-09-06 4 views
0

내가 피벗 테이블에 데이터를 기록하려고하지만 난 를 얻을이 내가 갈거야 내 코드입니다Laravel 삽입

"널 (null)의 멤버 함수 손님()에 전화" 잘못된?

나는이 시도하고 내가 무슨 잘못

이벤트 모델

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

컨트롤러

,369에 혼란 스러워요
//RSVP events 
public function rsvpCheck() 
{ 
    $check = Guest::find(5); 
    //$guestCheck = Event::where('id',5)->first(); 

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

    return $check->events; 
} 
+1

은 어디'guest' 방법을 callig하는 피벗 테이블에 삽입 할 필요가? – jaysingkar

+0

이벤트 모델'public function guests() { return $ this-> belongsToMany ('App \ Models \ Guest', 'event_guest', 'guest_id', 'event_id'); }' – Didi

+0

어디서 정의 할 것인가? – tam5

답변

0

이렇게하면됩니다. (

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

가 데이터베이스에 몇 가지 더미 데이터를 추가하여 고객 모델에 이벤트 모델 사용에 create_events_migration 아래의 관계를 관계

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

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

을이 추가 타협한 이벤트와 손님)을 작성하여이 코드를 경로에 붙여 넣으십시오.

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

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

     }); 

및 viceversa에

경로 : 수 ('/ testevents', 기능() {

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

     }); 

당신이 당신의 더미 데이터를가 지금까지 작업을 얻고있어 알려줘

+0

볼 필요가 없습니다. 피벗 테이블의 데이터베이스에 저장하려고합니다. – Didi

+0

관계가 작동합니까? 그냥 디버그하려고합니다. – Mike

+0

이 공개 함수를 가지고 있습니다. { 스키마 ({event_guest, function (Blueprint $ table) { $ table-> integer ('event_id') -> unsigned() ('이벤트') -> onDelete ('cascade'); $ table-> integer ('guest_id ('이벤트 ') -> 참조(); $ table-> foreign ('event_id ' onDelete ('cascade'); ('손님') -> 참조 ('손님') -> 참조 ('ID') -> 참조 부호없는 unsigned() $ table-> primary ([ 'event_id', 'guest_id']); }); } – Didi

0

@Mike의 제안대로 관계 방법을 변경하십시오. 지금

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

를 두를 피벗 테이블에 데이터를 삽입 할 :

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

아래와 같이 고객 모델에 events() 방법을 변경합니다

아래와 같이 이벤트 모델에 guests() 방법 변경 사례 :
1) 피벗 데이터 없음

,210
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(); 
} 
관련 문제