我目前正在尝试为公司显示我们工人的日志,它正在工作,但问题是我无法在过滤后在刀片视图中显示 json 数据。我将分享下面的代码
这是模型
<?php
namespace App\ModelsCompanyABC;
use Illuminate\Database\Eloquent\Model;
use DB;
use Session;
use Auth;
use Carbon;
class WorkersHistoryModel extends Model
{
public function getHistory($type, $selectedWorkerId)
{
$logs="";
$logs_to_parse = $this->getHistoryQuery($type, $selectedWorkerId);
foreach ($logs_to_parse as $log){
if($log->type == 1)
{
$logs.='<div class="col-md-4 single-note-item all-category note-social">';
$logs.='<div class="card card-body ">';
$logs.='<span class="side-stick"></span>';
$logs.='<h5 class="note-title text-truncate w-75 mb-0" data-noteHeading="Give salary to employee">'.$log->lastname.' Contract<i class="point fas fa-circle ml-1 font-10"></i></h5>';
} else if( $log->type == 2)
{
$logs.='<div class="col-md-4 single-note-item all-category note-business">';
$logs.='<div class="card card-body ">';
$logs.='<span class="side-stick"></span>';
$logs.='<h5 class="note-title text-truncate w-75 mb-0" data-noteHeading="Give salary to employee">'.$log->lastname.' Timecard<i class="point fas fa-circle ml-1 font-10"></i></h5>';
} else if( $log->type == 3){
$logs.='<div class="col-md-4 single-note-item all-category note-important">';
$logs.='<div class="card card-body ">';
$logs.='<span class="side-stick"></span>';
$logs.='<h5 class="note-title text-truncate w-75 mb-0" data-noteHeading="Give salary to employee">Give salary to employee <i class="point fas fa-circle ml-1 font-10"></i></h5>';
}
$logs.='<p class="note-date font-12 text-muted">'.$log->log_date.'</p>';
$logs.='<div class="note-content">';
$logs.='<p class="note-inner-content text-muted" data-noteContent="'.$log->action.'">'.$log->action.'</p>';
$logs.='</div>';
$logs.='<div class="d-flex align-items-center">';
$logs.='<span class="mr-1"><i class="far fa-star favourite-note"></i></span>';
$logs.='<span class="mr-1"><i class="far fa-trash-alt remove-note"></i></span>';
$logs.='<div class="ml-auto">';
$logs.='<div class="category-selector btn-group">';
$logs.='<a class="nav-link dropdown-toggle category-dropdown label-group p-0" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="true">';
$logs.='<div class="category">';
$logs.='<div class="category-business"></div>';
$logs.='<div class="category-social"></div>';
$logs.='<div class="category-important"></div>';
$logs.='<span class="more-options text-dark"><i class="icon-options-vertical"></i></span>';
$logs.='</div>';
$logs.='</a>';
$logs.='<div class="dropdown-menu dropdown-menu-right category-menu">';
$logs.='<a class="note-business badge-group-item badge-business dropdown-item position-relative category-business text-success" href="javascript:void(0);"><i class="mdi mdi-checkbox-blank-circle-outline mr-1"></i>Business</a>';
$logs.='<a class="note-social badge-group-item badge-social dropdown-item position-relative category-social text-info" href="javascript:void(0);"><i class="mdi mdi-checkbox-blank-circle-outline mr-1"></i> Social</a>';
$logs.='<a class="note-important badge-group-item badge-important dropdown-item position-relative category-important text-danger" href="javascript:void(0);"><i class="mdi mdi-checkbox-blank-circle-outline mr-1"></i> Important</a>';
$logs.='</div>';
$logs.=' </div>';
$logs.='</div>';
$logs.='</div>';
$logs.='</div>';
$logs.='</div>';
}
return $logs;
}
public function getHistoryQuery($type, $selectedWorkerId)
{
if ($type == 0) {
$logs = DB::table('ws_logs')
->select(
'ws_logs.action',
'ws_logs.type',
'recipient_id',
'person.lastname',
'person.firstname',
DB::raw("to_char(ws_logs.log_date,'MON-DD-YYYY HH12:MI PM') as log_date")
)
->join('person', 'person.id', '=', 'ws_logs.recipient_id')
->where('department', '=', Session::get('userDepartment'))
->where('ws_logs.type', '<=', 2)
->orderBy('ws_logs.log_date', 'DESC')
->get();
} else {
$logs = DB::table('ws_logs')
->select(
'ws_logs.action',
'ws_logs.date',
'ws_logs.remarks',
'ws_logs.type',
'recipient_id',
'person.lastname',
'person.firstname'
)
->join('person', 'person.id', '=', 'ws_logs.recipient_id')
->where('department', '=', Session::get('userDepartment'))
->where('ws_logs.type', '=', $type)
->orderBy('ws_logs.date', 'asc')
->get();
}
if ($selectedWorkerId) {
$logs = $logs->where('recipient_id', '=', $selectedWorkerId);
}
return $logs;
}
public function getAllRecipientIds()
{
$logs = DB::table('ws_logs')
->select('recipient_id', 'person.lastname', 'person.firstname')
->join('person', 'person.id', '=', 'ws_logs.recipient_id')
->where('department', '=', Session::get('userDepartment'))
->distinct()
->get();
$recipientIds = $logs->mapWithKeys(function ($item) {
return [$item->recipient_id => $item->lastname ];
});
$recipientIds = $recipientIds->sortBy(function ($fullName, $recipientId) {
return $fullName;
});
return $recipientIds->toArray();
}
}
这是控制器
<?php
namespace App\Http\Controllers\CompanyABC;
use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\ModelsCompanyABC\WorkerHistoryModel;
class WorkerHistoryController extends Controller
{
public function __construct(WorkerHistoryModel $workerHistoryModel){
$this->generalModel= new \App\GeneralModel();
$this->workerHistoryModel = $workerHistoryModel;
$this->middleware('auth');
}
//
public function getLogsByWorker(Request $request)
{
$model = new WorkerHistoryModel();
$logs = $model->getHistoryQuery(0, $request->worker);
return response()->json($logs);
}
public function showHistoryPage(Request $request)
{
$logs = $this->workerHistoryModel->getHistory(0, $request->worker);
$d = $this->generalModel->get_profile_pict(Auth::user()->id);
$data['menus'] = $this->generalModel->get_main_menu_links();
$recipientIds = $this->workerHistoryModel->getAllRecipientIds();
$data['profile'] = $d['profile'];
$data['profilepic'] = $d['profilepic'];
$data['logs'] = $logs;
$data['recipientIds'] = $recipientIds;
return view('companyabc.worker_dtr.department.workhistory', $data);
}
public function getHistory(Request $request)
{
$logs=$this->WorkerHistoryModel->getHistory($request->type);
return response()->json($logs);
}
}
这是使用 ajax 的刀片视图
<ul>
<li class="nav-item" id="">
<div class="d-flex align-items-center">
<a href="javascript:void(0)" class="nav-link rounded-pill note-link d-flex align-items-center px-2 px-md-3 mr-0 mr-md-2"></a>
<p class="mr-2 mb-0 text-info">Worker's Name:</p>
<select class="form-control custom-select" id="history_worker_select" onchange="handleSelectChange()">
<option>Select Worker's Name</option>
@foreach ($recipientIds as $recipientId => $fullName)
<option value="{{ $recipientId }}">{{ $fullName }}</option>
@endforeach
</select>
</div>
</li>
</ul>
<div class="tab-content">
<div id="note-full-container" class="note-has-grid row">
{!! $logs !!}
</div>
</div>
$("#history_worker_select").change(function() {
var worker= $("#history_worker_select").val();
$.ajax({
url: 'https://192.168.7.211/companyabc/ws_dtr/ajaxGetWorkerHistoryList',
type: 'GET',
data: {
worker: worker,
},
dataType: 'json',
success: function(data) {
console.log(data);
},
error: function(xhr, status, error) {
console.error(error);
}
});
});