Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to a member function associate() on a non-object

Error Message: http://puu.sh/d4l0F/5b0ac07e68.png

I've even saved the $transportation object before trying to create associations. I've verified that both $transporation, $from and $to are all their respective objects and they are.

I'm sure I'm missing something stupid here but I'm out of ideas.

My code:

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;
    }
}

Location Model:

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();
}

}

Transportation Model:

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');
    }
}
like image 299
Dylan Pierce Avatar asked Nov 27 '22 03:11

Dylan Pierce


1 Answers

I had a similar issue. I made the stupid mistake of not adding the "return" in the relationship method!

Make sure you return the relationship... Obviously this will not work:

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

This is the correct way:

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

Easy to miss, hard to debug...

like image 87
HPage Avatar answered Dec 04 '22 02:12

HPage