在try 将参数从一个控制器传递到另一个控制器时,我遇到了缺少参数的问题.参数是$id,因此数据最初是从details blade.php中的POST方法进入函数postCreateStepOne的,但是,我想将数据传递到一个新的视图中,然后返回

Redirect()->route(‘details.tenant.step.two’)->with([‘id’=>$id] );}

这就是错误发生的地方.但是,如果我跳过它进入一个新的路径,并直接返回到带有COMPACT参数的视图中,它就可以很好地工作.例如,

Return view(‘Docent.DetailsStep-Two’,mini(‘id’,‘Property’));

然而,我更喜欢一个新的URL,因为我正在使用Laravel做多步表单.

Error enter image description here

web.php

Route::get('/document/details/viewing/{id}', 'ViewDetails')->name('details.tenant');
Route::post('/document/details/viewing/{id}', 'postCreateStepOne')->name('post.step-one');
Route::get('/document/details/viewing/step-2/{id}', 'ViewDetailsStep2')->name('details.tenant.step.two');

TenanatController.php

public function viewDetails($id){

        $view = Properties::findOrFail($id);
        return view('document.details', compact('view'));
    }

public function ViewDetailsStep2(Request $request, $id){

        $view = Properties::findOrFail($id);
        $property = $request->session()->get('property');

        return view('document.details-step-two', compact('view', 'property'));
    }

public function postCreateStepOne($id, Request $request)
    {
        $validatedData = $request->validate([
            'property-name' => 'required',
        ]);
  
        if(empty($request->session()->get('property'))){
            $property = new Tenancy();
            $property->fill($validatedData);
            $request->session()->put('property', $property);
        }else{
            $property = $request->session()->get('property');
            $property->fill($validatedData);
            $request->session()->put('property', $property);
        }
  
        return  redirect()->route('details.tenant.step.two')->with( ['id' => $id] );
    }

details.blade.php

<form action="{{ route('post.step-one', $view->id) }}" method="POST">
                @csrf

                <div class="card-body">
                    <div class="form-group">
                        <label for="title">Property Name:</label>
                        <input type="text" value="" class="form-control" id="property-name"  name="property-name">
                    </div>
                </div>

                <div class="card-footer text-right">
                    <button type="submit" class="btn btn-primary">Next</button>
                </div>
</form>

推荐答案

当您在重定向上使用with时,参数将通过会话传递.如果要重定向到具有给定路由参数的路由,则应在路由函数本身中传递该参数,例如

return  redirect()->route('details.tenant.step.two', ['id' => $id]);

Laravel相关问答推荐

URL类图中的循环关系

我是否需要/是否可以从控制器运行LARLAVEL备份?

如何定制有关密码被泄露的Laravel Breeze S消息?

在Laravel中URL中添加保护ID

嵌套数组请求的 Laravel 验证,其中如果整数之一为零,则另一个应大于零

阻止用户查看其他用户正在查看的内容的最佳方法是什么?

Laravel PHP 框架的新手. /以外的路由不起作用

Laravel 5 - 仅在特定页面/控制器(页面特定assets资源)上添加样式表

Laravel 5.4 存储:下载文件.文件不存在,但显然存在

你如何找到 Laravel 外观背后的底层类?

Laravel 5.4 LengthAwarePaginator

无法安装 Laravel 包 - 干预图像

未定义的类常量App\Providers\RouteServiceProvider::HOME

Laravel - 批量分配异常错误

WhereNotExists Laravel Eloquent

从 Artisan 电话中获得响应

Laravel 5 与 Postgresql

如何在 Laravel 视图中找到当前语言?

带有 Xdebug 的 Laravel 5 总是抛出The payload is invalid..

错误:找不到模块 'webpack/lib/rules/DescriptionDataMatcherRulePlugin' 需要堆栈: