Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnie55's avatar

Laravel redirect does not work at all

Am trying to return redirect back after saving a record to DB but it fails. Generally only the login redirect and home are working. Return view is also working fine . This is what I have tried:

return redirect()->back()->with('success',"Product updated successfully");

I have also tried using return redirect('main/products'); but still doesn't work. I also tried using the return Redirect::to('main/products'); but still nop!

However, if I inspect on the browser under response, I can see that the html code of the view is return, just not displayed.

Yes I Know, seems simple but its frustrating me. Since it's affecting my entire system am guessing there is something am missing its not the syntax. I appreciate your help

0 likes
29 replies
Snapey's avatar

can you show the full method?

remember to put ``` on a line before and after your code blocks

johnie55's avatar
public function updateProducts(){
          request()->validate([
            'name'=>'required',
            'description'=>'required'
        ]);
        $product=Product::findOrFail(\request('id'));
        $product->name=\request('name');
        $product->description=\request('description');
        $product->save();

        return redirect()->back()->with('success',"Product updated successfully");  
    } 

This is the entire method

johnie55's avatar

am also new to laracasts, kindly forgive my way of posting code

chatty's avatar

can you paste the whole controller?

munazzil's avatar

Use as like below whether redirect() or back(),

   return redirect()->with('success',"Product updated successfully");
Snapey's avatar

is updateProducts mentioned in your route, or is this function called from another controller method?

munazzil's avatar

Else can try like below,

      return Redirect::back()->with('success',"Product updated successfully");
johnie55's avatar

Yes its in the route

$controller = "Main\ProductsController@";
Route::post('/main/products/update',$controller.'updateProducts');
Snapey's avatar

Does it redirect back correctly if you have a validation error?

johnie55's avatar

@Snapey New development, I inspected on the browser and found that the view is actually been return as response. The problem is that it does not refresh page to display new content. I have an image I don't know how to share it here

johnie55's avatar

Thanks @munazzil. This returns the response but doesn't reload the page with new content. The browser doesn't reload which makes me suspect it could be problem. lemme try another browser

johnie55's avatar

@raravel

<?php

namespace App\Http\Controllers\Main\System;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\URL;
use App\Http\Controllers\Controller;

use App\Models\Main\System;
use App\Models\Main\Product;
use App\Repositories\SearchRepo;
use Illuminate\Support\Facades\Schema;

class ProductsController extends Controller
{

    public function productsPage(){
        return view('main.products.products',[

        ]);
    }


    public function storeProducts(){
        request()->validate([
            'product_name'=>'required',
            'description'=>'required'
        ]);
        $product=new Product;
        $product->name=\request('product_name');
        $product->description=\request('description');
        $product->save();
        return redirect('main/products')->with('success',"New Product created successfully");
    }

    public function updateProducts(){
        request()->validate([
            'name'=>'required',
            'description'=>'required'
        ]);
        $product=Product::findOrFail(\request('id'));
        $product->name=\request('name');
        $product->description=\request('description');
        $product->save();

        return redirect()->back()->with('success',"Product updated successfully");  
    }
}
Snapey's avatar

Are you posting the form data with ajax?

Snapey's avatar

Sharing images , easiest to post it to one of the spammy image sharing sites like imgur and then link here

johnie55's avatar

@aswinghosh

@extends('layouts.dashboard')

@section('page_heading','Products ')

@section('section')

    <div class="card border-left-success shadow h-100 py-2">
        <div class="card-body">
            @include('common.bootstrap_table_ajax',[
            'table_headers'=>["name","description","action"],
            'data_url'=>'/main/system/products/list',
            'base_tbl'=>'products'
            ])
        </div>
    </div>
    <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="favoritesModalLabel" id="product_modal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title"
                    id="product_modal">New Guarantee Product</h4>
                    <button type="button" class="btn btn-success btn-icon-split" class="close"
                    data-dismiss="modal"
                    aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                </div>
                <div class="modal-body">
                    {!!Form::autoForm(\App\Models\Main\Product::class,"main/system/products/update")!!}

                </div>
            </div>
        </div>
    </div>
@endsection

The modal is the one I use for updating. This issue is everywhere in the entire system not just this part. Redirects only return response but don't reload the page

Tray2's avatar

Remove this

{!!Form::autoForm(\App\Models\Main\Product::class,"main/system/products/update")!!}

And replace it with proper form code.

johnie55's avatar

@aswinghosh . Didn't get you there, am using several packages I have installed using composer like swap,turbolinks, fixer e.t.c

munazzil's avatar

I don't know why you used to assign a variable $controller in your route use as like below and check,

     Route::post('/main/products/update','Main\ProductsController@updateProducts');
aswinghosh's avatar

why are you using autoForm. You can use traditional form instead .I think your controller is fine.

johnie55's avatar
<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="favoritesModalLabel" id="product_modal">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h4 class="modal-title"
                    id="product_modal">New Guarantee Product</h4>
                    <button type="button" class="btn btn-success btn-icon-split" class="close"
                    data-dismiss="modal"
                    aria-label="Close">
                    <span aria-hidden="true">&times;</span></button>
                </div>
                <div class="modal-body">
                    <form class="ajax-post model_form_id" method="post" action="{{url('main/system/products/update')}}">
                        @csrf
                        <input type="hidden" name="id" value="{{$product->id}}">
                        <div class="form-group name">
                            <div class="fg-line">
                                <label class="fg-label control-label label_name">Product Name</label>
                                <input value="" type="text" name="name" class="form-control"></div>
                            </div>
                            <div class="form-group description">
                                <div class="fg-line">
                                    <label class="fg-label control-label label_description">Description</label>
                                    <input value="" type="text" name="description" class="form-control"></div>
                                </div>
                                <div class="form-group row">
                                    <label class="control-label col-md-3">&nbsp;&nbsp;</label>
                                    <div class="col-md-6">
                                        <button type="submit" class="btn btn-primary btn-raised submit-btn"><i class="zmdi zmdi-save"></i> Submit</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

@tray2 am still having the error

johnie55's avatar

Have tried this. Didn't work either. I used variable coz I have many routes on same file sharing same controller. It's easier for me

johnie55's avatar
johnie55
OP
Best Answer
Level 1

Good news: I fixed the problem. Bad news: It was a silly mistake.

I had put an HTML line of code at the end of my resources blade to test if all resources were being loaded by console logging the value. This is the line I had added:

    <input type="hidden" name="material_page_loaded" value="1">
</body>
</html>

Sorry guys I took your time for this. I appreciate your help.

Please or to participate in this conversation.