Doubt about Aravel; How does he execute this code?

Asked

Viewed 61 times

1

I’m learning about Laravel and I was intrigued to learn how part of the code works framework concerning Seeder and the question is about the code to follow:

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 the Laravel redirects the execution of that code?

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

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

  • The class Categoria has something like extends Model, right?

  • 1

    Yes, this class has a extends Model.

  • The method can be in this class =)

  • there’s no way in this place with that name.

  • 1

    https://laravel.com/api/5.5/Illuminate/Database/Query/Builder.html#method_insert

1 answer

2


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

Builder.php class Insert function. (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))
        );
    }

Browser other questions tagged

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