Currently, I’m trying to get all the info from my database and make the function redirect to a list page where all the data gets displayed. The redirect is currently not working and I’m getting the following error:
SymfonyComponentHttpFoundationRedirectResponse::__construct(): Argument #2 ($status) must be of type int, array given.
The error is specifically coming from the return redirect part of my code. This is working fine for my classmates who link it to the same blade.php page as I am. Do I need to add anything else just because I am redirecting instead of using the view declaration type?
My controller:
public function show(): RedirectResponse
{
$projects = project::all();
return redirect('list', ['projects' => $projects]);
}
page it needs to link to (just for reference):
@extends('layouts.layout')
@section('content')
<main>
@foreach($project as $projects)
<h2>{{ $project->title }}</h2>
<p>{{ $project->picture_url }}</p>
<p>{{ $project->description }}</p>
<p>{{ $project->explanation }}</p>
<button> Edit </button>
<button> Delete </button>
@endforeach
</main>
@endsection
2
Answers
use return redirect()->route(‘routename’, compact(‘projects’));
To redirect to a named route you will need to use the
Redirector
instance which you can get by calling the redirect method without any parameters.This instance allows you to call a
route
method which will return aRedirectResponse
to target named routes, like this:You can pass parameters in the second argument of this function, so in your case you would need something like this:
Since these kind of redirect responses are common, Laravel also provides a
to_route
helper, which can be used like this:See the laravel docs: https://laravel.com/docs/10.x/redirects#redirecting-named-routes