草庐IT

php - laravel belongstomany 有条件

coder 2024-04-10 原文

我有以下模型。

class Training extends \Eloquent {

    // Add your validation rules here
    public static $rules = [
        'name' => 'required',
        'city' => 'required',
        'province' => 'required',
        'budget_year' => 'required|integer',
        's_date' => 'required|date',
        'e_date' => 'required|date'
    ];

    // Don't forget to fill this array
    protected $fillable = [
        'name',
        'city',
        'province',
        'budget_year',
        's_date',
        'e_date'
    ];

    public function material(){
        return $this->hasMany('Material');
    }

    public function budget(){
        return $this->belongsToMany('Budget')->withPivot('amount');
    }

    public function budgetById($training_id){
        $this->belongsToMany('Budget')->where('training_id', '=', $training_id)->get();
    }

}

当我使用DB::getQueryLog调试budgetById方法时,查询如下

select budgets.*, 
budget_training.training_id as pivot_training_id, 
budget_training.budget_id as pivot_budget_id 
from budgets inner join budget_training on budgets.id = budget_training.budget_id 
where budget_training.training_id is null and training_id='6'

它返回 0 行,但是当我尝试修改查询并在 pgadmin 中运行它时,以下脚本运行良好。

select budgets.*, 
budget_training.training_id as pivot_training_id, 
budget_training.budget_id as pivot_budget_id 
from budgets inner join budget_training on budgets.id = budget_training.budget_id 
where budget_training.training_id='6'

注意我从 Laravel 生成的查询中删除了 training_id is null and。我的 budgetById 方法有什么问题?

最佳答案

你已经调用了 get() 而没有在这里使用 return:

public function budgetById($training_id){
    // = in where is optional in this case
    $this->belongsToMany('Budget')->where('training_id', '=', $training_id);
}

你应该这样使用:

public function budgetById($training_id){
    // = in where is optional in this case
    return $this->belongsToMany('Budget')->where('training_id', '=', $training_id);
}

关于php - laravel belongstomany 有条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27163716/

有关php - laravel belongstomany 有条件的更多相关文章

  1. ruby - 如何根据特征实现 FactoryGirl 的条件行为 - 2

    我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden

  2. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  3. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  4. ruby-on-rails - 使用包含多个关联和单独的条件 - 2

    我的Gallery模型中有以下查询:media_items.includes(:photo,:video).rank(:position_in_gallery)我的图库模型有_许多媒体项,每个都有一个照片或视频关联。到目前为止,一切正常。它返回所有media_items包括它们的photo或video关联,由media_item的position_in_gallery属性排序。但是我现在需要将此查询返回的照片限制为仅具有is_processing属性的照片,即nil。是否可以进行相同的查询,但条件是返回的照片等同于:.where(photo:'photo.is_processingIS

  5. ruby-on-rails - 在 haml View 中重构条件 - 2

    除了可访问性标准不鼓励使用这一事实指向当前页面的链接,我应该怎么做重构以下View代码?#navigation%ul.tabbed-ifcurrent_page?(new_profile_path)%li{:class=>"current_page_item"}=link_tot("new_profile"),new_profile_path-else%li=link_tot("new_profile"),new_profile_path-ifcurrent_page?(profiles_path)%li{:class=>"current_page_item"}=link_tot("p

  6. ruby-on-rails - 在具有 ActiveRecord 条件的相关模型中按字段排序 - 2

    我正在尝试按Rails相关模型中的字段进行排序。我研究的所有解决方案都没有解决如果相关模型被另一个参数过滤?元素模型classItem相关模型:classPriority我正在使用where子句检索项目:@items=Item.where('company_id=?andapproved=?',@company.id,true).all我需要按相关表格中的“位置”列进行排序。问题在于,在优先级模型中,一个项目可能会被多家公司列出。因此,这些职位取决于他们拥有的company_id。当我显示项目时,它是针对一个公司的,按公司内的职位排序。完成此任务的正确方法是什么?感谢您的帮助。PS-我

  7. ruby - 如果满足给定条件,则结束 ruby​​ 程序 - 2

    基本上,我只是试图在满足特定条件时停止程序运行其余行。unlessraw_information.firstputs"Noresultswerereturnedforthatquery"breakend然而,在程序运行之前我得到了这个错误:Invalidbreakcompileerror(SyntaxError)执行此操作的正确方法是什么? 最佳答案 abort("Noresultswerereturnedforthatquery")unlesscondition或unlessconditionabort("Noresultswer

  8. ruby-on-rails - 如果条件与 &&,是否有任何性能提升 - 2

    如果用户是所有者,我有一个条件来检查说删除和文章。delete_articleifuser.owner?另一种方式是user.owner?&&delete_article选择它有什么好处还是它只是一种写作风格 最佳答案 性能不太可能成为该声明的问题。第一个要好得多-它更容易阅读。您future的自己和其他将开始编写代码的人会为此感谢您。 关于ruby-on-rails-如果条件与&&,是否有任何性能提升,我们在StackOverflow上找到一个类似的问题:

  9. ruby - 与条件正则表达式作斗争 - 2

    我有一个简单的问题,但我无法解决这个问题。我的字符串格式为ID:dddd,具有以下正则表达式:/^ID:([a-z0-9]*)$/或者如下:ID:1234Status:232,所以用下面的正则表达式:/^ID:([a-z0-9]*)Status:([a-z0-9]*)$/现在我想制作一个可以处理两者的正则表达式。我想到的第一件事是:/^ID:([a-z0-9]*)$|^ID:([a-z0-9]*)Status:([a-z0-9]*)$/它匹配,但我正在研究条件正则表达式,并认为应该可以按照(伪代码)ifthestringcontains/Status://^ID:([a-z0-9]*)

  10. ruby - 重构条件变量赋值 - 2

    我正在做一个项目。目前我有一个相当大的条件语句,它根据一些输入参数为变量赋值。所以,我有这样的东西。ifsomeconditionx=somevalueelsifanotherconditionx=adifferentvalue...重构它的最佳方法是什么?我希望我最终会得到类似的东西x=somevalueifsomecondition||anothervalueifanothercondition这种事情有规律吗? 最佳答案 只需将赋值放在if之外即可。x=ifsomeconditionsomevalueelsifanotherc

随机推荐