首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用非对象上的成员函数关联()

调用非对象上的成员函数关联()
EN

Stack Overflow用户
提问于 2014-11-25 00:45:16
回答 1查看 3.1K关注 0票数 4

错误消息:http://puu.sh/d4l0F/5b0ac07e68.png

在尝试创建关联之前,我甚至保存了$transportation对象。我已经验证了$transporation、$from和$to都是它们各自的对象,而且它们都是。

我肯定我错过了一些愚蠢的东西,但我没有主意了。

我的代码:

代码语言:javascript
复制
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;
    }
}

地点模式:

代码语言:javascript
复制
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();
}

}

运输模式:

代码语言:javascript
复制
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');
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-16 10:20:14

我也有过类似的问题。我犯了一个愚蠢的错误,没有在关系方法中添加“返回”!

一定要把这段感情还给你..。显然,这将使而不是工作:

代码语言:javascript
复制
public function medicineType() 
   {
      $this->belongsTo('MedicineType', 'id');
   }

这是正确的方式:

代码语言:javascript
复制
public function medicineType() 
   {
      return $this->belongsTo('MedicineType', 'id');
   }

很容易错过很难调试..。

票数 34
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27116924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档