我的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;
}
}
一切看起来很好,我不会错过,但我得到了这个错误 Class App\Http\Repositories\ProductRepositary does not exist 我试过了
-
composer dump-autoload
-
php artisan config:cache
-
php artisan cache:清楚
-
php artisan optimize:清楚
该文件位于右侧目录中
- app/Http/Repositories/ProductRepositary.php
但是仍然存在错误的一切,是否有人面临同样的问题,你找到的解决方案是什么?
在给予否定之前,请提供解决方案并给予负面评分
1 回答
我认为
create_new_product
函数中的ProductRepositary
类中存在语法错误 . 你错过了分号,所以添加分号 .我希望你能理解 .