Search code examples
laravelhttp-redirectroutesdestroy

redirect error exception array to string conversion Laravel 8


im trying to redirect my delete function back to index page that have parameter team. but it keeps throwing me Error Exception array to string conversion. here my index fucntion on 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'));   
}

and here is my delete function inside the same file 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');
}

here the index and destroy route

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

Solution

  • Route::get('/sistem/monitor/index/{team}', 'MonitorController@index')->name('sistem.monitor.index');
    

    And in your controller:

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

    Inside the route you should always pass the route name. In your route on web.php you specify a custom route name by doing: ->name('sistem.monitor.index')