gpt4 book ai didi

php - Laravel 5.4 内部错误 : Failed to retrieve the default value

转载 作者:可可西里 更新时间:2023-10-31 23:10:38 25 4
gpt4 key购买 nike

我有一个向数据库提交数据的表单。并将数据提交到数据库。但是 Laravel 在尝试将其重定向到同一 Controller 中的另一个方法时会产生错误。

ReflectionException in RouteDependencyResolverTrait.php line 57:
Internal error: Failed to retrieve the default value

enter image description here

这是我使用的 Controller 。请检查 public function store(Request $request) 方法。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Inbox;

class ContactUsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('pages.contactus');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// validate the data
// store the data in the database
// redirect to another page
$this->validate($request, [
'name' => 'required|max:255',
'email' => 'required',
'telephone' => 'required',
'message' => 'required',
]);

$inbox = new Inbox;

$inbox->name = $request->name;
$inbox->email = $request->email;
$inbox->telephone = $request->telephone;
$inbox->message = $request->message;
$inbox->isnew = 1;
$inbox->category = $request->category;

$inbox->save();

// redirects to the route
//return redirect()->route('contactus.show', $inbox->id);

return redirect()->action(
'ContactUsController@show', ['id' => 11]
);

}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
print_r($id);
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

我尝试了两种不同场合的方法。但是 Laravel 每次都显示相同的错误。

return redirect()->route('contactus.show', $inbox->id);

return redirect()->action(
'ContactUsController@show', ['id' => 11]
);

这是路由文件web.php

Route::get('contactus', 'ContactUsController@create');
Route::get('contactus/show', 'ContactUsController@show');
Route::post('contactus/store', 'ContactUsController@store');

我不知道是什么导致了这个问题。任何建议都会有所帮助。

谢谢!

最佳答案

您在重定向中将 id 值传递给路由器,但您并未告诉路由器在路由中使用该值。因此,路由器不知道将 id 值传递给 show 方法。

将路线更改为如下所示:

Route::get('contactus/{id}', 'ContactUsController@show');

然后您的重定向应该工作,您应该被重定向到(使用示例中的 ID)/contactus/11

关于php - Laravel 5.4 内部错误 : Failed to retrieve the default value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42816437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com