Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

untymage's avatar

Model::create VS Model:insert (Number of query)

When to use insert method instead of create ? in this example the insert perform only one query for 3 tags :


$inputField = 'tag1,tag2,tag3';

$tags = explode(',', $inputField);

$data = collect($tags)->map(function ($tag){
    return ['name' => $tag];
})->toArray();

Tag::insert($data);

// 1 query


create 3 query


$inputField = 'tag1,tag2,tag3';

$tags = explode(',', $inputField);

foreach ($tags as $name) {
    Tag::create(compact('name'));
}

// 3 query


So if insert method performs 1 query why most of videos in laracasts uses create in foreach loop?

0 likes
1 reply
tykus's avatar

It is up to you to decide when you feel one is more appropriate than the other.; insert clearly will result in fewer queries.

Just be aware that the insert method bypasses the model; so there are not Model events firing for the insert(s); your timestamp fields will not be set.

2 likes

Please or to participate in this conversation.