【问题标题】:BadMethodCallException in Macroable.php line 81:Macroable.php 第 81 行中的 BadMethodCallException:
【发布时间】:2016-06-29 23:54:47
【问题描述】:

我正在尝试与我正在进行的项目建立关系。我得到的错误是: Macroable.php 第 81 行中的 BadMethodCallException: 方法组织不存在。

这是我的存储方法。

公共函数存储(请求 $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.');

} 我的 CalendarEvent 模型中的关系是这样设置的

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

我的组织模型中的关系是这样设置的

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

感谢您的帮助。

【问题讨论】:

    标签: php laravel-5.2


    【解决方案1】:

    您是否设置了路由模型绑定?即使使用这种设置,我认为您不能仅从请求中访问模型,您需要为要注入的模型设置适当的参数。

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

    或者问题是您试图在$request 上保存,而您需要在$calendar_event 上执行此操作,如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 2017-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多