Laravel控制器中的构造函数依赖注入问题

时间:2020-06-10 17:42:11

标签: php laravel

我第一次尝试在构造函数中进行laravel依赖注入,但是由于某些原因,我的productService变量返回null,这就是为什么我得到以下错误:

Call to a member function create() on null

productService应该找回Product模型的实例,在product模型中,我有一个创建方法,我遵循的是胖模型瘦控制器的愿景。

在我的ProductController中,我有这样的内容:

use Illuminate\Http\Request;
use App\Models\Product;


class ProductController extends Controller
{



    public $productService;


    // Constructor
    public function _construct(Product $product)
    {
        $this->productService = $product;
    }

    public function create(Request $request)
    {
        try
        {
            $this->productService->create($request);

            return response()->json([
                'product' => $product,
            ], 200);

        } 
        catch (\Exception $e)
        {
            return response()->json([
                'message' => $e->getMessage(),
            ], $e->getStatus() );
        }

    }

我通过axios调用在控制器中调用create方法,那我做错了什么?

0 个答案:

没有答案
相关问题