TarikAli wrote a reply+100 XP
4mos ago
it creats values with this option (master details table) in the same service
public function createValue(Option $option, array $value): OptionValue
{
$optionValue = new OptionValue();
$optionValue->option_id = $option->id;
$optionValue->fill($this->mapOptionValue($value));
$optionValue->save();
return $optionValue;
}
TarikAli started a new conversation+100 XP
4mos ago
Hello, i have this function in a service :
public function createOptionWithValues(array $data): Option
{
$data = $this->prepareOptionData($data);
$validationErrors = $this->validateOption($data);
if (!empty($validationErrors)) {
throw ValidationException::withMessages($validationErrors);
}
$option = new Option();
$option->title = $data['title'];
$option->normalized_title = $data['normalized_title'];
$option->type = OptionTypesEnum::from($data['type']);
$option->status_id = $data['status_id'];
$option->sort = $data['sort'] ?? 0;
$option->image = $data['image'] ?? null;
$option->meta_data = $data['meta_data'] ?? null;
$option->save();
// Create option values if provided
if (!empty($data['values'])) {
foreach ($data['values'] as $value) {
$this->createValue($option, $value);
}
}
return $option;
}
I want to use DB::Transaction to store option with values or rollback all
first: can we refactor this function second: can i use DB::Transaction in a service or in the controller
TarikAli liked a comment+100 XP
4mos ago
yes
Step 1, find out who they are (authentication)
Step 2, determine what they are allowed to do (authorization)
If you need to hold different user metadata for each type of user, polymorphic relationships are the way.
I wrote an article years ago... https://novate.co.uk/using-laravel-polymorphic-relationships-for-different-user-profiles/
TarikAli wrote a reply+100 XP
4mos ago
TarikAli started a new conversation+100 XP
4mos ago
Hello, i have these routes
Route::group([
'prefix' => LaravelLocalization::setLocale(),
'middleware' => ['localeSessionRedirect', 'localizationRedirect', 'localeViewPath']
],
function () {
Route::group([
'prefix' => 'shared',
'as' => 'shared.',
'middleware' => ['auth:admin,teacher'],
], function () {
Route::prefix('options')->name('options.')->group(function () {
Route::resource('', OptionController::class)->parameters(['' => 'option']);
});
});
});
when i loged in in the same browser it tow tabs one admin and the other teacher and
@dd(auth()->guard('admin')->check(), auth()->guard('teacher')->check())
the tow gives true
i want to get the options accordding to admin or teacher