Laravel 5,不能通过请求方法插入,不存在

时间:2016-06-20 16:01:51

标签: laravel-5 eloquent laravel-routing

我试图做的事情没有取得任何成功:

在welcome.blade中我有一个foreach,有一些板和子板(由用户随机生成),你可以点击子板,然后像这样/ subboardOne。我在我的routes.php上得到了这个。

<form class="form col-md-12 center-block" role="form" method="POST" action="/{{$subboardcoll->id}}">

然后你可以通过表格在这个子板上发布一个帖子,但由于我真的不知道laravel如何知道他在哪里,表格是这样的:

public function index($subboard)
{
    $subboardcoll = Subboard::where('subboaName', $subboard)->first();
    $threads = Thread::where('subboaId', $subboardcoll->id)
           ->orderBy('created_at', 'desc')
           ->get();
    return view('threads.thread', compact('threads', 'subboardcoll'));
}

这个$ subboardcoll-&gt; id来自控制器,它通过索引函数发送集合:

public function store(Request $request)
    {
        $this->validate($request, [
            'comentario' => 'required|max:2000',
            //'g-recaptcha-response' => 'required|recaptcha',
            //'imagen' => 'required',
        ]);

        $request->subboards()->threads()->create([
            'thrName' => $request->nombre,
            'thrComment' => $request->comentario,
            'thrImg' => $request->imagen,
            'thrSubject' => $request->tema,
        ]);

        return redirect()->back();

    }

然后我试图发送我的表单并存储自动插入subboardId的线程,但laravel不识别子板方法:

BadMethodCallException in Macroable.php line 81: Method subboards does not exist.

并且给了我这个erorr:

public function subboard()
{
    return $this->belongsTo(Subboard::class, 'subboaId');
}

你们能帮助我们知道原因吗?还有更好的形式去做我正在尝试的事情吗?我在laravel新手,谢谢

编辑:

Thread.php

public function thread()
{
    return $this->hasMany(Thread::class);
}

Subboard.php

{{1}}

1 个答案:

答案 0 :(得分:0)

请求对象中不存在方法子板。考虑这样做

public function store($id, Request $request)
{
    $this->validate($request, [
        'comentario' => 'required|max:2000',
        //'g-recaptcha-response' => 'required|recaptcha',
        //'imagen' => 'required',
    ]);

   Subboard::find($id)->threads()->create([
        'thrName' => $request->nombre,
        'thrComment' => $request->comentario,
        'thrImg' => $request->imagen,
        'thrSubject' => $request->tema,
    ]);

   //Alternative query statement
   Subboard::where('id', $id)->first()->threads()->create([.....

    return redirect()->back();

}