2014-11-25 35 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

相关问题