Laravel 对非对象调用成员函数associate()
错误消息: 在尝试创建关联之前,我甚至保存了$transportation对象。我已经验证了$Transportation、$from和$to都是它们各自的对象,它们都是 我肯定我错过了一些愚蠢的东西,但我没有主意了 我的代码:Laravel 对非对象调用成员函数associate(),laravel,laravel-4,eloquent,Laravel,Laravel 4,Eloquent,错误消息: 在尝试创建关联之前,我甚至保存了$transportation对象。我已经验证了$Transportation、$from和$to都是它们各自的对象,它们都是 我肯定我错过了一些愚蠢的东西,但我没有主意了 我的代码: class RideBuilder implements RideBuilderInterface { public function create(Advance $advance) { $ride = new Ride;
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');
}
}
运输模式:
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');
}
}
我也有类似的问题。我犯了一个愚蠢的错误,没有在关系方法中添加“return” 确保您返回关系。。。显然,这将不起作用:
public function medicineType()
{
$this->belongsTo('MedicineType', 'id');
}
这是正确的方法:
public function medicineType()
{
return $this->belongsTo('MedicineType', 'id');
}
容易错过,难以调试…您是否在
运输模型上实现了belongsTo“transportable”关系?@SteveBaumanmoprhTo
而不是belongsTo
,但是的,这一定是问题所在。啊,那一定是问题所在!一旦我能测试,我会做一个答案。谢谢。谢谢你忘了留下我的答案。这就是答案。哦,该死的,是的,我也错过了:)这帮我节省了两个小时无用的调试时间。