在 Laravel 5.7 中出现反射异常错误,提示类不存在。

4

我的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;
    }
}

Everything looks fine , i don't know where i'm missing but i'm getting this error Class App\Http\Repositories\ProductRepositary does not exist i have tried

  • composer dump-autoload
  • php artisan config:cache
  • php artisan cache:clear
  • php artisan optimize:clear

the file is inside right directory

  • app/Http/Repositories/ProductRepositary.php

everything but still error is there , does anyone faced the same issue and what was the solution you have found for that?

before giving negative, please provide the solution and give negative score


ProductRepositary.php 文件是否在正确的目录中?另外顺便提一下,您拼写了 "Repository" 错误。 - Mike
是的,它在app/http/Repositories/ProductRepositary.php文件中。 - Vijay
http 还是 Http - Mike
仅限于 Http 内部。 - Vijay
使用小写的'class'来定义ProductRepositary,我不确定这是否能解决问题。但是可以试一下。 - Rakesh Mishra
显示剩余2条评论
1个回答

3

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

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

我希望您能理解。


@VijayKumar 恭喜啊,我几天前也遇到了同样的问题 :D - Sagar Gautam
我有类似的问题,我仔细检查了每个分号的位置,但错误仍然存在。 - Sebastian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接