Support the ongoing development of Laravel.io →
Database Eloquent Architecture
Last updated 2 years ago.
0

Basically, you first want to check the input has the data you're looking for, if it does, try to process/validate it, if it validates, then use it in your query.

// Get the query builder instance for the model
$query = Model::query();

if (Input::has('start_at'))
{
    try {
        $start_at = Carbon::createFromFormat('d/m/Y', Input::get('start_at'));
    } catch(Exception $e) {
        // Carbon failed to process input
    }

    if (isset($start_at))
    {
        $query->where('created_at', '>=', $start_at);
    }
}

if (Input::has('end_at'))
{
    try {
        $end_at = Carbon::createFromFormat('d/m/Y', Input::get('end_at'));
    } catch(Exception $e) {
        // Carbon failed to process input
    }

    if (isset($end_at))
    {
        $query->where('created_at', '<=', $end_at);
    }
}

$results = $query->get();
Last updated 2 years ago.
0

hi iWader, thank you for the replay

I've 4 input that could affect the query, like if the date is isset and name is isset, then i will want to query the date between which has name of someone. In more deep i will have date between, name and instructor. I will have these in one page to see the result :

  1. date between (start and end)
  2. name of student
  3. instructor
  4. class
    if i choose only the date between, so i will query all user, class and instructor
    if i choose date with name, i will want to query the spesific user with all of his instructor and class
    if i choose the date, name and instructor, i will want to query the spesific user with spesific instructor and all class
    or i can choose the class only to show all the user and all instructor, or i can choose the instructor only to show all user and all class and so on, can you please advice me the proper way to do this? thank you in avance
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.