4

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.

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:

{!! 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.

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

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');
    }
}

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

4 Comments

Hello, thanks for the help!! I'm having a problem when I press Submit, I got this error: QueryException in Connection.php line 620 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'data_inicio' in 'field list' It's trying to find these columns in the database, but I'm not using these dates in any query. I think there's something wrong in the method call or the parameter pass.
Can you post your model class? in order to see how assign the viariables to the column or columns in your model.
I posted my model class, I believe the relationship is correct. I tried your way but I dont receive transactions, only the current user. I tried this way: $transactions = Transaction::whereBetween('created_at', array($data_inicio, $data_fim))->get(); } else { $transactions = Auth::user()->transactions; } but it returns to me all the transactions, not only of the current user.
I have a relation very similar, the between you post works very straightforward. Return only the objects between two dates. Must be another problem. Declaration in your User model perhaps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.