Isu*_*uru 9 php laravel laravel-5 laravel-5.4
我有一个表单,它将数据提交给数据库.它将数据提交给数据库.但是当Laravel尝试将其重定向到同一控制器中的另一个方法时会产生错误.
ReflectionException in RouteDependencyResolverTrait.php line 57:
Internal error: Failed to retrieve the default value
Run Code Online (Sandbox Code Playgroud)
这是我使用的控制器.请检查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)
{
//
}
}
Run Code Online (Sandbox Code Playgroud)
我在两个不同的场合尝试了以下两种方法.但Laravel每次都会出现同样的错误.
return redirect()->route('contactus.show', $inbox->id);
return redirect()->action(
'ContactUsController@show', ['id' => 11]
);
Run Code Online (Sandbox Code Playgroud)
这是路径文件web.php.
Route::get('contactus', 'ContactUsController@create');
Route::get('contactus/show', 'ContactUsController@show');
Route::post('contactus/store', 'ContactUsController@store');
Run Code Online (Sandbox Code Playgroud)
我不知道是什么导致了这个问题.任何建议都会有所帮助.
谢谢!
您正在将id值传递给重定向中的路由器,但您没有告诉路由器在路由中使用此值.因此,路由器不知道将id值传递给show方法.
将路线更改为如下所示:
Route::get('contactus/{id}', 'ContactUsController@show');
Run Code Online (Sandbox Code Playgroud)
然后你的重定向应该工作,你应该被重定向到(使用你的例子中的ID)/contactus/11.
| 归档时间: |
|
| 查看次数: |
6802 次 |
| 最近记录: |