doubijiao2094 2016-10-12 06:18
浏览 46
已采纳

Laravel Eloquent,如何用公式定义表关系?

On Laravel Eloquent,

How can I define relation condition with something that not exist on the table but calculated from a value in the table.

For example :

quarters                transactions
 _________ _________    _________ ___________
|   name  |  desc   |  |   id  |  date        |
 _________ _________    _________ ___________
|     1   | Jan-Mar |  |     1   | 2016-04-01 |
|     2   | Apr-Jun |  |     2   | 2016-05-01 |
|     3   | Jul-Sep |  |     3   | 2012-07-21 |
|     4   | Oct-Dec |  |     4   | 2014-01-31 |
 _________ _________    _________ ___________

It's work fine with this

SELECT 
FROM transactions tx
INNER JOIN quarters q ON q.name = ((MONTH(tx.date)+2)/3)

Quarters.php

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Quarters extends Model{

}

Transactions.php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Transactions extends Model{

    public function quater(){
        return $this->belongsTo('App\Models\Quarters' , '????', 'name') ;
    }
}

What should I define in my model in order to be able to do: Transactions::where("some_field", "some_value")->with("quater")->get()

note: I want to do this without modify table structure.

  • 写回答

2条回答 默认 最新

  • drbhjey445647 2016-10-12 10:12
    关注

    I end up created my own Relation class

    <?php
    
    namespace App\Models;
    
    use \Illuminate\Database\Eloquent\Builder;
    use \Illuminate\Database\Eloquent\Relations\HasMany;
    use Illuminate\Database\Eloquent\Collection;
    use App\Models\Quarter;
    
    class HasManyQuarter extends HasMany {
    
        public function __construct($localKey, $object){
            $instance = new Quarter;
            $query = $instance->newQuery();
            $foreign_key = "name";
    
            parent::__construct($query, $object, $instance->getTable().'.'.$foreign_key, $localKey);
        }
    
        public function addEagerConstraints(array $models)
        {
            $keys = $this->getKeys($models, $this->localKey);
            $_keys = array();
            foreach($keys as $idx => $k){
                $_keys[$idx] = ( date("n", strtotime($k))+2 ) / 3; // Transform the Key
            }
    
            $this->query->whereIn($this->foreignKey, $keys);
        }
    
        public function match(array $models, Collection $results, $relation)
        {
            $dictionary = $this->buildDictionary($results);
    
            foreach ($models as $model) {
                $key = $model->getAttribute($this->localKey);
    
                $month = ( date("n", strtotime($key))+2 ) / 3; // Transform the Key
    
                if ( isset($dictionary[ $month ]) ) {
                    $value = $this->getRelationValue($dictionary, $month, 'many');
                    $model->setRelation($relation, $value);
                }
    
            }
            return $models;
        }
    }
    

    And my model have:

    public function quarter(){
       return new HasManyQuarter('date', $this);
    }
    

    I extends HasMany because it's suite my case where my real class are not transaction/quarter. but this can get anyone idea of how to implement it.

    The idea is overide addEagerConstraints to alter the query and also match() to match the results back to each records.

    Relations/HasMany.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?