错误消息:http://puu.sh/d4l0F/5b0ac07e68.png
在尝试创建关联之前,我甚至保存了$transportation对象。我已经验证了$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');
}
}发布于 2015-05-16 10:20:14
我也有过类似的问题。我犯了一个愚蠢的错误,没有在关系方法中添加“返回”!
一定要把这段感情还给你..。显然,这将使而不是工作:
public function medicineType()
{
$this->belongsTo('MedicineType', 'id');
}这是正确的方式:
public function medicineType()
{
return $this->belongsTo('MedicineType', 'id');
}很容易错过很难调试..。
https://stackoverflow.com/questions/27116924
复制相似问题