skip to Main Content

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


  1. use return redirect()->route(‘routename’, compact(‘projects’));

    Login or Signup to reply.
  2. 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 a RedirectResponse to target named routes, like this:

    return redirect()->route('route-name'); 
    

    You can pass parameters in the second argument of this function, so in your case you would need something like this:

    public function show(): RedirectResponse
    {
        $projects = project::all();
    
        return redirect()->route('list', ['projects' => $projects]);
    }
    

    Since these kind of redirect responses are common, Laravel also provides a to_route helper, which can be used like this:

    public function show(): RedirectResponse
    {
        $projects = project::all();
    
        return to_route('list', ['projects' => $projects]);
    }
    

    See the laravel docs: https://laravel.com/docs/10.x/redirects#redirecting-named-routes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search