3
\$\begingroup\$

This is my Laravel seeder for two database tables. I need to make this seeder more efficient and optimized. How can this be achieved? I would appreciate some tips.

$userId = User::first()->id;
 // information of email template variable
 $emailTemplateVariables = [
 'user-create' => ['module' => 1, 'module_event' => 1, 'value' => ['USER_NAME', 'USER_EMAIL']],
 'user-deactive' => ['module' => 1, 'module_event' => 7, 'value' => ['USER_NAME', 'USER_EMAIL']],
 'purchase-order-create' => ['module' => 5, 'module_event' => 1, 'value' => ['ORDER_NO', 'PRODUCT_NAME', 'PURCHASE_DATE', 'PRODUCT_QUANTITY', 'ORDER_PRICE', 'SUPPLIER', 'SUPPLIER_INVOICE_NO', 'WAREHOUSE', 'STORE_NAME', 'STORE_ADDRESS', 'STORE_PHONE', 'STORE_WEBSITE', 'STORE_EMAIL']]
 ];
 collect($emailTemplateVariables)->each(function ($templateVariableItem, $slug) use ($userId) {
 $templateVariableIds = collect($templateVariableItem['value'])
 ->map(function ($value) use ($userId) {
 return EmailTemplateVariable::updateOrCreate(
 ['variable_key' => $value],
 ['created_by' => $userId]
 )->id;
 })->toArray();
 EmailTemplateSetting::create([
 'slug' => $slug,
 'module_id' => $templateVariableItem['module'],
 'module_event_id' => $templateVariableItem['module_event'],
 'template_variable_ids' => json_encode($templateVariableIds),
 'created_by' => $userId,
 ]);
 });
Sᴀᴍ Onᴇᴌᴀ
29.6k16 gold badges46 silver badges203 bronze badges
asked Nov 21, 2023 at 12:15
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Your question runs the risk of not getting reviewed because it lacks any decent context. Did you read up on how to ask a good question? See: Help center. We'll be more willing to put effort into reviewing your code, if you show a bit of effort on your part. Also, don't forget there's an answer to your question on Stack Overflow you didn't respond to. \$\endgroup\$ Commented Nov 21, 2023 at 21:05

1 Answer 1

2
\$\begingroup\$

Consider using arrow functions

While it likely won't lead to much in terms of efficiency, the syntax can be simplified. When a closure/function only has one expression then it can often be converted to an arrow function, which eliminates the need for a use statement since there is no separate scope.

For example- the nested callback passed to map():

 function ($value) use ($userId) {
 return EmailTemplateVariable::updateOrCreate(
 ['variable_key' => $value],
 ['created_by' => $userId]
 )->id;
 }

Converting this to an arrow function gives:

 fn($value) => EmailTemplateVariable::updateOrCreate(
 ['variable_key' => $value],
 ['created_by' => $userId]
 )->id

Eliminate single use variables

The variable $templateVariableIds is only used once after it is assigned. Perhaps the value was inspected during debugging but otherwise there is not much point to assign it a variable - the value can be substituted in the one place it is used.

I haven't tested it but conceivably the last collect() block could be simplified to this:

collect($emailTemplateVariables)->each(fn($templateVariableItem, $slug) => EmailTemplateSetting::create([
 'slug' => $slug,
 'module_id' => $templateVariableItem['module'],
 'module_event_id' => $templateVariableItem['module_event'],
 'template_variable_ids' => json_encode(
 collect($templateVariableItem['value'])
 ->map(fn($value) => EmailTemplateVariable::updateOrCreate(
 ['variable_key' => $value],
 ['created_by' => $userId]
 )->id)->toArray()
 ),
 'created_by' => $userId,
]));
answered Mar 9, 2024 at 1:07
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.