将数据重定向回到表格完成的位置Laravel

发布于 2025-01-29 10:19:14 字数 3138 浏览 0 评论 0原文

我有一个表格,控制器从DB中获取数据所请求的数据。然后,我想将其重定向到与表单同一页面,但使用我从数据库中获得的数据。我使用的是Laravel 5.5

这是我的控制器的控制器:页面

    public function listsStore(Request $request) {
        $data = $request->all();
        $user = $request->user;
        $day = date('Y-m-d', strtotime($request->day));
        $from =  date('Y-m-d', strtotime($request->from));
        $to = date('Y-m-d', strtotime($request->to));
        $radio = $data['radio-btn'];

        // dd($user, $from, $to);
        // dd($radio);

        if($radio === 'tasks') {
            if($request->day) {
                $tasks = Task::where('user_id', $user)
                        ->whereDate('start', $day)
                        ->orderBy('start','DESC')
                        ->get();
                // dd($tasks);
                return redirect()->back()->with('tasks', $tasks);
            }else{ // gets for the range of dates requested
                $tasks = Task::where('user_id', $user)
                        ->whereBetween('start',[$from, $to])
                        ->orderBy('start','DESC')
                        ->get()
                        ->groupBy(function($date) {
                        return Carbon::parse($date->start)->format('Y-m-d'); 
                        });
                // dd($tasks);
                return redirect()->back()->with('tasks', $tasks);
            } 
        }elseif($radio === 'timecards'){ //IF REQUEST IS TIMECARDS
            if($request->day) { //gets for the day requested
                $timecards = Timecard::where('user_id',$user)
                            ->whereDate('checkin', $day)
                            ->orderBy('checkin','DESC')
                            ->get();
                // dd($timecards);
                return redirect()->back()->with('timecards', $timecards);
            } else{ // gets for the range of dates requested
                $timecards = Timecard::where('user_id', $user)
                            ->whereBetween('checkin',[$from, $to])
                            ->orderBy('checkin','DESC')
                            ->get()
                            ->groupBy(function($date) {
                                return Carbon::parse($date->checkin)->format('Y-m-d'); 
                            });
                // dd($timecards);
                return redirect()->back()->with('timecards', $timecards);
            }
        }
        // return redirect('backend.list');
    }

的控制器:

    public function lists() {
        $users = User::all();
        return view( 'backend.list', compact('users'));
    }

在视图中,我尝试了这种方式:

    @if(session()->has('timecards'))
        @foreach($timecards as $timecard)
            <div>{{$timecard->checkin}}</div>
        @endforeach
    @endif

谢谢

更新:现在我获取了数据,但它以JSON的形式,例如格式,任何关于我如何的想法可以操纵它吗? 我的观点:

    @if(session()->has('data'))
        {{session('data')}}
    @endif

谢谢

I have a form from which the controller gets the data requested from the db. I want to then redirect to the same page as the form but with the data i got from the db. I am using laravel 5.5

This is my controller for the store:

    public function listsStore(Request $request) {
        $data = $request->all();
        $user = $request->user;
        $day = date('Y-m-d', strtotime($request->day));
        $from =  date('Y-m-d', strtotime($request->from));
        $to = date('Y-m-d', strtotime($request->to));
        $radio = $data['radio-btn'];

        // dd($user, $from, $to);
        // dd($radio);

        if($radio === 'tasks') {
            if($request->day) {
                $tasks = Task::where('user_id', $user)
                        ->whereDate('start', $day)
                        ->orderBy('start','DESC')
                        ->get();
                // dd($tasks);
                return redirect()->back()->with('tasks', $tasks);
            }else{ // gets for the range of dates requested
                $tasks = Task::where('user_id', $user)
                        ->whereBetween('start',[$from, $to])
                        ->orderBy('start','DESC')
                        ->get()
                        ->groupBy(function($date) {
                        return Carbon::parse($date->start)->format('Y-m-d'); 
                        });
                // dd($tasks);
                return redirect()->back()->with('tasks', $tasks);
            } 
        }elseif($radio === 'timecards'){ //IF REQUEST IS TIMECARDS
            if($request->day) { //gets for the day requested
                $timecards = Timecard::where('user_id',$user)
                            ->whereDate('checkin', $day)
                            ->orderBy('checkin','DESC')
                            ->get();
                // dd($timecards);
                return redirect()->back()->with('timecards', $timecards);
            } else{ // gets for the range of dates requested
                $timecards = Timecard::where('user_id', $user)
                            ->whereBetween('checkin',[$from, $to])
                            ->orderBy('checkin','DESC')
                            ->get()
                            ->groupBy(function($date) {
                                return Carbon::parse($date->checkin)->format('Y-m-d'); 
                            });
                // dd($timecards);
                return redirect()->back()->with('timecards', $timecards);
            }
        }
        // return redirect('backend.list');
    }

controller for the page:

    public function lists() {
        $users = User::all();
        return view( 'backend.list', compact('users'));
    }

and in the view i tried this way:

    @if(session()->has('timecards'))
        @foreach($timecards as $timecard)
            <div>{{$timecard->checkin}}</div>
        @endforeach
    @endif

Thank you

Update: now i get the data but it is in a json like format, any idea of how i can manipulate it?
my view:

    @if(session()->has('data'))
        {{session('data')}}
    @endif

Thankss

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦屿孤独相伴 2025-02-05 10:19:14

在listsstore()中

return Redirect:: route('route-name')->with( ['data' => $data] );

,在您的路线中,您可以阅读

Session::get('data');

In listsStore()

return Redirect:: route('route-name')->with( ['data' => $data] );

and in your route you can read it with

Session::get('data');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文