dongleman4760 2016-11-29 05:27
浏览 239
已采纳

如何将数据传递给重定向到(Laravel)的路由返回的视图

The following is my situation.

I have two models with controllers containing CRUD methods.

  • Document.
  • File.

While creating a file, an already document is to be specified (selected) to which the created file will be attached to.

In the store method of my documentController after storing the newly created document I want to redirect to files/create route with the id of newly created document, so that files can be created without having to choose a document.

What I have so far

DocumentController

 $input = $request->all();
 $document = $this->documentRepository->create($input);
 return redirect(route('files.create'),array('documentID' => $document->id));

file/create.blade.php

@if( isset($documentID) )
<input type="text" name="document_id" value="{{ $documentID }}" hidden="true">

@else
<div class="form-group col-sm-6">

    {!! Form::label('file_type', 'Document ID:') !!}

    <select name="document_id" class="form-control">
        @foreach( $documents as $document)
            <option value="{{ $document->id }}">{{ $document->id }}-{{ $document->applicant()->name }}</option>
        @endforeach
    </select>
</div>
@endif

How can i achive this.

  • 写回答

5条回答 默认 最新

  • duanen19871021 2016-11-29 05:54
    关注

    The second parameter in the response() method is the http status code that should be used when redirecting.

    You need to chain the with() method:

     $input = $request->all();
     $document = $this->documentRepository->create($input);
     return redirect(route('files.create'))->with('documentID', $document->id);
    

    You can then access the passed session data from the view with {{ session('documentID') }}

    See: https://laravel.com/docs/5.3/responses#redirecting-with-flashed-session-data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器