Use Form Request for that, exatly the same what is in this episode
https://laracasts.com/series/whip-monstrous-code-into-shape/episodes/1
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Seniors:
I have a controller method that has some complexity and it's doing several tasks at once:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class RegisterationController extends Controller
{
public function store(Request $request)
{
// 1. check request for user unique identifier. then firstOrCreate this user then pass output to next step
// 2. take the user and create new (requests) relationship record and pass that to the next step
// 3. take the relationship record and create 2 (details records) relationships for that and pass the original record to next step
// 4. run database procedure and take result to update the relationship record
// 4. if all of above went well, upload some attachment and add record to attachment table
}
}
the current code is working fine. I wrote everything on the store method with help of private functions but I don't like it and I think it's not maintainable and not really OOP.
I'm thinking about using Laravel *** Pipeline *** class and create didecated class for each step but I'm not sure if it's the right thing.
Also I can use Repository pattern but also I don't think it's very useful here.
any tips???
@uniqueginun Your code could look likes this
UserRegisterationOperationController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\UserRegisterationOperation\StoreForm;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class UserRegisterationOperationController extends Controller
{
public function store(StoreForm $form)
{
try {
[$user, $task, $files] = $form->persist();
return response(['user' => $user, 'task' => $task, 'files' => $files], 201);
} catch (Exception $e) {
Log::error($e->getMessage());
return response(['error' => $e->getMessage()], 500);
}
}
}
And here is StoreForm what is a form request
<?php
namespace App\Http\Requests\UserRegisterationOperation;
use App\Models\User;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\DB;
class StoreForm extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string',
'desc' => 'required|string',
// etc
];
}
/**
* Get custom attributes for validator errors.
*
* @return array
*/
public function attributes()
{
return [
'name' => 'Name',
'desc' => 'Desciption',
// etc
];
}
public function persist()
{
return DB::transaction(function () {
$user = $this->createUser();
$task = $this->createTask($user);
$files = $this->createFiles($user);
return [$user, $task, $files];
});
}
protected function createUser()
{
return User::firstOrCreate(
['email' => $this->email],
['name' => $this->name, 'password' => bcrypt($this->password)]
);
}
protected function createTask($user)
{
return $user->tasks()->create([
'name' => $this->name,
'desc' => $this->task_desc
]);
}
protected function createFiles($user)
{
$files = collect($this->allFiles())->map(function ($file, $key) {
return [
'name' => $key,
'path' => $file->store('photos')
];
})->values()->toArray();
$user->uploads()->insert($files);
return $files;
}
}
So you have there a validation, which is very important.
Please or to participate in this conversation.