douba2011 2016-10-30 06:09
浏览 66
已采纳

Blade Query与多个where和orWhere子句匹配某些id

I have a laravel project that I want to check if the user either owners or rents a property from a MySQLDB, I have a rewrite rule that allows them to visit a page and edit this property

Route::get('/housing/manage/{id}', function() {
        return view('pages.housing_management.housing_management');
    });

Now, I have this query that checks if they either own or rent it, but thats to check if theres any properties that match that rule, is where a way I can make sure that they own or rent the propertie with id = {id} that is sent with the page?

@if (count(Properties::where('owner', '=', Auth::user()->id)->orWhere('rented_by', '=', Auth::user()->id)->orderBy('id', 'DESC')->limit(1)->get()) > 0)
<p>Authentication Passed, you either own or rent this property.</p>
@endif
  • 写回答

2条回答 默认 最新

  • douqiju2520 2016-10-30 06:26
    关注

    I think the best practice for this case is adding two relations in the User Model to the Properties Model - Like this:

    public function ownedProperties() {
        return $this->hasMany(Properties::class, 'owner');
    }
    
    public function rentedProperties() {
        return $this->hasMany(Properties::class, 'rented_by');
    }
    

    And then your check is gonna be something like this:

    @if ($user->ownedProperties->contains($propertyId) || $user->rentedProperties->contains($propertyId))
       <p>Authentication Passed, you either own or rent this property.</p>
    @endif
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部