【问题标题】:redirect error exception array to string conversion Laravel 8将错误异常数组重定向到字符串转换 Laravel 8
【发布时间】:2021-09-22 13:11:39
【问题描述】:

我试图将我的删除功能重定向回具有参数团队的索引页面。但它不断向我抛出错误异常数组到字符串的转换。 这是我在 MonitorController.php 上的索引功能

public function index(Team $team)
{   

    $team = Team::where('id',$team->id)->first();

    $objective = Objective::with('keyresult')
                    ->where('team_id',$team->id)
                    ->get();
                    
    $objective = Objective::with('task')
                    ->where('team_id',$team->id)
                    ->get();

    $objective = Objective::with('deadline')
                    ->where('team_id',$team->id)
                    ->get();
    
    return view('/sistem/monitor/index', compact('objective','team'));   
}

这是我在同一文件 MonitorController.php 中的删除函数

public function destroy(Team $team, Objective $objective, Deadline $deadline)
{
    //for deleting objective
    Objective::destroy($objective->id);
    Deadline::destroy($deadline->id);
    return redirect()->action([MonitorController::class, 'index',['team'=>$team]])->with('status', 'Objective Successfully Deleted');
}

这里是索引和销毁路径

Route::get('/sistem/monitor/index/{team}', 'MonitorController@index');
Route::delete('/sistem/monitor/objective/details/{team}/{objective}', 'MonitorController@destroy');

【问题讨论】:

  • 你可以做 return redirect()->route('your_route_name')->with('status', 'Objective Successfully Deleted');
  • 你能详细说明一下吗?我尝试返回 redirect()->route('sistem/monitor/index/team', 'MonitorController@index');但我得到 Symfony\Component\Routing\Exception\RouteNotFoundException Route [/sistem/monitor/index/{team}] 未定义。
  • 我在上面回答了

标签: laravel redirect routes destroy


【解决方案1】:
Route::get('/sistem/monitor/index/{team}', 'MonitorController@index')->name('sistem.monitor.index');

在你的控制器中:

return redirect()->route('sistem.monitor.index', ['team' => $team])->with('status', 'Objective Successfully Deleted');

在路由内部,您应该始终传递路由名称。在 web.php 上的路由中,您可以通过以下方式指定自定义路由名称:->name('sistem.monitor.index')

【讨论】:

  • 嗨,谢谢你的回答,我完全按照你说的做,但现在我得到了 [Route: sistem.monitor.index] [URI: sistem/monitor/index/{team} 缺少必需参数] [缺少参数:团队]。
  • 你能再检查一下吗,我更新了
  • 对不起,因为我是新来的,所以我不能赞成你的回答
猜你喜欢
  • 2016-10-18
  • 2021-07-16
  • 1970-01-01
  • 2019-05-01
  • 2016-09-01
  • 2022-01-25
  • 1970-01-01
  • 2016-10-07
  • 2023-01-05
相关资源
最近更新 更多