可以将客户分配给许多公司。因此,当选择了客户时,相应的公司应出现。我得到的是company_ids
,而不是公司名称。
我想我快到了
我在下面包括了controller,view和jquery部分
JobController.php
public function edit(Job $job)
{
$subJobs = $job->sub_jobs;
$customers = User::all();
$selectedCustomer = $job->user_id;
//dd($selectedCustomer);
$companies = Company::all();
$selectedCompany = $job->company_id;
return view('employees.jobs.edit', compact('job', 'subJobs', 'customers', 'selectedCustomer', 'companies', 'selectedCompany'));
}
public function ajax(Job $job)
{
//Get the Job Number
$id = $job->id;
//Get the user_id of the job
$userId = Job::where('id', '=', $id)->pluck('user_id')->first();
//Show all customers
$customers = User::all();
//dd($customers);
//Show the companies according to the customer list
$companies = Job::where('user_id', '=', $userId)
->pluck('company_id')->unique();
//dd($companies);
return json_encode($companies);
}
public function update(Request $request, Job $job)
{
$this->validate($request, [
'job_owner' => 'required'
]);
//dd($request);
$customer = new User();
$customer->user_id = $request['customer'];
$customer = User::Find($request['customer']);
$customer->companies()->attach($request['company']);
$customer->save();
$job->user_id = $request['customer'];
$job->company_id = $request['company'];
$job->description= $request['description'];
$job->owner= $request['owner'];
$job->status= $request['status'];
$job->despatch_status= $request['despatch_status'];
$job->invoice_status= $request['invoice_status'];
$job->sales_status= $request['sales_status'];
$job->entered_status= $request['entered_status'];
$job->delivery_date= $request['delivery_date'];
$job->delivery_time= $request['delivery_time'];
$job->comp= $request['comp'];
$job->cost= $request['cost'];
$job->profit= $request['profit'];
$job->price= $request['price'];
$job->nbt= $request['nbt'];
$job->vat= $request['vat'];
$job->total_value= $request['total_value'];
$job->save();
$jobs = Job::all();
return view('employees.jobs.job', compact('jobs', $jobs));
}
jobs.edit.blade.php
<form action="{{ route('employee.job.update',$job->id) }}" method="post">
@method('PUT')
@csrf
<div class="form-group">
<label for="inputJobNumber">Job Number</label>
<input type="text" class="form-control" id="inputJobNumber" name="id" value="{{$job->id}}" readonly>
</div>
<div class="form-group">
<label for="inputCustomerName">Customer Name</label>
<select name="customer" class="form-control" id="inputUser">
<option value="">--Select User--</option>
@if ($customers->count())
@foreach ($customers as $customer)
<option value="{{ $customer->id }}" {{ $selectedCustomer == $customer->id ? 'selected="selected"' : '' }}>
{{ $customer->name }}
</option>
@endforeach
@endif
</select>
</div>
<div class="form-group">
<label for="inputCustomerName">Company Name</label>
<select name="company" class="form-control" id="inputCompany">
<option value="">--Select Company--</option>
@if ($companies->count())
@foreach ($companies as $key => $value)
<option value="{{ $key }}" {{ $selectedCompany == $key ? 'selected="selected"' : '' }}>
{{ $value->name }}
</option>
@endforeach
@endif
jobs.edit.blade.php的jQuery
<script type="text/javascript">
$(document).ready(function() {
$('select[name="customer"]').on('change', function() {
var cusotmerId = $(this).val();
if(cusotmerId) {
$.ajax({
url: '/employee/job/ajax/'+cusotmerId,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="company"]').empty();
$.each(data, function(key, value) {
$('select[name="company"]').append('<option value="'+ key +'">'+ value +'</option>');
});
}
});
}else{
$('select[name="company"]').empty();
}
});
});
</script>
给定user_id时,我需要获取company_ids
$companies = DB::table('jobs')
->where('user_id', '=', $userId)
->join('companies', 'jobs.company_id', '=', 'companies.id')
->get()
->unique();
答案 0 :(得分:0)
根据您的class ViewController: UIViewController {
let url = "http://roadfiresoftware.com/feed/json"
struct Blog: Decodable {
let title: String
let homepageURL: URL
let articles: [Article]
enum CodingKeys : String, CodingKey {
case title
case homepageURL = "home_page_url"
case articles = "items"
}
}
struct Article: Decodable {
let id: String
let url: URL
let title: String
}
let task = URLSession.shared.dataTask(with: URL(string: url)!) { (data, response, error) in
guard let data = data else {
NSLog("Error: No data to decode")
return
}
guard let blog = try? JSONDecoder().decode(Blog.self, from: data) else {
NSLog("Error: Couldn't decode data into Blog")
return
}
NSLog("blog title: \(blog.title)")
NSLog("blog home: \(blog.homepageURL)")
NSLog("articles:")
for article in blog.articles {
NSLog("- \(article.title)")
}
}
}
,您正在请求jobs.edit.blade.php
进行客户变更,但是您的/employee/job/ajax/'+cusotmerId
方法需要一个JobController::ajax
实例作为参数,这似乎是错误的,请尝试此操作Job $job
的实现:
JobController::ajax
更新
让我们假设public function ajax(Customer $customer)
{
$companies = $customer->companies()->pluck("name", "company_id");
return json_encode($companies);
}
确实在接收实例Job。根据我看到的代码,您可以像这样实现:
JobController::ajax
如果public function ajax(Job $job)
{
$userId = $job->user_id;
$companies = Company::whereHas('jobs', function($query) use ($userId) {
$query->where('user_id', '=', $userId);
})->pluck('name', 'company_id');
return json_encode($companies);
}
关系尚不存在,则需要使用以下方式更新jobs
模型
Company