noblemfd
1 month ago
124
5
Laravel

How to Approve all the posts of a particular Employee

Posted 1 month ago by noblemfd

I have written a code for post of employees and it is working perfectly:

Controller

public function index()
{
    $userEmployee = Auth::user()->employee_id;
    $posts = Post::latest()->where('employee_id', $userEmployee)->get();
    return view('admin.post.index', compact('posts'));
}

And it generates this view shown below. The view loads all the unapproved posts for a particular employee:

 <table class="table table-bordered table-striped table-hover dataTable js-exportable">
        <thead>
                        <tr>
                            <th>ID</th>
                            <th>Title</th>
                            <th>Author</th>
                            <th><i class="material-icons">visibility</i></th>
                            <th>Is Approved</th>
                            <th>Status</th>
                            <th>Action</th>

                        </tr>
                        </thead>
                        <tfoot>
                        <tr>
                            <th>ID</th>
                            <th>Title</th>
                            <th>Author</th>
                            <th><i class="material-icons">visibility</i></th>
                            <th>Is Approved</th>
                            <th>Status</th>
                            <th>Action</th>

                        </tr>
                        </tfoot>
                        <tbody>
                        @foreach($posts as $key=>$post)
                        <tr>
                            <td>{{ $key + 1 }}</td>
                            <td>{{ str_limit($post->title,'10') }}</td>
                            <td>{{ $post->user->name }}</td>
                            <td>{{ $post->view_count }}</td>
                            <td>
                                @if($post->is_approved == true)
                                    <span class="badge bg-blue">Approved</span>
                                @else
                                    <span class="badge bg-pink">Pending</span>
                                @endif
                            </td>
                            <td>
                                @if($post->status == true)
                                    <span class="badge bg-blue">Published</span>
                                @else
                                    <span class="badge bg-pink">Pending</span>
                                @endif
                            </td>
                         </tr>
                        @endforeach

           </tbody>
  </table>

    <form action="{{route('admin.post.approve', $post->id)}}" method="POST" enctype="multipart/form-data">
        @csrf
        @method('PUT')
     <div>
        <button type="submit" class="btn btn-primary"><i class="fas fa-arrow-right"></i>Approve</button>
    </div> 
   </form> 

I want to add a submit button to the view called approve. Once the button is clicked, it checks where is_approved is = 0 for the loaded employee and turns all the is_approved that relates to the employee (the loaded) data to 1.

I have written this function in the same controller, but I see that it will only work for a selected row:

public function approve($id){

    $post = Post::find($id);
    if ($post->is_approved == false){
        $post->is_approved = true;
        $post->save();
        $post->user->notify(new AuthorPostApprove($post));

        Toastr::success('Post Successfully Approved');
    }else{
        Toastr::info('Post is already Approved');
    }

    return redirect()->back();
}

and have this route/web.php

Route::get('posts/', '[email protected]')->name('post.index');
Route::put('/post/{id}/approve', '[email protected]')->name('post.approve');

How do I re-write my Controller, view and route to achieve this?

Thank you.

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