Use joint with Eloquent easily
Here is how this package will be useful :) :
- Facilitated how to write join in your codes.
- Make aliases in your tables during your queries.
- Retrieve data from a query as a entity.
Start by downloading the package first
composer require zonkedcode/eloquent-join
Config\app.php
:
Register the service in your application configuration EloquentJoin\EloquentJoinServiceProvider::class,
Publish the package configuration :
php artisan vendor:publish --tag=eloquent-join
The package subtracts the names of aliases from names of your models. By default it takes into account a single character, you are free to specify the number of characters to take into account in the configuration of the package config\eloquent-join.php
.
Using the package
All your models must use the trait
EloquentJoin\EloquentJoinBuilder
<?php
namespace App\User;
use EloquentJoin\EloquentJoinBuilder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use EloquentJoinBuilder;
}
Said it's not boring, thankfully the package saves you time by adding artisan commands following :
- Creating a model by automatically adding this trait
php artisan join:model Post
- Creating an entity :
php artisan join:entity Post
- Creating a repository :
php artisan join:repository Post
- Creating a full model: This command combines the three commands above.
php artisan join:init Post
join:repository
You wonder by chance what is the order When you are building a Laravel application, do you often wonder how to organize my codes? Very good question, the package advises you to create a repository file for each model. It is now in this repository file that you will start writing all of your methods that interact with your tables.
If you want to give a custom alias name to a model :
<?php
namespace App\User;
use EloquentJoin\EloquentJoinBuilder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use EloquentJoinBuilder;
/**
* The attribute will contain alias table.
*
* @var mixed
*/
protected $aliasTable = 'use';
}
If you initialize this variable to false, you do not want to just have an alias
<?php
namespace App\User;
use EloquentJoin\EloquentJoinBuilder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use EloquentJoinBuilder;
/**
* The attribute will contain alias table.
*
* @var mixed
*/
protected $aliasTable = false;
}
You have the full possibility of customizing the length of an alias for a model.
<?php
namespace App\User;
use EloquentJoin\EloquentJoinBuilder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use EloquentJoinBuilder;
/**
* The attribute will contain length alias table.
*
* @var int
*/
protected $aliasLength = 3;
}
How to join
Imagine we have this relationship in our database
Users
id
name
created_at
Posts
id
user_id
title
We want to recover all Posts created by users
So the package requires us to save this join description in a model between the two usually the one that serves as a parent.
<?php
namespace App\User;
use EloquentJoin\EloquentJoinBuilder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use EloquentJoinBuilder;
/**
* We record the description of the joint.
*
* @return \EloquentJoin\Relations\HasJoin
*/
public function post()
{
return $this->hasInnerJoin(\App\Post::class);
}
}
Methods available :
- hasRightJoin,
- hasLeftJoin,
- hasInnerJoin,
Now when we call this method at runtime, the package will convert this description to an SQL query. It does the matching by taking the primary key of the model User and the foreign key in the format user_id.
Either you specify them simply :
<?php
namespace App\User;
use EloquentJoin\EloquentJoinBuilder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use EloquentJoinBuilder;
/**
* We record the description of the joint.
*
* @return \EloquentJoin\Relations\HasJoin
*/
public function post()
{
return $this->hasInnerJoin(\App\Post::class, 'u.id', 'p.user_id', '=');
}
}
You have to put the keys by specifying tables or aliases if you had allowed it. The fourth parameter is the comparison of the join, if you do not specify the equality (=) is default.
Using join in our queries
We call the static withJoin method by setting the names of your joins.
Example 1
App\User::withJoin('post')->get();
Example 2 :
App\User::withJoin('post', 'account')
->whereKey(1)
->where('p.title', 'Thanks')
->get([
'p.title',
'a.amount',
'u.name'
]);
Use of entities
If you want to recover the data as an entity, you must first dispose of this file using previously viewed commands.
Then you create properties in the entity that correspond to the fields in the table as well as getters.
<?php
class UserEntity
{
private $id;
private $name;
private $created_at;
/**
* Get Id
*
*/
public function getId()
{
return $this->id;
}
/**
* Get Name
*
*/
public function getName()
{
return $this->name;
}
/**
* Get Created_at
*
*/
public function getCreatedAt()
{
return $this->created_at;
}
}
Now we call the getEntity method to retrieve data as an entity :
App\User::whereKey(1)->getEntity();
This method getEntity tries to find the entity file that corresponds to the model in question and makes a reflection by initializing all the properties that correspond to the field names of the table.
You can simply mean the properties to initialize.
App\User::whereKey(1)->getEntity(['name']);
This note is an English translation, I think this package will almost overcome your problems under Eloquent ORM.
All your concerns at chriskuika12@gmail.com