jeudi 21 novembre 2019

Laravel Sorting errors

Hi guys, i have did all the setting and set-up, and i don't know which mistake i had made since laravel didn't throw back the error message to me. I wish to sort the (Code), (Description), (Parent_id) and (Status), can someone guide me where i had made the mistake ? My Objective is to click on the (code) and the system are able to sort in ascending order and descending order !

Controller Code

public function index(Request $request)
{
    $codeSearch = $request->get('code');
    $descriptionSearch = $request->get('description');

    $sort = $request->get('sort')!=''? $request->get('sort'):'asc';

    $categories = Category::where('code', 'like', '%' . $codeSearch . '%')
        ->where('description', 'like', '%' . $codeSearch . '%')
        ->paginate(5);


    foreach ($categories as $category) {
        if ($policy = Category::find($category->parent_id, 'description')) {
            $category->parent_id = $policy->description;
        }
    }

    return view('category.index', ['category' => $categories]);
}

Index.blade.php Code

        <table class="table table-striped table-dark">
            <thead class="bg-dark" style="color: #636b6f">


            <tr>
                <th>Id</th>

                <th style="vertical-align: middle">
                    <a href="?search=&code=&field=code&sort=">
                        Code
                    </a>
                    
                </th>


                <th style="vertical-align: middle">
                    <a href="?search=&description=&field=code&sort=">
                        Description
                    </a>
                    
                </th>

                <th style="vertical-align: middle">
                    <a href="?search=&parent_id=&field=code&sort=">
                        Parent
                    </a>
                    
                </th>

                <th style="vertical-align: middle">
                    <a href="?search=&status=&field=code&sort=">
                        Status
                    </a>
                    
                </th>


                <th>Action</th>
                <th>Action</th>
            </tr>
            @foreach($category as $row)

                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>

                    <td><a href="" class="btn btn-warning">Edit</a></td>
                    <td>
                        <form method="post" class="delete_form" action="">
                            
                            

                            <input type="hidden" name="_method" value="DELETE"  />
                            <button type="submit" class="btn btn-danger">Delete</button>
                        </form>
                    </td>
                </tr>

            @endforeach
        </table>
        <div class="container">
            @foreach ($category as $row)
                
            @endforeach
        </div>
        <div class="pagination">
            
        </div>

    </div>
</div>
<script>
    $(document).ready(function () {
        $('.delete_form').on('submit', function () {
            if (confirm("Are you sure you want to delete it"))
            {
            return true;
            } else
                {
                return false;
            }
        });
    });

Web.php

 Route::group(['prefix' => 'laravel-crud-search-sort'],function(){    Route::get('/','categoryController@index'); });

My website interface looks like this for now : Interface



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire