Category : design-patterns

I’m developing an e-commerce website using Laravel 8, And I tried to change on Laravel folders structure I’ve put views folder inside new path modules/admins which works fine, and I’ve also created a Models folder in the modules/admins path which works fine, however, I am having trouble with the controllers folder. I still have the ..

Read more

There is a class in my project, for example CalculationService. This class has methods that manipulates data in it’s way. This class is injected in many places through application using the Laravel DI. Does it make sense to register it as singleton to avoid multiple instances through the application? class CalculationService { } class CarController ..

Read more

i’ve got a laravel project where service and repository classes are created as following: class UserService { public function save($data) { return User::create($data); } public function update($id, $data) { return User::where(‘id’, $id)->update($data); } public function delete($id) { return User::where(‘id’, $id)->delete(); } } class UserRepository { public function findAll() { return User::all(); } public function find($id) ..

Read more

I have two similar methods in my controller: First method: public function parseDataLayerEvent() { $events = $this->pixel_log->data[‘dataLayer’]; if (!is_array($events)) { throw new Exception(‘dataLayer is not an array’); } foreach ($events as $event) { if (!isset($event[‘event’])) { continue; } if (!isset($event[‘ecommerce’])) { continue; } if (!isset($event[‘ecommerce’][‘purchase’])) { continue; } $purchase = $event[‘ecommerce’][‘purchase’]; $validator = Validator::make($purchase, [ ..

Read more

I have two similar methods in my controller: First method: public function parseDataLayerEvent() { $events = $this->pixel_log->data[‘dataLayer’]; if (!is_array($events)) { throw new Exception(‘dataLayer is not an array’); } foreach ($events as $event) { if (!isset($event[‘event’])) { continue; } if (!isset($event[‘ecommerce’])) { continue; } if (!isset($event[‘ecommerce’][‘purchase’])) { continue; } $purchase = $event[‘ecommerce’][‘purchase’]; $validator = Validator::make($purchase, [ ..

Read more

I am working on a project, the previous programmer used this method to save photos, videos, etc. if (empty($data[‘large_image’])) { $large_image = $article->large_image;} else { if ($request->hasFile(‘large_image’)) { //uploads $cover_img_large = $data[‘cover_img_large’]; //echo ‘<pre>’; print_r($cover_path);die; if (file_exists($cover_img_large)) { unlink($cover_img_large); } $image_path = $data[‘large_image’]; $extension = $image_path->getClientOriginalExtension(); $New_path = rand(111111, 999999999) . ‘.’ . $extension; $path_path ..

Read more

Is there a design pattern or good practices for this kind of view inizialization code? In this case, depending on the parameter ($tipoPedido) it sets up the view in different ways: public function create(Request $request, RepositorioDepositos $repositorioDepositos, RepositorioEmpresas $repositorioEmpresas) { $tipoPedido = strtoupper($request->tipo); $datos = [‘action’ => "/api/pedidos", ‘tipo’ => $tipoPedido]; if ($tipoPedido == TipoRemitoStock::ESPECIAL) ..

Read more

I’m building a simple booking system in Laravel which is used by both admins and normal users. In my BookingController.php is have an index method to fetch all bookings. public function index(Request $request): Response { $limit = ($request->has(‘limit’)) ? $request->get(‘limit’) : 20; $bookings = $this->booking ->pushCriteria(new BelongsToTeam()) ->pushCriteria(new RequestWith()) ->pushCriteria(new ModelFilter()) ->orderBy(‘date’) ->paginate($limit); return Inertia::render(‘Bookings/Index’, ..

Read more

I have a question, why I need to use Laravel Service Container like this: $this->app->bind(‘HelpSpotAPI’, function ($app) { return new HelpSpotAPI($app->make(‘HttpClient’)); }); and call it in the controller like this: resolve(‘HelpSpotAPI’)->domeSomething() While we can create an instance directly like this: (new HelpSpotAPI)->domeSomething() Thank you! Sourc..

Read more