2014-11-25 162 views
2

錯誤消息:http://puu.sh/d4l0F/5b0ac07e68.png調用一個成員函數助理()在非對象

我甚至試圖建立關聯之前,節省了$運輸對象。我已經驗證了$ transporation,$ from和$ to都是它們各自的對象,它們是。

我確定我在這裏錯過了一些愚蠢的東西,但我沒有想法。

我的代碼:

class RideBuilder implements RideBuilderInterface 
{ 
    public function create(Advance $advance) 
    { 
     $ride = new Ride; 
     if($ride->validate(Input::all())) { 
      $ride->save(); 

      $to = Location::find(Input::get('dropoffLocation')); 
      $from = Location::find(Input::get('pickupLocation')); 

      $transportation = new Transportation; 
      $transportation->save(); 

      $transportation->transportable()->associate($ride); 
      $transportation->to()->associate($to); 
      $transportation->from()->associate($from); 

      $event = new Event; 
      $event->start = Input::get('ridePickUpTime'); 
      $event->save(); 

      $event->eventable->save($transportation); 
      $event->subjectable->save($advance); 
     } 
     return $ride; 
    } 
} 

選址模型:

class Location extends Elegant 
{ 
protected $table = 'locations'; 

public $rules = array(
    'label'   => 'required|min:2', 
    'street'  => 'required', 
    'city'   => 'required', 
    'state'   => 'required', 
    'type'   => 'required', 
); 

public function advance() 
{ 
    return $this->belongsTo('Booksmart\Booking\Advance\Model\Advance'); 
} 

public function locationable() 
{ 
    return $this->morphTo(); 
} 

} 

運輸型號:

class Transportation extends Elegant 
{ 
    protected $table = 'transportations'; 

    public function event() 
    { 
     $this->morphOne('Booksmart\Component\Event\Model\Event'); 
    } 

    public function start_location() 
    { 
     $this->belongsTo('Booksmart\Component\Location\Model\Location', 'start_location'); 
    } 

    public function end_location() 
    { 
     $this->belongsTo('Booksmart\Component\Location\Model\Location', 'end_location'); 
    } 
} 
+0

您是否在'Transportation'模型上實現了belongsTo'transportable'關係? – 2014-11-25 00:56:51

+0

@SteveBauman'moprhTo'而不是'belongsTo',但是,那肯定是問題所在。 – 2014-11-25 10:10:02

+0

啊,必須這樣!一旦我可以測試,我會作出回答。謝謝。 – 2014-11-25 14:14:20

回答

20

我有一個類似的問題。我犯了一個愚蠢的錯誤,在關係方法中不加入「返回」!

確保返回的關係......顯然,這將工作:

public function medicineType() 
    { 
     $this->belongsTo('MedicineType', 'id'); 
    } 

這是正確方式:

public function medicineType() 
    { 
     return $this->belongsTo('MedicineType', 'id'); 
    } 

容易錯過,難以調試...

+0

謝謝忘了離開我的答案。這是答案。 – 2015-05-18 12:24:38

+0

哦該死的是我錯過了它:) – 2015-10-31 11:49:51

+2

這只是節省了2小時無用的調試時間。 – 2016-01-05 17:35:45

相關問題