Laravel 过滤日期之间的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30008404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:28:55  来源:igfitidea点击:

Laravel Filtering data between dates

laravelfiltercontrollerlaravel-5

提问by Eduardo Guimar?es

I'm beginner in Laravel and web development and I have a silly question, I'm building a system that lists the user financial transactions. In the homepage is listed all the transactions made by the user. I put two fields up there to make the date filter of the transactions, but I don't know how to proceed this filter in Laravel.

我是 Laravel 和 Web 开发的初学者,我有一个愚蠢的问题,我正在构建一个列出用户财务交易的系统。在主页中列出了用户进行的所有交易。我把两个字段放在那里来制作交易的日期过滤器,但我不知道如何在 Laravel 中进行这个过滤器。

I'm using datepicker and it is working fine, I also know that the validation system allows me to use Laravel date: after and date: before, but I don't know how and in which method in the controller send these dates. Here's my dates form code:

我正在使用 datepicker 并且它工作正常,我也知道验证系统允许我使用 Laravel date: after 和 date: before,但我不知道控制器中如何以及以哪种方法发送这些日期。这是我的日期表单代码:

{!! Form::open(['route' => 'transactions.index']) !!}

    {!! Form::label('data_inicio', 'De: ') !!}
    {!! Form::input('date', 'data_inicio', null, ['class' => 'datepicker', 'data-date-format' => 'dd/mm/yy']) !!}

    {!! Form::label('data_fim', 'Até: ') !!}
    {!! Form::input('date', 'data_fim', null, ['class' => 'datepicker', 'data-date-format' => 'dd/mm/yy']) !!}

    {!! Form::submit('Enviar') !!}

{!! Form::close() !!}

The method in the controller which calls the view of the homepage is the index, here's the index code:

控制器中调用首页视图的方法是索引,这里是索引代码:

public function index()
{
    $transactions = Auth::user()->transactions;

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

Here is how I show the data in the view:

这是我在视图中显示数据的方式:

@foreach( $transactions as $transaction )
    <tr>
        <td><a href="#">{!! date('d-m-Y', strtotime($transaction->created_at)) !!}</a></td>
        <td><a href="#">{!! $transaction->title !!}</a></td>
        <td>{!! $transaction->amount !!}</td>
    </tr>
@endforeach

I need to send these dates I got to this same method (index) when the Pesquisar button was clicked to be able to seek in the database only the transactions of the proposed date, the problem is I do not know do this.

当点击 Pesquisar 按钮时,我需要将这些日期发送到相同的方法(索引),以便能够在数据库中仅查找建议日期的交易,问题是我不知道这样做。

I tried to send these dates as parameter request to the index method, modifying it like this:

我尝试将这些日期作为参数请求发送到 index 方法,像这样修改它:

public function index(Request $request)

I got a Column Not Found error when I click in the submit, but I'm not using this request in any query in the method. I really think that it has a simple way to do it and I appreciate any help!!

单击提交时出现“未找到列”错误,但我没有在该方法的任何查询中使用此请求。我真的认为它有一个简单的方法来做到这一点,我感谢任何帮助!!

Here's my model class:

这是我的模型类:

class Transaction extends Model {

    protected $table = 'transactions';

    protected $guarded = [];

    public function users()
    {
        return $this->belongsTo('App\User');
    }
}

采纳答案by Edgar Orozco

Try this to obtain the dates in your controller:

试试这个以获取控制器中的日期:

$data_inicio = Input::get('data_inicio');
$data_fim = Input::get('data_fim');

Then you can use the dates to query your Transaction relation (assuming this a one to many relationship and is configured propperly)

然后您可以使用日期来查询您的交易关系(假设这是一对多关系并且配置正确)

$transactions = Transaction::whereBetween('created_at',[$data_inicio, $data_fim])->where('user_id',Auth::id())->get();