μλ
μ£μ‘ν©λλ€
λλ μμ΄λ₯Ό μ λͺ»νλ€.
κ²μμ κ΄λ ¨ λͺ¨λΈκ³Ό ν¨κ» μλν©λλ€.
νμ§λ§ κ΄λ ¨ νλλ‘ μ λ ¬νκ³ μΆμ λ μ€λ³΅λ©λλ€.
μ λ ¬μ μ μ©λμ§ μλ where μ μ λ²μνκΈ° λλ¬Έμ
μ΄λ€ ν΄κ²°μ± ?
class OrderFood extends Model
{
protected $table = 'order_foods';
protected $fillable = [
'order_id','food_id','quantity','food_price'
];
public function order()
{
return $this->belongsTo(Order::class);
}
public function food()
{
return $this->belongsTo(Food::class)
->with('translate');
}
}
class Food extends Model
{
protected $table = 'foods';
protected $fillable = [
'title','date','tax','tax_enabled','tax_included','images','star','description','recipes','status','lang'
];
public function translate()
{
return $this->hasOne(FoodTranslator::class,'food_id')
->where('lang',app()->getLocale());
}
}
public function query()
{
DB::statement(DB::raw('set @rownum=0'));
DB::statement(DB::raw('SET collation_connection = utf8_general_ci'));
DB::statement(DB::raw('SET character_set_connection = utf8'));
$order_id = $this->order_id;
$order_foods = Order::find($order_id)->foods()->with(['food.translate'])->select(['*','order_foods.created_at',DB::raw('<strong i="5">@rownum</strong> := <strong i="6">@rownum</strong> + 1 AS rownum')]);
return $this->applyScopes($order_foods);
}
private function getColumns()
{
return [
['title' => trans('form.row'),'name' => 'rownum', 'data' => 'rownum','searchable'=>false],
['title' => trans('order.order_food'),'name' => 'food.translate.title', 'data' => 'food.translate.title','orderable'=>false],
['title' => trans('order.quantity'),'name' => 'quantity', 'data' => 'quantity'],
['title' => trans('order.food_price'),'name' => 'food_price', 'data' => 'food_price'],
['title' => trans('form.created_at'),'name' => 'created_at', 'data' => 'created_at'],
['title' => trans('form.action'),'name' => 'action', 'data' => 'action','searchable'=>false,'exportable'=>false,'printable'=>false],
];
}
μ΄ λ¬Έμ λ v8μμ μ΄λ―Έ μμ λμμ΅λλ€. Laravel 5.4μ νΈνλμ§λ§ μ£Όμ λ³κ²½ μ¬νμ΄ μμ΅λλ€. μμ§ μμ νκ·Έκ° μ§μ λμ§ μμμ΅λλ€. κ°μ¬ ν΄μ!
μ λ§ κ°μ¬ν©λλ€!
μ΄ λ¬Έμ λ v8μμ μμ λμ§ μμμ΅λλ€.
λ΄ μ½λλ₯Ό λ³κ²½ν΄μΌ ν©λκΉ?
μ¬κΈ°μ λ¬Έμ λ λ²μ κ΄κ³ λ§μ΅λκΉ?
public function translate()
{
return $this->hasOne(FoodTranslator::class,'food_id')
->where('lang',app()->getLocale());
}
λ¬Έμ λ where μ μ΄ μ μ©λμ§ μμ΅λκΉ? μ΄κ²μ λ΄κ° λ³΄μ§ λͺ»νλ λ€λ₯Έ λ¬Έμ κ° μλ ν v8μμ ν΄κ²°λμμ΅λκΉ?
μ£Όλ¬Έ μ 3κ°μ μΏΌλ¦¬κ° μ€νλ©λλ€.
1:
bindings:[]
query:"select count(*) as aggregate from (select '1' as `row_count` from `foods`) count_row_table"
time:0.29
2:
bindings:[]
query:"select *, <strong i="6">@rownum</strong> := <strong i="7">@rownum</strong> + 1 AS rownum from `foods` left join `food_translator` on `food_translator`.`food_id` = `foods`.`id` order by `food_translator`.`title` desc limit 10 offset 0"
time:0.81
3:
bindings:["fa", 1]
query:"select * from `food_translator` where `lang` = ? and `food_translator`.`food_id` in (?)"
time:0.46
μλ§λ where μ μ 3μ΄ μλ λ λ²μ§Έ 쿼리μ μμ΄μΌ ν©λλ€.
μ΄ μΏΌλ¦¬
query:"select * from `food_translator` where `lang` = ? and `food_translator`.`food_id` in (?)"
λ₯Ό μν
public function translate()
{
return $this->hasOne(FoodTranslator::class,'food_id')
->where('lang',app()->getLocale());
}
κ²°κ³Όμ μν₯μ λ―ΈμΉμ§ μλ
@aliworkshop μ§κΈ λ¬Έμ λ₯Ό λ³΄κ³ μλ κ²
κ°μ¬ν©λλ€ λ§μ€ν° :)
μ€λ₯κ° μμ λμ§ μμ΅λκΉ?
Yajra\DataTables λ€μμ€νμ΄μ€μ EloquentDataTable ν΄λμ€ νΈμ§ λ¬Έμ λ₯Ό μμ νμ΅λλ€.
protected function joinEagerLoadedColumn($relation, $relationColumn)
{
$table = '';
$lastQuery = $this->query;
foreach (explode('.', $relation) as $eachRelation) {
$model = $lastQuery->getRelation($eachRelation);
$fillable = [];
switch (true) {
case $model instanceof BelongsToMany:
$pivot = $model->getTable();
$pivotPK = $model->getExistenceCompareKey();
$pivotFK = $model->getQualifiedParentKeyName();
$this->performJoin($pivot, $pivotPK, $pivotFK);
$related = $model->getRelated();
$table = $related->getTable();
$tablePK = $related->getForeignKey();
$foreign = $pivot . '.' . $tablePK;
$other = $related->getQualifiedKeyName();
$lastQuery->addSelect($table . '.' . $relationColumn);
$this->performJoin($table, $foreign, $other);
break;
case $model instanceof HasOneOrMany:
$table = $model->getRelated()->getTable();
$foreign = $model->getQualifiedForeignKeyName();
$other = $model->getQualifiedParentKeyName();
$fillable = $model->getRelated()->getFillable();
break;
case $model instanceof BelongsTo:
$table = $model->getRelated()->getTable();
$foreign = $model->getQualifiedForeignKey();
$other = $model->getQualifiedOwnerKeyName();
break;
default:
throw new Exception('Relation ' . get_class($model) . ' is not yet supported.');
}
$this->performJoin($table, $foreign, $other,'inner',$fillable);
$lastQuery = $model->getQuery();
}
return $table . '.' . $relationColumn;
}
protected function performJoin($table, $foreign, $other, $type = 'left', $fillable = [])
{
$joins = [];
foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) {
$joins[] = $join->table;
}
if (! in_array($table, $joins)) {
$this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type);
if (in_array('lang',$fillable))
$this->query->where('lang', app()->getLocale());
}
}
μ΄ λ³κ²½μ lang
μ΄μ μλμΌλ‘ μ μ©ν©λλ€. where μ μ 쿼리λ₯Ό μ‘°μΈν©λλ€.
λ°©λ²μ΄ μ‘΄μ¬νλ€λ©΄ μ μ΄ λ€μκ³Ό κ°μ΄ μ μ©λλ κ΄κ³
μμ£Ό μ’μ κ²μ
λλ€
@aliworkshop κ°μ¬ν©λλ€. νμ§λ§ μ μλ μ루μ μ κ·νμ κ²½μ°μλ§ μλν©λλ€. μλ μ½λλ₯Ό μ¬μ©νμ¬ μλν΄ λ³Ό μ μμ΅λκΉ?
λ°κΎΈλ€:
$this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type);
μ ν¨κ»:
$this->query->join($table, $foreign, '=', $other, $type);
μμ νλ‘μ νΈμ μ½κ° λ°μκ³ μ΅κ·Όμλ OSμ λ§μ μκ°μ ν μ ν μ μμ΅λλ€. κ°μ¬ ν΄μ!
μλ
λ€μ λ§μ€ν°
λλμ΄ λ¬Έμ μ λν λ λ€λ₯Έ ν΄κ²°μ±
μ μ°Ύμ΅λλ€
Yajra\DataTables λ€μμ€νμ΄μ€μ EloquentDataTable ν΄λμ€λ₯Ό λ€μκ³Ό κ°μ΄ νΈμ§νλ©΄ μλ§λ μλν κ²μ
λλ€.
protected function joinEagerLoadedColumn($relation, $relationColumn)
{
$table = '';
$lastQuery = $this->query;
foreach (explode('.', $relation) as $eachRelation) {
$model = $lastQuery->getRelation($eachRelation);
switch (true) {
case $model instanceof BelongsToMany:
$pivot = $model->getTable();
$pivotPK = $model->getExistenceCompareKey();
$pivotFK = $model->getQualifiedParentKeyName();
$this->performJoin($pivot, $pivotPK, $pivotFK);
$related = $model->getRelated();
$table = $related->getTable();
$tablePK = $related->getForeignKey();
$foreign = $pivot . '.' . $tablePK;
$other = $related->getQualifiedKeyName();
$lastQuery->addSelect($table . '.' . $relationColumn);
$this->performJoin($table, $foreign, $other);
break;
case $model instanceof HasOneOrMany:
$table = $model->getRelated()->getTable();
$foreign = $model->getQualifiedForeignKeyName();
$other = $model->getQualifiedParentKeyName();
break;
case $model instanceof BelongsTo:
$table = $model->getRelated()->getTable();
$foreign = $model->getQualifiedForeignKey();
$other = $model->getQualifiedOwnerKeyName();
break;
default:
throw new Exception('Relation ' . get_class($model) . ' is not yet supported.');
}
$wheres = $model->getQuery()->getQuery()->wheres;
$this->performJoin($table, $foreign, $other,'inner',$wheres);
$lastQuery = $model->getQuery();
}
return $table . '.' . $relationColumn;
}
protected function performJoin($table, $foreign, $other, $type = 'left', $wheres = [])
{
$joins = [];
foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) {
$joins[] = $join->table;
}
if (! in_array($table, $joins)) {
$this->query->join($table, $foreign, '=', $other, $type);
foreach ($wheres as $where) {
if ($where['boolean'] == 'and')
$this->query->where($where['column'], $where['operator'], $where['value']);
else
$this->query->orWhere($where['column'], $where['operator'], $where['value']);
}
}
}
λ€μ λ²μ μμ μ΄ μ½λλ₯Ό μ μ©νμμμ€