doucheng2053
2017-12-26 17:32
浏览 125
已采纳

如何从Laravel的上一页获取数据

So, i have some categories. And in each category you can add posts.

But in form page for adding posts, how to get the value of that category i.e. of previous page?

This is my form:

<div class="container">
    {!! Form::open(['action' => 'TopicController@store', 'method' => 'POST']) !!}
        <div class="form-group">
            {{ Form::label('title', 'Title') }}
            {{ Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title of the Post']) }}
        </div>
        <div class="form-group">
            {{ Form::label('desc', 'Desc') }}
            {{ Form::textarea('desc', '', ['class' => 'form-control', 'placeholder' => 'Description of the Post']) }}
        </div>
        {{ Form::submit('Submit', ['class' => 'btn btn-default']) }}
    {!! Form::close() !!}
</div>

Link in category page to form:

<a href="/topics/create">Create New Post</a>

Controller:

$this->validate($request, [
            'title' => 'required',
            'desc' => 'required',
        ])
$topic = new Topic;
$topic->topic_title = $request->input('title');
$topic->topic_body = $request->input('desc');
$topic->user_id = auth()->user()->id;
$topic->save();
return redirect('/')->with('Seccess', 'Topic Created');

show.blade.php contains link to this form page. But to get the id of the category page that is referring to this form??

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doxp30826 2017-12-26 18:56
    已采纳

    You need to pass category_id into your link as route parameter:

    <a href="/topics/create/{{ $category_id }}">Create New Post</a>
    

    Catch category_id in /topics/create/category_id route:

    Route::post('/topics/create/{category}', 'TopicsController@create');
    

    And then use it to create a hidden field in your form:

    <div class="container">
        {!! Form::open(['action' => 'TopicController@store', 'method' => 'POST']) !!}
        {{ Form::hidden('category_id', $category_id) }}
        ...
    </div>
    

    And then in your controller:

    ...
    $topic->category_id = $request->input('category_id');
    $topic->user_id = auth()->user()->id;
    $topic->save();
    ...
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题