jeudi 17 octobre 2019

Laravel custom relationship with 'or' condition

Need to fetch the 'orders' for a 'user' based on the user_id OR email. E.g:

SELECT * FROM `orders` WHERE user_id = 123 OR (user_id IS NULL AND o.email = 'test@example.com')



<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{

    /**
     * Get users paid orders
     *
     * @var $this
     */
    public function orders()
    {
        return $this->hasMany('App\Models\Order')->where(['paid' => 1])->orderBy('created_at', 'DESC');
    }
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2OTJrqH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire