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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动