php - laravel 5.7 类不存在反射异常报错

标签 php laravel laravel-5.7

我的 api.php

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

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

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

ProductRepositary.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 类不存在 我试过了

  • Composer 转储自动加载
  • php artisan 配置:缓存
  • php artisan 缓存:清除
  • php artisan optimize:clear

文件在正确的目录中

  • app/Http/Repositories/ProductRepositary.php

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

给负分前,请给出解决方案并给负分

最佳答案

我认为 create_new_product 函数中的 ProductRepositary 类存在语法错误。你在那里漏掉了分号,所以添加分号。

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

希望你能理解。

关于php - laravel 5.7 类不存在反射异常报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53700413/

相关文章:

laravel - 图像无法显示,因为它包含错误 laravel

php - Laravel Horizo​​n 抛出错误 : Call to undefined function Laravel\Horizon\Console\pcntl_async_signals()

php - 自然排序 WordPress 帖子标题(按字母顺序和数字)?

php - 使用 php 爆炸数组作为 WHERE 的 MySQL 查询

php - Laravel $request->has ('name' )不起作用

php - 如何在 Laravel 关系中进行 INNER JOIN?

php - 字符集和数据问题

php - 用于查找和替换印度字符的正则表达式

laravel - Codeception,无法模拟ajax行为

php - laravel 5.7.15 419 抱歉,您的 session 已过期。请刷新并重试