Shiva
7 months ago
82
11
Laravel

Getting my posts to show the correct amount

Posted 7 months ago by Shiva

I'm feeling very silly right now, because I'm sure this is a simple question, but I can't seem to figure it out. What I've done is I've create a page with some posts and you can sort them by a number of months. The problem I'm having is that I can't seem to get them to display only those posts.

My function

public function trackPosts()
{
    $menus_child = Menu::where('menu_id', 0)->with('menusP')->get();
    $contacts = Contact::all();

    $posts = Auth::user()->posts->sortByDesc('post_date');
    

    return view('public.track-posts', compact('menus_child', 'contacts', 'posts'));
}

public function sortPosts(Request $request)
{
    if($request->all() == 'three_months')
    {
        $to = Carbon::now('+2:00');

        $from = $to->copy()->subMonth(3);

        $posts = Post::whereBetween('post_date', [$from, $to])->get();
    }

    return redirect()->route('blog.track-post', compact('post'));
}

and this is my blade

@extends('layouts.public')
@section('content')
    <div class="content_wrapper">
            <div class="row">
                <div class="col-lg-12">
                    <form action="{{ route('blog.sort-posts') }}">
                        <select class="sort-posts" name="sort-posts">
                            <option selected>Open this select menu</option>
                            <option value="three_months">Last 3 months</option>
                            <option value="six_months">Last 6 months</option>
                            <option value="2019">2019</option>
                        </select>
                    </form>
                </div>
            </div>
        @foreach($posts as $post)
            <div class="row">
                <div class="col-lg-12">
                    <div class="card mt-15">
                        <div class="card-header">
                            <div class="invoice-details">
                                <div>
                                    Posted: {{ Carbon\Carbon::parse($post->post_date)->format('d F Y') }}
                                </div>
                            </div>
                        </div>

                        <div class="card-body">
                            <div class="row text-center">
                                {{ $post->title }}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        @endforeach
    </div>

    <script>
        $(document).ready(function(){
            $('.sort-posts').change(function(){
                $(this).closest("form").submit();
            });
        })
    </script>
@stop

and my web.php

Route::get('/blog/track-posts', '[email protected]')->name('blog.track-posts');
Route::get('/blog/sort-posts', '[email protected]')->name('blog.sort-posts');

Please sign in or create an account to participate in this conversation.