donglu3184 2019-03-26 16:12
浏览 93
已采纳

如何从被调用的函数重定向回编辑页面?

On edit.blade.page there is a button for file deleting which leads to the update method in a controller. The update method calls private deleteImage method from which I want to redirect a user back to edit.blade.page, but it redirects me somewhere else. I have tried all approach from the documentation

Controller

public function update(Request $request, $id){
        ($request['method']==true) ? $this->imageDelete($request, $id) : null;
    }

private function imageDelete (Request $request, $id){
        if($request['method']=='destroy'){
            $file = public_path().'/storage'.$request['old_image'];

            $validatedData = $request->validate(['images'=> 'string',]);

            $old_images = explode(',', $request['images']);
            $paths = array();
            foreach ($old_images as $old){
                ($old != $request['old_image']) ? $paths[] = $old : null ;
            }
            (File::exists($file)) ? File::delete($file) : null;

            Announcement::where('slug', $id)->update(array_merge($validatedData,array('images'=>implode(",",$paths),)));
            return redirect()->route('announcements.edit',[$id])->with('status',1);
        }
        return redirect()->route('announcements.edit',[$id])->with('status', 0);
    }

Route

Route::resource('announcements', 'AnnouncementController', ['names' => [
                                                                    'index' => 'announcements',
                                                                    'store' => 'announcements.store',
                                                                    'show'  => 'announcements.show',
                                                                    'destroy'=>'announcements.destroy',
                                                                    'update' => 'announcements.update',

                                                                ]])
  • 写回答

3条回答 默认 最新

  • dongyidao1461 2019-03-26 16:59
    关注

    Alright! It seems like laravel doesn't see the return from other functions rather than predefined by itself. So the solution, in that case, would be making the return from within the update method

    public function update(Request $request, $id){
      return ($request['method']==true) ? $this->imageDelete($request, $id) : null;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?