如何在 laravel html 中对数据库中的数据进行排序

How to sort data from database in laravel html

我正在尝试对 table 中的特定行进行排序。但我卡在中间。我正在使用 Kyslik\ColumnSortable\Sortable;在模型文件中。但没有实际作用。有什么想法吗?

class estates extends Model
{
use Sortable;

protected $fillable = [ 'Entry', 'Price' ];

protected $table="estates";

public $Sortable = ['Entry', 'Price']; 
}

我的控制器;

public function sumos()
{
  $data['este'] = estates::all();
  return view('pages.sumo', $data);

  return view('pages.sumo',['este' => estates::sortable()->get()]);

}

并在html;

<table align="center">
<tr>
    <td colspan="11" id="header"><h1>物件概要<h1></td>
</tr>
    <tr>
        <th width="340px">会社名</th>
        <th width="80px">物件名</th>
        <th width="135px">所在地</th>
        <th width="135px">総戸数</th>
        <th width="80px">間取り</th>
        <th width="100px">専有面積</th>
        <th width="80px">バルコニー面積</th>
        <th width="100px">竣工年月日</th>
        <th width="100px">@sortablelink('Entry', '入居年月日')</th>
        <th width="100px">@sortablelink('Price', '価格')</th>
        <th width="100px">販売会社名</th>
    </tr>
<table>


@foreach($este as $row) 
<table align="center">
    <tr>
        <td width="340px">Company Name</td>
        <td width="80px">{{ $row->Building_Names }}</td>
        <td width="135px">{{ $row->Addresses }}</td>
        <td width="135px">{{ $row->HouseHolds }}</td>
        <td width="80px">{{ $row->Rooms }}</td>
        <td width="100px">{{ $row->Balconys }}</td>
        <td width="80px">{{ $row->Extents }}</td>
        <td width="100px">{{ $row->Constrution }}</td>
        <td width="100px">{{ $row->Entry }}</td>
        <td width="100px">{{ $row->Price }}</td>
        <td width="100px">{{ $row->Company }}</td> 
    </tr>
</table>
@endforeach

知道如何解决(如何排序)特定数据吗? 谢谢

最简单的解决方案之一是使用 datatables

使用 laravel 内置方法通过两种方式进行排序..

by query and by collection

try this one

estates::orderBy('Entry')->orderBy('Price')->get()

我快速浏览了您正在使用的软件包的文档。

https://github.com/Kyslik/column-sortable

您必须将 @sortablelink('field', 'Field') 添加到 table headers。

另外,看controller方法,你的排序永远达不到。

public function sumos()
{
  return view('pages.sumo',['este' => estates::sortable()->get()]);
}

希望对您有所帮助

你应该写这个。希望这会解决您的问题

<table align="center">
    <tr>
        <td colspan="11" id="header"><h1>物件概要<h1></td>
    </tr>
        <tr>
            <th width="340px">会社名</th>
            <th width="80px">物件名</th>
            <th width="135px">所在地</th>
            <th width="135px">総戸数</th>
            <th width="80px">間取り</th>
            <th width="100px">専有面積</th>
            <th width="80px">バルコニー面積</th>
            <th width="100px">竣工年月日</th>
            <th width="100px">@sortablelink('Entry', '入居年月日')</th>
            <th width="100px">@sortablelink('Price', '価格')</th>
            <th width="100px">販売会社名</th>
        </tr>

@foreach($este as $row) 
        <tr>
            <td width="340px">Company Name</td>
            <td width="80px">{{ $row->Building_Names }}</td>
            <td width="135px">{{ $row->Addresses }}</td>
            <td width="135px">{{ $row->HouseHolds }}</td>
            <td width="80px">{{ $row->Rooms }}</td>
            <td width="100px">{{ $row->Balconys }}</td>
            <td width="80px">{{ $row->Extents }}</td>
            <td width="100px">{{ $row->Constrution }}</td>
            <td width="100px">{{ $row->Entry }}</td>
            <td width="100px">{{ $row->Price }}</td>
            <td width="100px">{{ $row->Company }}</td> 
        </tr>
 @endforeach
 </table>

还在我们的模型顶部添加 use Kyslik\ColumnSortable\Sortable;

在你的控制器中修改这个

public function sumos()
{
  $este= estates::sortable(['Price' => 'desc'])->get();
  return view('pages.sumo', compact('este'));
}