Laravel、Elount和Kyslik排序合并表?

Laravel、Elount和Kyslik排序合并表?,laravel,sorting,join,eloquent,Laravel,Sorting,Join,Eloquent,我又卡住了。这一次使用3个联接表的排序链接。 我正在这样构建我的查询: ... $query= Interaction::where('interactions.user_id', $id)->where('interactions.active', '1'); $query->join('contacts','interactions.contact_id','=','contacts.id'); $query->join('products','interactions.

我又卡住了。这一次使用3个联接表的排序链接。 我正在这样构建我的查询:

...
$query= Interaction::where('interactions.user_id', $id)->where('interactions.active', '1'); 
$query->join('contacts','interactions.contact_id','=','contacts.id');
$query->join('products','interactions.product_id','=','products.id');

$contact_username = request('contact_username');
$contact_name = request('contact_name');
$contact_type = request('contact_type');
$product_name = request('product_name');
$platform = request('platform');
$dateMode = request('dateMode');
$date = request('date');
$completed_at = request('completed_at');
$notesMode = request('notesMode');
$notes = request('notes');

//settings up some filters based on the request:
if ($contact_username) $query->whereJsonContains('contacts.username', $contact_username);
if ($contact_name) $query->where('contacts.name', $contact_name);
if ($contact_type) $query->where('contacts.type', $contact_type);
if ($product_name) $query->where('products.name', $product_name);
if ($platform) $query->where('interactions.platform', $platform);
if ($notes && $notesMode=='+') $query->where('interactions.notes', 'like', "%".$notes."%");
if ($notes && $notesMode=='-') $query->where('interactions.notes', 'not like', "%".$notes."%");
if ($date && $dateMode) $query->whereDate('date', $dateMode, $date);
if ($completed_at && $dateMode) $query->whereDate('completed_at', $dateMode, $completed_at);

//and finally:
$interactions = $query->sortable()->paginate(20,['interactions.*','contacts.username','contacts.name','contacts.type','products.name']);
...
$query= Interaction::where('interactions.user_id', $id)->where('interactions.active', '1')->with(['contact','product']);
//the two JOIN rows that were here were dropped. Everything else stayed the same.
public function product() { return $this->belongsTo(Product::class); }
public function contact() { return $this->belongsTo(Contact::class); }
在我看来,我有:

<th>@sortablelink('contact.username','Username')</th>
<th>Platform</th>
<th>Type</th>
<th>@sortablelink('contact.name','Name')</th>
<th>@sortablelink('product.name','Product')</th>
<th>@sortablelink('date','Date')</th>
<th>@sortablelink('notes','Notes')</th>
@sortablelink('contact.username','username'))
站台
类型
@sortablelink('contact.name','name')
@sortablelink('product.name','product')
@sortablelink('date','date')
@可排序链接('notes','notes')
最后两个链接工作得很好,所有内容都按其应有的顺序排序,因为它们来自“交互”表,但应按连接的“产品”和“联系人”表中的列排序的链接失败

当@sortablelink中的这些列来自联接表时,引用它们的正确方法是什么?


PS:我还有另外两个视图,当只使用“Products”和“Contacts”表时,它们工作正常,并且这些表中每列的排序工作正常,因此我知道模型设置正确。

解决方案非常简单,但我不得不睡一觉,然后在早上再次解决这个问题。:) 我没有手动连接其他两个表,而是这样做:

...
$query= Interaction::where('interactions.user_id', $id)->where('interactions.active', '1'); 
$query->join('contacts','interactions.contact_id','=','contacts.id');
$query->join('products','interactions.product_id','=','products.id');

$contact_username = request('contact_username');
$contact_name = request('contact_name');
$contact_type = request('contact_type');
$product_name = request('product_name');
$platform = request('platform');
$dateMode = request('dateMode');
$date = request('date');
$completed_at = request('completed_at');
$notesMode = request('notesMode');
$notes = request('notes');

//settings up some filters based on the request:
if ($contact_username) $query->whereJsonContains('contacts.username', $contact_username);
if ($contact_name) $query->where('contacts.name', $contact_name);
if ($contact_type) $query->where('contacts.type', $contact_type);
if ($product_name) $query->where('products.name', $product_name);
if ($platform) $query->where('interactions.platform', $platform);
if ($notes && $notesMode=='+') $query->where('interactions.notes', 'like', "%".$notes."%");
if ($notes && $notesMode=='-') $query->where('interactions.notes', 'not like', "%".$notes."%");
if ($date && $dateMode) $query->whereDate('date', $dateMode, $date);
if ($completed_at && $dateMode) $query->whereDate('completed_at', $dateMode, $completed_at);

//and finally:
$interactions = $query->sortable()->paginate(20,['interactions.*','contacts.username','contacts.name','contacts.type','products.name']);
...
$query= Interaction::where('interactions.user_id', $id)->where('interactions.active', '1')->with(['contact','product']);
//the two JOIN rows that were here were dropped. Everything else stayed the same.
public function product() { return $this->belongsTo(Product::class); }
public function contact() { return $this->belongsTo(Contact::class); }
我在“交互”模型中添加了如下关系:

...
$query= Interaction::where('interactions.user_id', $id)->where('interactions.active', '1'); 
$query->join('contacts','interactions.contact_id','=','contacts.id');
$query->join('products','interactions.product_id','=','products.id');

$contact_username = request('contact_username');
$contact_name = request('contact_name');
$contact_type = request('contact_type');
$product_name = request('product_name');
$platform = request('platform');
$dateMode = request('dateMode');
$date = request('date');
$completed_at = request('completed_at');
$notesMode = request('notesMode');
$notes = request('notes');

//settings up some filters based on the request:
if ($contact_username) $query->whereJsonContains('contacts.username', $contact_username);
if ($contact_name) $query->where('contacts.name', $contact_name);
if ($contact_type) $query->where('contacts.type', $contact_type);
if ($product_name) $query->where('products.name', $product_name);
if ($platform) $query->where('interactions.platform', $platform);
if ($notes && $notesMode=='+') $query->where('interactions.notes', 'like', "%".$notes."%");
if ($notes && $notesMode=='-') $query->where('interactions.notes', 'not like', "%".$notes."%");
if ($date && $dateMode) $query->whereDate('date', $dateMode, $date);
if ($completed_at && $dateMode) $query->whereDate('completed_at', $dateMode, $completed_at);

//and finally:
$interactions = $query->sortable()->paginate(20,['interactions.*','contacts.username','contacts.name','contacts.type','products.name']);
...
$query= Interaction::where('interactions.user_id', $id)->where('interactions.active', '1')->with(['contact','product']);
//the two JOIN rows that were here were dropped. Everything else stayed the same.
public function product() { return $this->belongsTo(Product::class); }
public function contact() { return $this->belongsTo(Contact::class); }

我希望这会对某人有所帮助。昨晚我花了很多时间想弄清楚。该软件包只支持
HasOne/BelongsTo
关系,任何你需要建立自己的东西-谢谢,Kyslik。我只是浏览了软件包中的工作示例,并找到了一种使其工作的方法!:)我将在几分钟后在下面发布我的解决方案。谢谢你的大包!!!对于筛选,您可能会发现有用:)