2017 © Pedro Peláez
 

library laravel-morph-where-has

Fix whereHas for morphTo relations.

image

rackbeat/laravel-morph-where-has

Fix whereHas for morphTo relations.

  • Monday, July 9, 2018
  • by lasserafn
  • Repository
  • 1 Watchers
  • 1 Stars
  • 12 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Use whereHas for morphed relationships in Laravel

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

Total Downloads Latest Stable Version License , (*2)

Installation

You just require using composer and you're good to go!, (*3)

composer require rackbeat/laravel-morph-where-has

The Service Provider is automatically registered., (*4)

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., (*5)

Our solution, is that you define every possible morphed class. Like so:, (*6)

<?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

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

This will correctly query a relation with the type and any queries you've added., (*8)

Requirements

  • PHP >= 7.1

Inspiration

Solution based upon work by github@thisdotvoid - modified to fix some common issues., (*9)

The Versions

09/07 2018

dev-master

9999999-dev

Fix whereHas for morphTo relations.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rackbeat

database laravel eloquent mysql bug belongsto morphto morph-where-has where has laravel_reserved_0 laravel_reserved_ laravel_reserved

09/07 2018

1.0

1.0.0.0

Fix whereHas for morphTo relations.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Rackbeat

database laravel eloquent mysql bug belongsto morphto morph-where-has where has