rackbeat/laravel-morph-where-has 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rackbeat/laravel-morph-where-has

最新稳定版本:2.0

Composer 安装命令:

composer require rackbeat/laravel-morph-where-has

包简介

Fix whereHas for morphTo relations.

README 文档

README

Usually, you cant say whereHas('contact') if contact is a morphTo relationship. This package aims to fix that.

Total Downloads Latest Stable Version License

Installation

You just require using composer and you're good to go!

composer require rackbeat/laravel-morph-where-has

The Service Provider is automatically registered.

Usage

1. Add possible variations in your model

The problem, is that the morph relationship can have a hard time determining how to handle the whereHas call.

Our solution, is that you define every possible morphed class. Like so:

<?php

class Invoice extends Model {
    // Old morph relation
    public function owner() {
        return $this->morphTo('owner'); 
    }
    
    // New solution
    public function customer() {
        return $this->morphTo('owner')->forClass(App\Customer::class);
    }
    
    public function supplier() {
        return $this->morphTo('owner')->forClass(App\Supplier::class);
    }
}

2. Use whereHas

Invoice::whereHas('supplier', function($query) {
    $query->whereName('John Doe');
})->get();

This will correctly query a relation with the type and any queries you've added.

Requirements

  • PHP >= 7.1

Inspiration

Solution based upon work by github@thisdotvoid - modified to fix some common issues.

统计信息

  • 总下载量: 57.88k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-09