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条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?