doulv1760 2018-03-01 08:57
浏览 340
已采纳

如何在Laravel中的find()方法中指定两个或更多条件?

I'm having problems fetching data using Model::find() with two conditions. I want to return one record that has the given member_id AND its role is guest:

$member = CalendarMembers::find(["member_id" => $r->member_id, "role" => "guest"]);

But this isn't working. I have one user that when created, a new register is added to calendar_members table specifying this user as parent, yet, when attempting to fetch this user automatically created by just giving the member_id, it will be fetched, which SHOULD NOT.

I cannot use other ways such as:

$member = \DB::table("calendar_members")->where([ ["member_id", "=", $r->member_id], ["role", "=", "guest"] ])->first();

Because then $member->delete() or $member->destroy() will throw an error saying either delete or destroy do not exist.

  • 写回答

3条回答 默认 最新

  • duanlei20082008 2018-03-01 08:59
    关注

    The find() method uses ID to get a record. You need to use the where() and first() methods:

    CalendarMembers::where(['member_id' => $r->member_id, 'role' => 'guest'])->first();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里