Php 如何在laravel中使用按钮传递列名?

Php 如何在laravel中使用按钮传递列名?,php,mysql,laravel,sorting,Php,Mysql,Laravel,Sorting,我已经能够对表中的数据进行排序,但我正在尝试获取它,以便当我单击列标题时,它会按该标题类别对数据进行排序 路线 index.blade.php @extends('layouts.master') @section('content') <div class="container"> <div> <div style="margin-top:10px;"> <h3>Contacts</h3&

我已经能够对表中的数据进行排序,但我正在尝试获取它,以便当我单击列标题时,它会按该标题类别对数据进行排序

路线

index.blade.php

@extends('layouts.master')

@section('content')

<div class="container">
    <div>
        <div style="margin-top:10px;">
            <h3>Contacts</h3>
        </div>
        <div style="margin-top:10px; margin-bottom:10px;">
            <a class="btn btn-sm btn-success" href="{{ route('contacts.create') }}">Create New Contact</a>
        </div>
    </div>

    <div class="table-responsive">
    <table class="table table-hover">
        <tr class="row" style="text-align:center">
            <th class="col-sm-1"><button type="submit" class="btn" href="{{ route('contacts.sort', ['column' => 'firstName'] )}}">First Name</button></th>
            <th class="col-sm-2"><button type="submit" class="btn" href="{{ route('contacts.sort', ['column' => 'lastName'] )}}">Last Name</button></th>
            <th class="col-sm-2"><button type="submit" class="btn" href="{{ route('contacts.sort', ['column' => 'email'] )}}">Email</button></th>
            <th class="col-sm-1"><button type="submit" class="btn" href="{{ route('contacts.sort', ['column' => 'phone'] )}}">Phone</button></th>
            <th class="col-sm-1"><button type="submit" class="btn" href="{{ route('contacts.sort', ['column' => 'birthday'] )}}">Birthday</button></th>
            <th class="col-sm-4">Action</th>
        </tr>
        <tbody>
        @foreach ($contacts as $key => $value)
            <tr class="row" style="text-align:center">
                {{--  Need to add in sort functions for each catagory --}}
                <td class="col-sm-1">{{$value->firstName}}</td>
                <td class="col-sm-2">{{$value->lastName}}</td>
                <td class="col-sm-2">{{$value->email}}</td>
                <td class="col-sm-1">{{$value->phone}}</td>
                <td class="col-sm-1">{{$value->birthday}}</td>
                <td class="col-sm-4">
                    <form action="{{ route('contacts.destroy', $value->id) }}" method="post">
                        <a class="btn btn-sm btn-success" href="{{ route('contacts.show', $value->id)}}">Show</a>
                        <a class="btn btn-sm btn-warning" href="{{ route('contacts.edit', $value->id)}}">Edit</a>
                        <a class="btn btn-sm btn-second" href="{{ route('contacts.createAddress', ['contact_id' => $value->id])}}">Add Address</a>
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-sm btn-danger">Delete</button>
                    </form>
                </td>
            </tr>
        @endforeach 
        </tbody>
    </table>
    </div>
    {{ $contacts->links() }}
</div>
@endsection 
当我点击按钮时,什么也没发生。我没有从转储中得到任何信息,网络显示按钮未执行任何操作


提前感谢

您需要在路线中定义列

Route::get('/sort/{column}', 'ContactController@sort')->name('contacts.sort');
Route::get('/sortDesc/{column}', 'ContactController@sortDesc') >name('contacts.sortDesc');
在你的控制器里

public function sort($column)
{
    dump($column);
    $contacts = DB::table('contacts')->orderBy($column)->simplePaginate(10);
    return view('contacts.sort', ['contacts' => $contacts]);
}

使用锚定标记而不是按钮,并将排序参数值传递到URL。

您需要更改视图中的按钮与表单一起工作,只需将它们更改为锚定标记即可。您没有在“提交”按钮上提交任何表单

<tr class="row" style="text-align:center">
            <th class="col-sm-1"><a class="btn" href="{{ route('contacts.sort', ['column' => 'firstName'] )}}">First Name</a></th>
            <th class="col-sm-2"><a class="btn" href="{{ route('contacts.sort', ['column' => 'lastName'] )}}">Last Name</a></th>
            <th class="col-sm-2"><a class="btn" href="{{ route('contacts.sort', ['column' => 'email'] )}}">Email</a></th>
            <th class="col-sm-1"><a class="btn" href="{{ route('contacts.sort', ['column' => 'phone'] )}}">Phone</a></th>
            <th class="col-sm-1"><a class="btn" href="{{ route('contacts.sort', ['column' => 'birthday'] )}}">Birthday</a></th>
            <th class="col-sm-4">Action</th>
        </tr>

行动

将按钮更改为如下标签

<th class="col-sm-1"><a type="submit" class="btn" href="{{ route('contacts.sort', ['column' => 'phone'] )}}">Phone</a></th>
Route::get('/sort/{column}', 'ContactController@sort')->name('contacts.sort');
Route::get('/sortDesc/{column}', 'ContactController@sortDesc')->name('contacts.sortDesc');
然后在控制器中,由于您使用的是get request,因此请删除排序函数参数上的$request并更改为

public function sort($column)
{
    dump($column);
    $contacts = DB::table('contacts')->orderBy($column)->simplePaginate(10);
    return view('contacts.sort', ['contacts' => $contacts]);
}

好的,我该怎么做?
Route::get('/sort/{column}', 'ContactController@sort')->name('contacts.sort');
Route::get('/sortDesc/{column}', 'ContactController@sortDesc')->name('contacts.sortDesc');
public function sort($column)
{
    dump($column);
    $contacts = DB::table('contacts')->orderBy($column)->simplePaginate(10);
    return view('contacts.sort', ['contacts' => $contacts]);
}