Alfred Garrett
Alfred Garrett

Reputation: 3

BadMethodCallException in Macroable.php line 81:

I am trying to set up a relationship with a project I am working on. The error I get is: BadMethodCallException in Macroable.php line 81: Method Organization does not exist.

Here is my store method.

public function store(Request $request) {

$calendar_event = new CalendarEvent();

$calendar_event->title            = $request->input("title");
$calendar_event->start            = $request->input("start");
$calendar_event->end              = $request->input("end");
$calendar_event->is_all_day       = $request->input("is_all_day");
$calendar_event->background_color = $request->input("background_color");



$request->Organization()->calendar()->save($calendar_event);

return redirect()->route('calendar_events.index')->with('message', 'Item created successfully.');

} My relationship in my CalendarEvent model is set up like this

 public function Organization()
{
    return $this->belongsTo('App\Organization');
}

The relationship in my Organization model is set up like this

public function calendar()
{
    return $this->hasMany('App\CalendarEvent');
}

Thank you for your help.

Upvotes: 0

Views: 719

Answers (1)

fosron
fosron

Reputation: 301

Did you setup a route model binding? And even with that setup, i don't think you can access the model just from the request, you need to setup a proper parameter for the model you want injected.

public function store(Request $request, YourModel $model) {

}

Or the problem is that you are trying to save on $request, when you need to do that on $calendar_event, like this:

$calendar_event->Organization()->calendar()->save($calendar_event);

Upvotes: 0

Related Questions