如何创建一个单独的类来在表中创建新记录并通过控制器上的接口或外观调用它

发布于 2025-01-14 02:03:07 字数 830 浏览 4 评论 0原文

我想重构我的控制器代码,该代码在表 products 中创建新记录:

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,
       ]);
       ...
     }

现在为了重构此代码,我有两个选择:

1- 创建一个单独的方法在同一个控制器上,然后像这样调用它:

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

2- 创建一个单独的类并通过 interfacefacade 调用它(最好的方法)

现在我想使用第二个选项,但我不知道真如何!

所以如果你知道请告诉我...

I want to refactor my Controller code which is creating a new record at table products:

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,
       ]);
       ...
     }

Now in order to refactor this code, I have two options:

1- Create a separate method at the same Controller and then call it like this:

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

2- Create a separate Class and call it by the interface or facade (Best way)

Now I wanted to use the 2nd option but I don't know really how to!

So if you know please let me know...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2025-01-21 02:03:07

首先创建您的存储库服务提供者:

php artisan make:provider RepositoryServiceProvider

然后您应该在注册方法中映射接口和存储库(当然您也必须创建 ProductRepository 和 ProductRepositoryInterface),例如:

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

}

之后您可以将存储库注入到控制器中,例如:

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

这是您的 ProductRepository< /strong>:

<?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);
    }
}

和您的 ProductRepositoryInterface:

<?php

namespace App\Repositories;

interface CategoryRepositoryInterface
{
    /**
     * @param  array  $productArray
     * @return Product
     */
    public function createProduct(array $productArray): Product;
}

First create your repository service provider:

php artisan make:provider RepositoryServiceProvider

then you should map Interface and your Repository (ofcourse you must create ProductRepository and ProductRepositoryInterface too) inside register method like:

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

}

after that you can inject your repository to your controller like:

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

Here is your ProductRepository:

<?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);
    }
}

and your ProductRepositoryInterface:

<?php

namespace App\Repositories;

interface CategoryRepositoryInterface
{
    /**
     * @param  array  $productArray
     * @return Product
     */
    public function createProduct(array $productArray): Product;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文