laravel 5.7 中类不存在反射异常错误

2024-04-06

我的 api.php

Route::get('getProducts' , 'ProductController@getProducts');

产品控制器.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Services\ProductService;


class ProductController extends Controller
{
    private $productService;

    public function __construct(ProductService $productService){
        $this->productService = $productService;
    }

    public function getProducts(){
        return $this->productService->get_products();
    }

    public function addProduct(Request $request){
        $all_data = array(
            'product_name' => $request['product_name'],
            'product_code' => $request['product_code'],
            'category_id' => $request['category_id'],
            'sub_category_id' => $request['sub_category_id'],
            'unit' => $request['unit']
        );
        return $this->productService->create_product($all_data);
    }
}

产品服务.php

namespace App\Http\Services;


use App\Http\Repositories\ProductRepositary;


    class ProductService
    {
        protected $productRepositary;
        public function __construct(ProductRepositary $productRepositary){
                $this->productRepositary = $productRepositary;
        }


        public function get_products(){
            return $this->productRepositary->get_products();
        }

        public function create_product($data){
            return $this->productRepositary->create_new_product($data);
        }
    }

产品库.php

<?php

namespace App\Http\Repositories;

use App\Product;

/**
 * 
 */
Class ProductRepositary
{
    public function getModel(){
        return new Product;
    }

    public function get_products(){
        $all_data = $this->getModel()->all();
        return $all_data;
    }

    public function create_new_product($data){
        $created = $this->getModel()->firstOrCreate($data)
        return $created;
    }
}

一切看起来都很好,我不知道我错过了什么 但我收到这个错误类 App\Http\Repositories\ProductRepositary 不存在我努力了

  • 作曲家转储自动加载
  • php artisan 配置:缓存
  • php artisan 缓存:清除
  • php artisan 优化:清除

该文件位于正确的目录中

  • 应用程序/Http/Repositories/ProductRepositary.php

一切都存在,但仍然存在错误,是否有人遇到同样的问题,您找到的解决方案是什么?

在给出否定之前,请提供解决方案并给出否定分数


我认为你的语法有错误ProductRepositary里面的类create_new_product功能。您错过了那里的分号,因此添加分号。

public function create_new_product($data){
    $created = $this->getModel()->firstOrCreate($data); // Here you have missed semicolon
    return $created;
}

我希望你能理解。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

laravel 5.7 中类不存在反射异常错误 的相关文章

随机推荐