Today, here’s a dedicated Laravel component: Sight!
Laravel is one of the fastest. But now, with Sight, you can develop faster.
What does Sight do?
Sight implements a Presenter layer on the Server Side. This allows you to easily convert the data retrieved from the server into presentable data. Since Sight, Laravel is the only framework that supports Server Side’S MVP mode.
Why use Sight?
First, speed up development.
Second, domestic Phper all understand, big factory is prohibited SQL join table more than three tables.
FOR beginners, the database is queried in the FOR loop. And if you forbid that, then it’s also possible that they PLUCK the associated ID. Then, the nested FOR loop inside the FOR loop looks up the associated data. Sight has a nice Pluck function that, when you find the ID, requests the relevant data to Sight, and Sight concatenates the data for you. What it does is sort the data by associating ids with keys. Thus greatly improve the efficiency of the program.
Sight is fairly simple to use.
The following example, for 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 the code
In the example above, the code converts created_at from int to time and created_by from user ID to user name.
We see that:
Created_at uses MappingTypeEnum::METHOD_NAME. Where is that method? It’s in a press trait. So, you can also define your own traits.
Created_by reads user_name directly from the associative array because MappingTypeEnum::JOIN_FIELD is used.
The above code looks a bit long, but onRelationByObject() can be changed to onRelation() and the code is shorter.
Similarly, addFieldMappingByObject() is passed in as an array, and the code is also shorter.
Sight goes far beyond that. It supports ElasticSearch as well as MySQL.
Although it’s a pure array operation, it also has innerJoin and outerJoin, and hasOne, hasMany…
Of course, there are more features, which you’ll need to read the documentation carefully.
Sight tries to solve the problem of finding data and converting it into displayable data, and it does a really good job of Coding More Happy; Coding More Quickly!
Making address: https://github.com/BardoQi/SightCopy the code