Home > Mobile >  How to add sorting to Laravel view
How to add sorting to Laravel view

Time:05-20

Hi guys I want to add a sorting filter to here..I want to filter data by ASC order but the question is data coming from using webpack. Can I add to ASC order filter to here {{ $option->value }}

</option>
    @if ($field->options->count() > 0)
        @foreach ($field->options as $option)
            <option value="{{ $option->id }}"
            @if ($defaultValue == $option->id)
            selected="selected"
            @endif
            >
            {{ $option->value }}
</option>

CodePudding user response:

@extends('layout')

@section('content')
<h1>Products</h1>

<p>
    <a  href="/products/create"><span ></span> Add Product</a>
</p>

<table >
    <thead>
        <th>@sortablelink('category.name', 'Category')</th>
        <th>@sortablelink('name', 'Name')</th>
        <th>@sortablelink('sku', 'SKU')</th>
        <th>@sortablelink('price', 'Price')</th>
        <th>Actions</th>
    </thead>
    <tbody>
        @if ($products->count() == 0)
        <tr>
            <td colspan="5">No products to display.</td>
        </tr>
        @endif

        @foreach ($products as $product)
        <tr>
            <td>{{ $product->category->name }}</td>
            <td>{{ $product->name }}</td>
            <td>{{ $product->sku }}</td>
            <td>${{ $product->price }}</td>
            <td>
                <a  href="{{ action('ProductsController@edit', ['id' => $product->id]) }}">Edit</a>

                <form style="display:inline-block" action="{{ action('ProductsController@destroy', ['id' => $product->id]) }}" method="POST">
                    @method('DELETE')
                    @csrf
                    <button > Delete</button>
                </form>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

{!! $products->appends(Request::except('page'))->render() !!}

<p>
    Displaying {{$products->count()}} of {{ $products->total() }} product(s).
</p>

@endsection

CodePudding user response:

You can sort the collection:

@foreach ($field->options->sortBy('your_column_name') as $option)

// Example
->sortBy('your_column_name') // ASC
->sortByDesc('your_column_name') // DESC

if any problem you can use ->all() collection method

->sortBy('your_column_name')->all()
  • Related