如何解决如何使用 WHERE LIKE或等效
我有一个设备预订登记/日历系统,我目前正在使用 Laravel。
设备可以根据需要直接预订给员工或安排在未来的日期。 例如,笔记本电脑 1 可能有 3 个预订,但笔记本电脑一次只能预订一个人。
系统启动并运行,用户可以搜索当前/预定的预订。 但是我无法让用户搜索员工姓名,因为我需要拆分查询。
如您所见,我首先从第 7-14 行获取所有设备。
然后,我将每个设备的所有预订附加到 $bookings 集合中的一个名为“可用预订”的新键中。
我想知道如何搜索员工姓名字段的可用预订键,因为我已经将雄辩的查询作为集合返回。
public function searchCurrentBookings(Request $request) {
// Making sure the user entered a keyword.
if ($request->has('query')) {
//Get all devices
$bookings = devices::where('device_status','!=','decommisioned')
->where(function ($query) {
$query->Where('devices.device_id','like','%' . request('query') . '%')
->orWhere('device_serial','%' . request('query') . '%')
->orWhere('model_name_fk','%' . request('query') . '%')
->orWhere('device_status','%' . request('query') . '%');
})
->get();
//Append all of the bookings for each device to collection
foreach($bookings as $booking) {
$booking->available_bookings = bookings::leftJoin('staff','staff.staff_id','bookings.checked_out_to')
->where('device_id','=',$booking->device_id)
->WhereIn('bookings.status',['scheduled','checked_out'])
}
//How can I search the available bookings in this collection?
$collection = $bookings->available_bookings->Where('staff_name','%' . request('query') . '%')
// If there are results return them,if none,return the error message.
return $bookings->count() ? $bookings : $error;
}
}
以下是 $bookings 对象的示例
[
{
"device_id": "Laptop 1","device_serial": "63YNERQN","model_name_fk": "Dell Latitude 3310","device_status": "unavailable","available_bookings": [
{
"id": 45,"device_id": "Laptop 1","date_from": "1-May-2021","date_to": "5-May-2021","status": "checked_out","checked_out_to": 1,"updated_at": "2021-05-27T11:52:13.000000Z","staff_id": 1,"staff_name": "Jaden Bird"
},{
"id": 46,"date_from": "6-May-2021","date_to": "8-May-2021","status": "scheduled","checked_out_to": 2,"staff_id": 2,"staff_name": "Lilo Berry"
},{
"id": 47,"date_from": "17-May-2021","date_to": "27-May-2021","checked_out_to": 4,"staff_id": 4,"staff_name": "Barbora Forester"
}
]
},{
"device_id": "Laptop 3","device_serial": "PNX9N8R8","model_name_fk": "Dell Latitude 7490","available_bookings": [
{
"id": 48,"device_id": "Laptop 3","date_from": "5-May-2021","checked_out_to": 3,"staff_id": 3,"staff_name": "Kaiden Ojeda"
}
]
},{
"device_id": "Laptop 4","device_serial": "GTUEDDDH","model_name_fk": "Dell Latitude 5300","device_status": "available","available_bookings": [
{
"id": 50,"device_id": "Laptop 4","date_from": "30-May-2021","date_to": "30-May-2021","checked_out_to": 6,"staff_id": 6,"staff_name": "Luka Evans"
}
]
},{
"device_id": "Projector","device_serial": "Projector","model_name_fk": "Epson EH-TW550","available_bookings": []
}
]
以下系统的示例图像。 我想在所有预订中或仅在活动预订中搜索员工姓名(以较容易者为准)。
数据库结构如下:
型号:
设备:
<?PHP
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Devices extends Model
{
#use HasFactory;
protected $table = 'devices';
/**
* @var string $primaryKey Name of the primary key
*/
protected $primaryKey = 'device_id';
/**
* @var bool $incrementing The PK does not increment
*/
public $incrementing = false;
/**
* @var string $keyType Primary key is a string (i.e. not an integer)
*/
protected $keyType = 'string';
/**
* @var array $fillable The attributes that are mass assignable.
*/
protected $fillable = [
'device_id','device_serial','model_name_fk','device_status'
];
public function bookings() {
$this->hasMany(Bookings::class,'device_id','device_id');
}
}
预订:
<?PHP
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Bookings extends Model
{
#use HasFactory;
protected $table = 'bookings';
/**
* @var string $primaryKey Name of the primary key
*/
protected $primaryKey = 'id';
/**
* @var bool $incrementing The PK does not increment
*/
public $incrementing = true;
/**
* @var string $keyType Primary key is a string (i.e. not an integer)
*/
protected $keyType = 'integer';
/**
* @var array $fillable The attributes that are mass assignable.
*/
protected $fillable = [
'device_id','date_from','date_to','status','checked_out_to'
];
public function staff() {
$this->belongsTo(Staff::class,'checked_out_to','staff_id');
}
}
员工:
<?PHP
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Staff extends Model
{
protected $table = 'staff';
/**
* @var string $primaryKey Name of the primary key
*/
protected $primaryKey = 'staff_id';
/**
* @var bool $incrementing The PK does not increment
*/
public $incrementing = true;
/**
* @var string $keyType Primary key is a string (i.e. not an integer)
*/
protected $keyType = 'integer';
/**
* @var array $fillable The attributes that are mass assignable.
*/
protected $fillable = [
'staff_id','staff_name'
];
}
解决方法
希望这能帮到你。
在您的设备模型类文件中添加关系。
public function bookings() {
return $this->hasMany(Booking::class,'device_id','device_id');
}
在您的 Booking 模型类文件中添加关系。
public function staff() {
return $this->belongsTo(Staff::class,'checked_out_to','staff_id');
}
现在在您的控制器文件中,您可以使用以下内容过滤关系数据。
$search = $request->search_name;
$bookings = Device::with('bookings','bookings.staff')->whereHas('bookings.staff',function($q) use($search) {
return $q->where('staff_name','like','%' . $search .'%');
})->get();
在调用 get()
函数之前,在 Booking 表中添加您需要的所有其他查询条件。
注意:我希望我的表关系是正确的。如果不是,请根据您的映射进行更正。
编辑:我忘记在模型关系函数中添加返回语句。我在答案中更新了相同的内容。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。