2016-10-30 14:09
浏览 65

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>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • douqiju2520 2016-10-30 14: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>
    解决 无用
    打赏 举报

相关推荐 更多相似问题