标签关系

This commit is contained in:
2025-03-27 11:13:46 +08:00
parent 6e79dd47c4
commit aff152e40d
7 changed files with 90 additions and 5 deletions
+3 -3
View File
@@ -144,8 +144,8 @@ class CustomerController
} }
// 执行分页查询 // 执行分页查询 //关联标签tags //关联标签及分组tags.group
$query = Customer::where($where) $query = Customer::with('tags')->where($where)
->orderBy('id', 'desc'); ->orderBy('id', 'desc');
if (!Util::isEmpty($keyword)) { if (!Util::isEmpty($keyword)) {
$query->where(function ($query) use ($keyword) { $query->where(function ($query) use ($keyword) {
@@ -210,7 +210,7 @@ class CustomerController
return Util::fail([], 'id' . trans('not_null')); return Util::fail([], 'id' . trans('not_null'));
} }
$res = Customer::select('id', 'user_id', 'grade', 'channel_id', 'car_type_id', 'country_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'address', 'lat', 'lng', 'follow_next', 'follow_remark', 'sex') $res = Customer::with('tags')->select('id', 'user_id', 'grade', 'channel_id', 'car_type_id', 'country_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'address', 'lat', 'lng', 'follow_next', 'follow_remark', 'sex')
->where('id', $id) ->where('id', $id)
->first(); ->first();
if ($res) { if ($res) {
+34 -2
View File
@@ -2,6 +2,7 @@
namespace app\controller; namespace app\controller;
use app\model\CustomerTag;
use app\model\TagGroup; use app\model\TagGroup;
use app\util\Util; use app\util\Util;
use support\Request; use support\Request;
@@ -17,12 +18,12 @@ class TagGroupController
$language = $request->user['language']; $language = $request->user['language'];
foreach ($list as &$item) { foreach ($list as &$item) {
$item['names'] = unserialize($item['names']); $item['names'] = unserialize($item['names']);
if(in_array($language, $languageArr)){ if (in_array($language, $languageArr)) {
$item['name'] = $item['names'][$language]; $item['name'] = $item['names'][$language];
} }
foreach ($item['tags'] as &$tag) { foreach ($item['tags'] as &$tag) {
$tag['names'] = unserialize($tag['names']); $tag['names'] = unserialize($tag['names']);
if(in_array($language, $languageArr)){ if (in_array($language, $languageArr)) {
$item['name'] = $item['names'][$language]; $item['name'] = $item['names'][$language];
} }
} }
@@ -30,4 +31,35 @@ class TagGroupController
return Util::success($list); return Util::success($list);
} }
/**
* 更新客户标签关系
* @param Request $request
* @return \support\Response
*/
public function updatePivot(Request $request)
{
$customerId = Util::getParam($request, 'customer_id');
$groupId = Util::getParam($request, 'group_id');
$tagId = Util::getParam($request, 'tag_id');
if (Util::isEmpty($customerId)) {
return Util::fail([], trans('customer_id') . trans('not_null'));
}
if (Util::isEmpty($groupId)) {
return Util::fail([], trans('tag_group_id') . trans('not_null'));
}
if(empty($tagId)){
$res = CustomerTag::where(['customer_id' => $customerId, 'group_id' => $groupId])->delete();
}else{
$res = CustomerTag::updateOrCreate(['customer_id' => $customerId, 'group_id' => $groupId], ['tag_id' => $tagId]);
}
if($res){
return Util::success($res, trans('success'));
}else{
return Util::fail($res, trans('fail'));
}
}
} }
+9
View File
@@ -96,4 +96,13 @@ class Customer extends Model
return $customer; return $customer;
} }
/**
* 客户的标签
*/
public function tags()
{
return $this->belongsToMany(Tag::class, 'customer_tag', 'customer_id', 'tag_id')
->withTimestamps(); // 如果需要记录关联时间
}
} }
+36
View File
@@ -0,0 +1,36 @@
<?php
namespace app\model;
use app\util\Util;
use support\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class CustomerTag extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'customer_tag';
/**
* The primary key associated with the table.
*
* @var string
*/
protected $primaryKey = 'id';
//设置黑名单
protected $guarded = ['id', 'created_at'];
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
}
+4
View File
@@ -48,4 +48,8 @@ class Tag extends Model
return $this->belongsTo(TagGroup::class, 'group_id', 'id'); return $this->belongsTo(TagGroup::class, 'group_id', 'id');
} }
public function customers()
{
return $this->belongsToMany(Customer::class, 'customer_tag', 'tag_id', 'customer_id');
}
} }
+2
View File
@@ -28,4 +28,6 @@ return [
'sign_not_null' => ' No affiliated company has been set', 'sign_not_null' => ' No affiliated company has been set',
'password_short' => ' Password cannot be less than 6 characters', 'password_short' => ' Password cannot be less than 6 characters',
'id_empty' => ' ID cannot be empty', 'id_empty' => ' ID cannot be empty',
'customer_id' => ' customer ID',
'tag_group_id' => ' tag group ID',
]; ];
+2
View File
@@ -28,4 +28,6 @@ return [
'sign_not_null' => ' 未设置所属公司', 'sign_not_null' => ' 未设置所属公司',
'password_short' => ' 密码不能少于6位', 'password_short' => ' 密码不能少于6位',
'id_empty' => ' id不能为空', 'id_empty' => ' id不能为空',
'customer_id' => ' 客户ID',
'tag_group_id' => ' 标签组ID',
]; ];