我的 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 类不存在 我试过了
文件在正确的目录中
一切都存在,但仍然存在错误,有没有人遇到同样的问题,您找到的解决方案是什么?
给负分前,请给出解决方案并给负分
最佳答案
我认为 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/
我的模型有defself.empty_building//stuffend我怎样才能对这个现有的进行rspec?,已经尝试过:describe"empty_building"dosubject{Building.new}it{shouldrespond_to:empty_building}endbutgetting:Failure/Error:it{shouldrespond_to:empty_building}expected#torespondto:empty_building 最佳答案 你有一个类方法self.empty_bu
有几种方法:first_or_create_by、find_or_create_by等,它们的工作原理是:与数据库对话以尝试找到我们想要的东西如果我们找不到,就自己做保存到数据库显然,并发调用这些方法可能会使两个线程都找不到它们想要的东西,并且在第3步中一个线程会意外失败。似乎更好的解决方案是,创建或查找即:提前在您的数据库中创建合理的唯一性约束。如果你想保存一些东西,就保存它如果有效,那就太好了。如果它因为RecordNotUnique异常而无法工作,它已经存在,太好了,加载它那么在什么情况下我想使用Rails内置的东西而不是我自己的(看起来更可靠)create_or_find?
是否可以在不实际下载文件的情况下检查文件是否存在?我有这么大的(~40mb)文件,例如:http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm这与ruby不严格相关,但如果发件人可以设置内容长度就好了。RestClient.get"http://mirrors.sohu.com/mysql/MySQL-6.0/MySQL-6.0.11-0.glibc23.src.rpm",headers:{"Content-Length"=>100} 最佳答案
当且仅当模型存在时,我才尝试更新模型的值。如果没有,我什么都不做。搜索似乎只返回更新或创建问题/答案,但我不想创建。我知道我可以用一个简单的方法来做到这一点:found=Model.find_by_id(id)iffoundupdatestuffend但是,我觉得有一种方法可以在一次调用中完成此操作,而无需分配任何临时本地值或执行if。如果记录不存在,我该如何编写一个Rails调用来更新记录而不出现嘈杂错误?最新的Rails3.x 最佳答案 您可以使用try在对find_by_id或where的结果调用update_attribut
类似的东西:defdomain_exists?(domain)#performcheck#returntrue|falseendputs"valid!"ifdomain_exists?("example.com") 最佳答案 require'socket'defdomain_exists?(domain)beginSocket.gethostbyname(domain)rescueSocketErrorreturnfalseendtrueend 关于ruby-如何使用Ruby检查域是否存
我有很多模型和关系。由于这个事实,在View/Controller中有很多调用,看起来像这样:@object.something.with_something.value链的某些部分最终可能为零,这完全没问题。检查终端对象是否存在的正确/干净/快速的方法是什么?正在调用类似的东西:@object.something.with_something.valueifdefined?@object.something.with_something.value还可以吗? 最佳答案 在本地,您需要使用&&运算符(不是defined?),但这很快
我在Ruby中有一个哈希:hash=Hash.new里面有一些键值对,比如说:hash[1]="One"hash[2]="Two"如果散列包含键2,那么我想将“Bananas”添加到它的值中。如果散列没有键2,我想创建一个新的键值对2=>"Bananas"。我知道我可以通过首先使用has_key?检查散列是否具有key2来做到这一点,然后采取相应的行动。但这需要一个if语句和不止一行。那么是否有一种简单、优雅的单行代码可以实现这一目标? 最佳答案 这个有效:hash[2]=(hash[2]||'')+'Bananas'如果您希望所有
我想通过控制台手动创建一个用户:User.find_or_create_by(email:"user@mail.com",first_name:"Stan",last_name:"Smith",password:"password",password_confirmation:"password",confirmed_at:Time.now)我在过去的项目中多次这样做,但这次没有用。它没有获取Devise密码模型属性,所以这是我得到的错误:PG::UndefinedColumn:ERROR:columnusers.passworddoesnotexist我有Rails4.1.4和Dev
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
Model.exists?("lower(email)=?",params[:email].downcase)返回错误:ArgumentError(参数数量错误(2代表0..1)):是否可以使用不区分大小写的匹配来执行exists?? 最佳答案 您需要做的就是:Model.exists?(["lower(email)=?",params[:email].downcase])它正在寻找一个参数,但您提供了两个。使用数组形式和查找式条件应该可以满足您的需求。 关于ruby-on-rails-