Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
Cómo hacer una clase separada para hacer un nuevo registro en la mesa y llamarlo a través de una interfaz o una fachada en el controlador

Quiero refactorizar mi código de controlador que está creando un nuevo registro en los products de la tabla:

 public function store(Request $request) { $newPro = Product::create([ 'name' => $request->product_name, 'en_name' => $request->product_name_english, 'type' => $request->product_type, 'cat_id' => $request->category_product, ]); ... }

Ahora, para refactorizar este código, tengo dos opciones:

1- Cree un método separado en el mismo controlador y luego llámelo así:

 $newPro = self::createProduct($request->product_name,$request->product_name_english,$request->product_type,$request->category_product)

2- Cree una Clase separada y llámela por la interface o facade (Mejor manera)

Ahora quería usar la segunda opción, ¡pero realmente no sé cómo hacerlo!

Así que si sabes por favor házmelo saber...

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Primero cree su proveedor de servicios de repositorio:

 php artisan make:provider RepositoryServiceProvider

luego debe asignar la interfaz y su repositorio (por supuesto, debe crear ProductRepository y ProductRepositoryInterface también) dentro del método de registro como:

 /** * Register services. * * @return void */ public function register() { $this->app->bind(ProductRepositoryInterface::class, ProductRepository::class); }

después de eso, puede inyectar su repositorio en su controlador como:

 public function store(Request $request, ProductRepositoryInterface $productRepository) { $newPro = $productRepository->createProduct($productData); ... }

Aquí está su repositorio de productos :

 <?php namespace App\Repositories; class ProductRepository extends BaseRepository implements ProductRepositoryInterface { protected Product $product; /** * @param Product $product */ public function __construct(Product $product) { $this->product = $product; } /** * @param array $productArray * @return Product */ public function createProduct(array $productArray): Product { return $this->product->create($productArray); } }

y su ProductRepositoryInterface:

 <?php namespace App\Repositories; interface CategoryRepositoryInterface { /** * @param array $productArray * @return Product */ public function createProduct(array $productArray): Product; }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!