Doubts about laravel; How does it execute this code?

1

I'm learning about Laravel and I was intrigued to know how a part of the framework code for Seeder works and the question is about the following code:

public function run() 
{    
   Categoria::insert(['nome'=>'Dúvidas','descricao'=>'Tire suas dúvidas agora mesmo!']);
   Categoria::insert(['nome'=>'Sugestões','descricao'=>'Gostaria de sugerir algo?']);
   Categoria::insert(['nome'=>'Outros','descricao'=>'Qualquer outro tipo de assunto']);
}

How and where does Laravel redirect the execution of this code?

About what I know of PHP , this is a call to a static function in the Categoria class, but there is no insert method in this class.

If you have website tips and books about Laravel you will be of good help ( PT preference).

    
asked by anonymous 17.01.2018 / 18:21

1 answer

2
The function you are looking for is within the Builder class (vendor / framework / src / Illuminate / Database / Query / Builder / Builder.php), it is difficult to find anything beyond documentation definitions, if you want to dismember each function of the framework you will have to look at the files, which are fortunately commented out.

Insert function of class Builder.php (Laravel version 5.5)

   /**
     * Insert a new record into the database.
     *
     * @param  array  $values
     * @return bool
     */
    public function insert(array $values)
    {
        // Since every insert gets treated like a batch insert, we will make sure the
        // bindings are structured in a way that is convenient when building these
        // inserts statements by verifying these elements are actually an array.
        if (empty($values)) {
            return true;
        }

        if (! is_array(reset($values))) {
            $values = [$values];
        }

        // Here, we will sort the insert keys for every record so that each insert is
        // in the same order for the record. We need to make sure this is the case
        // so there are not any errors or problems when inserting these records.
        else {
            foreach ($values as $key => $value) {
                ksort($value);

                $values[$key] = $value;
            }
        }

        // Finally, we will run this query against the database connection and return
        // the results. We will need to also flatten these bindings before running
        // the query so they are all in one huge, flattened array for execution.
        return $this->connection->insert(
            $this->grammar->compileInsert($this, $values),
            $this->cleanBindings(Arr::flatten($values, 1))
        );
    }
    
17.01.2018 / 18:49