Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Laravel Model uses Macroable to add macro capabilities to the data model

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

What is ThinkSNS?

ThinkSNS (TS for short), a platform-wide comprehensive social system, provides social software development and technical solutions for large, small and medium-sized enterprises and entrepreneurs at home and abroad.

Generate demand

When developing ThinkSNS Plus with Laravel, because many functional blocks are not written in a library, add actual functions in the form of an extension package, and the "polymorphic many-to-many" relationship is also used in many places. The problem comes, develop a question and answer program, want to add published questions or answered relationships to the user model, at first inherit a User model, add relationships, and then find the problem, because the user's tag uses polymorphic many-to-many relationships, I can not get this kind of relational data through the inherited user model because * able_type is the user data model class name or alias. And the class has changed since I inherited it.

Fulfill the requirements

Then it occurred to me that there was a Trait called Macroable in Laravel and found that Builder had this ability, but Model did not, and then added this Trait to the model to be used, and later found that if other models were to be used, would you want to add it again? Then a Trait was written:

Trait Macroable {use\ Illuminate\ Support\ Traits\ Macroable {_ _ call as macroCall;} / * Get a relationship value from a method. * * @ param string $key * @ return mixed * @ author Seven Du * / public function getRelationValue ($key) {$relation = parent::getRelationValue ($key); if (! $relation & & static::hasMacro ($key)) {return $this- > getRelationshipFromMethod ($key);} return $relation;} / * * Handle dynamic method calls into the model. * * @ param string $method * @ param array $parameters * @ return mixed * @ author Seven Du * / public function _ call ($method, $parameters) {if (static::hasMacro ($method)) {return $this- > macroCall ($method, $parameters);} return parent::__call ($method, $parameters);} / * * Handle dynamic static method calls into the method. * * @ param string $method * @ param array $parameters * @ return mixed * / public static function _ _ callStatic ($method, $parameters) {return parent::__callStatic ($method, $parameters);}}

Just use in the model you want to use.

Use

With this Trait, we can add functions to the User model using macro capabilities to add functions dynamically:

User::macro ('questions', function () {return $this- > hasMany (Question::class,' user_id', 'id');})

In this way, we can directly:

$questions = $user- > questions

Got all the questions posted by the user.

The above codes are all from ThinkSNS Plus. For the complete development code, you can see the warehouse:

GitHub: https://github.com/slimkit/thinksns-plus (it is not easy to open source, seek Star)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report