栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

BelongsToMany.php-1

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BelongsToMany.php-1

namespace IlluminateDatabaseEloquentRelations;

 

use IlluminateSupportArr;

use IlluminateSupportStr;

use IlluminateDatabaseEloquentModel;

use IlluminateDatabaseEloquentBuilder;

use IlluminateDatabaseEloquentCollection;

use IlluminateDatabaseEloquentModelNotFoundException;

// yesterday we just say this belongsTo

// today we just use this belongToMany

class BelongsToMany extends Relation

{// Belongs To Many just a relation

   

    protected $table; // the middle table , ha ha

   // intermediate: like middle ,

   // this is the intermediate table for the relation

 

   

    protected $foreignKey;// The foreign key of the parent model.

 

   

    protected $otherKey;// the relation key: like the associated key of the relation

 

   

    protected $relationName;//The "name" of the relationship.

 

   

    protected $pivotColumns = [];//The pivot table columns to the retrieve

   // retrieve like: search,

   // this is main table columns to be search, or retrieve.

 

   

    protected $pivotWheres = [];// Any pivot table restrictions.

   // a pivot table array store about this be restrictions.

 

   

    protected $pivotCreatedAt;// The custom pivot table column for the create_at timestamp.

   //The custom pivot table column

   // just for the created_at timestamp

 

   

    protected $pivotUpdatedAt;

   // The custom pivot table column

   // for updated_at timestamp.

 

   

    public function __construct(Builder $query, Model $parent, $table, $foreignKey, $otherKey, $relationName = null)

    {//Create a new belongs to many relationship instance.

        $this->table = $table;// set table

        $this->otherKey = $otherKey;// set the other Key

        $this->foreignKey = $foreignKey;//set foreignKey

        $this->relationName = $relationName;//set relationName

 

        parent::__construct($query, $parent);// use parent construct function

    }

 

   

    public function getResults()

    {

        return $this->get();

    }// Get the result of the relationship.

   // just a get result

 

   

    public function wherePivot($column, $operator = null, $value = null, $boolean = 'and')

    {//Set a where clause for a pivot table column.

        $this->pivotWheres[] = func_get_args();// this pivotWheres get args

 

        return $this->where($this->table.'.'.$column, $operator, $value, $boolean);

    }// just where

 

   

    public function orWherePivot($column, $operator = null, $value = null)

    {

        return $this->wherePivot($column, $operator, $value, 'or');

    }// set clause in or where

   // just a wrap function with the wherePivot

 

   

    public function first($columns = ['*'])

    {

        $results = $this->take(1)->get($columns);// get the first result

 

        return count($results) > 0 ? $results->first() : null;// get the first result

    }//execute like fire: the query and get the first result.

 

   

    public function firstOrFail($columns = ['*'])

    {// first or fail

        if (! is_null($model = $this->first($columns))) {

            return $model;

        }// has this first result ,just return it

// other throw new exception

        throw new ModelNotFoundException;

    }

 

   

    public function get($columns = ['*'])

    {// Execute the query as a "select" statement.

 

        // First we'll add the proper select columns onto the query so it is run with

        // the proper columns. Then, we will get the results and hydrate out pivot

        // models with the result of those columns as a separate model relation.

        $columns = $this->query->getQuery()->columns ? [] : $columns;

//First we'll add the proper select columns onto the query so it is run with the proper columns.

       //Then ,we will get the results and hydrate out pivot models with the result of those

       //columns as a separate model relation.

        $select = $this->getSelectColumns($columns);// get the select columns

 

        $builder = $this->query->applyScopes();// apply Scopes ,fire to builder

 

        $models = $builder->addSelect($select)->getModels();// get models

 

        $this->hydratePivotRelation($models);// hydrate Pivot Relation

 

        // If we actually found models we will also eager load any relationships that

        // have been specified as needing to be eager loaded. This will solve the

        // n + 1 query problem for the developer and also increase performance.

        if (count($models) > 0) {

            $models = $builder->eagerLoadRelations($models);

        }//loader other developer

 

        return $this->related->newCollection($models);// make a new collection

    }

 

   

    public function paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)

    {//Get a paginator for the "select" statement.

        $this->query->addSelect($this->getSelectColumns($columns));

// add select columns for this query

        $paginator = $this->query->paginate($perPage, $columns, $pageName, $page);

// set the paginate

        $this->hydratePivotRelation($paginator->items());

// hydrate Pivot Relation

        return $paginator;

    }

 

   

    public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'page')

    {//Paginate the given query into a simple paginator.

        $this->query->addSelect($this->getSelectColumns($columns));

// set select

        $paginator = $this->query->simplePaginate($perPage, $columns, $pageName);

// get the simple Paginate

        $this->hydratePivotRelation($paginator->items());

// hydrate Relation the result

        return $paginator;

    }//return result

 

   

    public function chunk($count, callable $callback)

    {//Chunk the results of the query.

        $this->query->addSelect($this->getSelectColumns());

// query addSelect  get Select Columns

        return $this->query->chunk($count, function ($results) use ($callback) {

            $this->hydratePivotRelation($results->all());

 

            return $callback($results);

        });// return the chunk

    }

 

   

    protected function hydratePivotRelation(array $models)

    {//Hydrate the pivot table relationship on the models.

 

        // To hydrate the pivot relationship, we will just gather the pivot attributes

        // and create a new Pivot model, which is basically a dynamic model that we

        // will set the attributes, table, and connections on so it they be used.

        foreach ($models as $model) {

            $pivot = $this->newExistingPivot($this->cleanPivotAttributes($model));

 

            $model->setRelation('pivot', $pivot);

        }// loop this models

       // and set the pivot about this model

    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/228171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号