Skip to content

Add when() method  #219

@ajcastro

Description

@ajcastro

Please, can we add when() method for conditional clause to make code more readable/concise.
https://laravel.com/docs/8.x/queries#conditional-clauses

I saw a code somewhere:

      const query = User.page(params.page || 1)
        .include('avatar')
        .orderBy(params.sort)

      if (params.search) {
        query.where('search', params.search)
      }

      const result = await query.params({ limit: 20 }).get()

Would be re-written to:

      const result = await User.page(params.page || 1)
        .include('avatar')
        .orderBy(params.sort)
        .when(params.search, (query, search) => query.where('search', search))
        .params({ limit: 20 })
        .get()

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions