简体   繁体   English

重定向不起作用laravel 5

[英]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? 如何解决它,为什么它不起作用?

return redirect, 返回重定向,

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

Here is the link . 这是链接

It should work. 它应该工作。

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
}

您应该将其退回:

return redirect('admin')->with($result);

with有两个参数

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

Could you please check with defining all your Routes in below format and then try redirecting in appropriate route 您能否以以下格式检查所有路线,然后尝试以适当的路线重定向

Route::group(array('prefix' => 'admin','middleware'=>'auth'), function() {
    Route::post('student/cust_post', ['as' => 'admin.index', 'uses' => 'Admin\IndexController@index']
    );
});

你有没有尝试过?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM