Home > PHP Framework > Laravel > Sight! A killer component that speeds up Laravel development is now open source!

Sight! A killer component that speeds up Laravel development is now open source!

藏色散人
Release: 2020-10-13 10:23:01
forward
2161 people have browsed it

The following is introduced to you by the Laravel tutorial column! , I hope it will be helpful to friends in need!

Sight! A killer component that speeds up Laravel development is now open source!

Today, I recommend a Laravel-specific component: Sight
Laravel development speed can be regarded as the fastest. However, if you add Sight now, your development speed will be even faster.
What did Sight do?
Sight implemented a Presenter layer on the Server Side. This allows you to easily convert the data retrieved from the server into displayable data. Since the introduction of Sight, Laravel has become the only framework that supports the MVP model of the Server Side.
Why use Sight?
First, it is to speed up development.
2. Domestic Phpers all know that large manufacturers prohibit SQL joins of more than three tables. For beginners, you will query the database in a FOR loop. If you have banned it, it is possible that they have PLUCKed out the relevant ID. Find out the results, and then nest a FOR loop within a FOR loop to check related associated data.
Sight provides a very good Pluck function. After finding the ID, request the relevant data and hand it over to Sight, and Sight will splice the data for you. What it does is organize the data by associating the ID with the KEY. This greatly improves program efficiency.
3. The use of Sight is quite simple.
For example, the following example is almost similar to the use of Model.

namespace App\Presenter

use Bardoqi\Sight\Presenter;
use Bardoqi\Sight\Traits\PresenterTrait;
use Bardoqi\Sight\Enums\MappingTypeEnum 
use Bardoqi\Sight\Enums\PaginateTypeEnum 
use App\Repositories\ArticleRepository;
use App\Repositories\UserRepository; 

class ArticlePresenter extents Presenter
{
   use PresenterTrait;

   public function getArticleList($where)
   {
       $articleArray = ArticleRepository::getList($where);
       $user_ids = $this->selectFields('id','title','created_at','created_by')
            ->fromLocal($articleArray,'articles')
            ->pluck('created_by');
       $users = UserRepository::getUsersWithIds($user_ids);
       $this->innerJoinForeign($users,'userss')
            ->onRelationByObject(Relation::of()
                ->localAlias('articles')
                ->localField('created_by')
                ->foreignAlias('users')
                ->foreighField('id')) 
            ->addFieldMappingByObject(FieldMapping::of()
                ->key('created_at')
                ->src('created_at')
                ->type(MappingTypeEnum::METHOD_NAME))
            ->addFieldMappingByObject(FieldMapping::of()
                ->key('created_by')
                ->src('user_name')
                ->type(MappingTypeEnum::JOIN_FIELD));         
       return $this->toPaginateArray(PaginateTypeEnum::PAGINATE_API);
   }
}
Copy after login

In the above example, the code converts created_at from int to time, and converts created_by from user id to user name.
We see that: created_at uses MappingTypeEnum::METHOD_NAME. Where is this method? It is in PresenterTrait. Therefore, you can also define your own Traits.
created_by directly reads the user_name in the associative array because MappingTypeEnum::JOIN_FIELD is used.
The above code seems a bit long, but onRelationByObject() can use onRelation() parameter passing method instead, and the code will be shorter.
Same as addFieldMappingByObject(), instead use addFieldMappingList() to pass in an array, and the code is shorter.

Sight is far more than just this feature. It not only supports data retrieved by MySQL, but also supports data retrieved by ElasticSearch.
Although it is a pure array operation, it also has innerJoin and outerJoin, and has hasOne, hasMany...
Of course, there are more functions, you have to read the documentation carefully.

Sight tries to solve the problem of converting the data into displayable data after you find it. It does a good job and can really make you Coding More Happy; Coding More Quickly!

Github address: https://github.com/BardoQi/Sight

Sight - the killer component that speeds up Laravel development is now open source! Hurry up FORK, Hurry up to STAR!

The above is the detailed content of Sight! A killer component that speeds up Laravel development is now open source!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template