Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect doesn't work laravel 5

Routes:

Route::group(['prefix'=>'admin','middleware'=>'auth'],function(){
    Route::get('/',['uses'=>'Admin\IndexController@index','as'=>'adminIndex']);
    Route::resource('/cat-n-cat','Admin\CatalogsNCategoriesController');
});

controller:

public function update($data)
    {
            $category = Category::find($data[0]['id']);
            $result = $this->category_rep->updateCategory($data,$category);    
            if (is_array($result) && !empty($result['error'])) {    
                return back()->with($result);
            }    
             redirect('admin')->with($result);    
    }

Model:

public function updateCategory($data,$category){
        $data=$data[0];
        if (empty($data)) {
            return array('error' => 'No data');
        }
        $result = $this->one($data['name']);            
        if (isset($result->id) && ($result->id != $category->id)) {
            return ['error' => 'Category with this name already exists'];
        }    
        $category->fill($data);    
        if($category->update()){    
            return ['status' => 'Category has been added'];
        }
    }

After editing category redirect doesn't trigger and i stay in same page. How to fix it and what is the reason why it doesn't work?

like image 348
Batmannn Avatar asked Mar 08 '17 13:03

Batmannn


2 Answers

return redirect,

return redirect('/admin')->with(compact('result')); 

Here is the link.

It should work.

like image 160
Rahul Avatar answered Oct 13 '22 14:10

Rahul


Use At Top

use Illuminate\Support\Facades\Redirect;

and

public function update($data)
{
        $category = Category::find($data[0]['id']);
        $result = $this->category_rep->updateCategory($data,$category);    
        if (is_array($result) && !empty($result['error'])) {    
            return Redirect('<PreviousControllerName>')->with($result); //Change It
        }    
         return Redirect('/')->with($result);    //Change It
}
like image 26
V16 Avatar answered Oct 13 '22 13:10

V16