Mounting a query with Laravel

Asked

Viewed 324 times

6

I’m studying the structure of the Laravel from this example already ready => beast.

In the project there is the repository userRepository with the following structure:

<?php

namespace App\Repositories;

use App\Models\User;

class UserRepository
{
    /**
     * Get users collection paginate.
     *
     * @param  int  $nbrPages
     * @param  array  $parameters
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function getAll($nbrPages, $parameters)
    {
        return User::with ('ingoing')
            ->orderBy ($parameters['order'], $parameters['direction'])
            ->when (($parameters['role'] !== 'all'), function ($query) use ($parameters) {
                $query->whereRole ($parameters['role']);
            })->when ($parameters['valid'], function ($query) {
                $query->whereValid (true);
            })->when ($parameters['confirmed'], function ($query) {
                $query->whereConfirmed (true);
            })->when ($parameters['new'], function ($query) {
                $query->has ('ingoing');
            })->paginate ($nbrPages);
    }
}

This structure makes use of the config file parameters:

<?php

return [

    'users' => [
        'order' => 'created_at',
        'direction' => 'desc',
        'role' => 'all',
        'valid' => false,
        'confirmed' => false,
        'new' => false,
    ],
    'posts' => [
        'order' => 'created_at',
        'direction' => 'desc',
        'new' => false,
        'active' => false,
    ],
    'contacts' => [
        'new' => false,
    ],
    'comments' => [
        'new' => false,
        'valid' => false,
    ],

];

The structure of the database is as follows:

Schema::create('users', function(Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
    $table->string('name')->unique();
    $table->string('email')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->enum('role', array('user', 'redac', 'admin'));
    $table->boolean('valid')->default(false);
});

I’m trying to understand these methods: whereRole, whereValid and whereConfirmed, I searched the entire project and I couldn’t find the structure of these methods, as Laravel puts together these methods?

1 answer

3


That’s one of the cool (and confusing things about Laravel).

These methods do not exist, they are short versions of the method where, what comes after this keyword is the name of the column that is part of the where.

That is to say,

$query->whereRole('algumValor')

is exactly the same thing as

$query->where('role', 'algumValor')

how Laravel assembles these methods?

This has already been answered in: Is there any magic method for calling an attribute as a method in php?

  • Cool!! And in the case of a bank column that is id? How would I mount this Where? whereRole_Id?

  • @Marceloboni How so? role is already a column.

  • In this template already ready, they use role as a field In one, I think of decoupling and creating a new table in the bd, referencing only the role id, in this case the column role became: $table->integer('role_id')->unsigned();

  • @Marceloboni in this case you exchange the snake_case for Camel case. It would be whereRoleId()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.