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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?