مرحبا
آسف
أنا لست جيدا في الإنجليزية؛
البحث يعمل مع النماذج ذات الصلة
ولكن عندما أريد الفرز كحقل ذي صلة ، يتم تكرار ذلك
لأن ترجمة شرط حيث لا ينطبق في الفرز
اي حل ؟
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],
];
}
تم إصلاح هذه المشكلة بالفعل في الإصدار 8. إنه متوافق مع Laravel 5.4 ولكن به تغييرات جذرية. لاحظ أنه لم يتم وضع علامة عليه على أنه ثابت بعد. شكرا!
شكر كثيرا!
لم يتم إصلاح هذه المشكلة في الإصدار 8
يجب أن أفعل التغيير في أكواد بلدي؟
القضية هنا على ترجمة العلاقة أليس كذلك؟
public function translate()
{
return $this->hasOne(FoodTranslator::class,'food_id')
->where('lang',app()->getLocale());
}
القضية هي حيث لا يتم تطبيق الشرط؟ تمت معالجة هذا الأمر في الإصدار 8 ما لم تكن هناك مشكلة أخرى لا أراها؟
عند الطلب يتم تنفيذ 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
ربما حيث يجب أن يكون البند في الاستعلام الثاني وليس 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 أعتقد أنني أرى المشكلة الآن. سأحفر هذا أبعد عندما سنحت لي الفرصة.
شكرا جزيلا سيد :)
لم يتم إصلاح الخطأ؟
لقد أصلحت مشكلة تحرير فئة EloquentDataTable في مساحة الاسم Yajra \ DataTables
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
حيث بند للانضمام إلى الاستعلام
إذا كانت طريقة ما قد وجدت تلك العلاقة حيث سيتم تطبيق البنود على هذا النحو
سيكون جيدا جدا
aliworkshop شكرًا ، لكن الحل المقترح سيعمل فقط على قضيتك. هل يمكنك محاولة استخدام الرمز أدناه؟
يحل محل:
$this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type);
مع:
$this->query->join($table, $foreign, '=', $other, $type);
مشغول قليلاً في مشاريع العمل ولا يمكنني تخصيص الكثير من الوقت في نظام التشغيل مؤخرًا. شكرا!
مرحبا مرة أخرى سيد
أجد حلًا آخر لهذه المشكلة
إذا قمنا بتعديل فئة EloquentDataTable في مساحة الاسم Yajra \ DataTables على هذا النحو ، فمن المحتمل أنها تعمل
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']);
}
}
}
الرجاء تطبيق هذا الرمز في الإصدار القادم