[英]How to get the value from another table under same database in laravel?
Make it simple... i have 1 database with 2 table which name ( bit_app_policy_category)
and (company_policy)
.简单点...我有 1 个数据库,其中有 2 个表,名称为
( bit_app_policy_category)
和(company_policy)
。 Inside (bit_app_policy_category)
i have these columns:在
(bit_app_policy_category)
里面我有这些列:
1. id
2. code
3. description
4. parent_id
5. status
Inside ( company_policy )
i have these columns:里面
( company_policy )
我有这些列:
1. id
2. policy_category_id
3. policy_title
4. version_no
5. policy_details
6. expiry_date
so what i want is to get the data value from the column ( 1.id )
inside the ( bit_app_policy_category )
into ( company_policy )
(2.policy_category_id)
column, but i don't know how to do it, i have been finding solution for like 20+ hrs and still lost and so done... could someone help me out ?所以我想要的是得到列中的数据值
( 1.id )
内( bit_app_policy_category )
到( company_policy )
(2.policy_category_id)
列,但我不知道怎么办呢,我一直在寻找解决方案像 20 多个小时一样,仍然迷路了,所以完成了......有人可以帮我吗?
<input type="hidden"
name="policy_category_id"
value="{{ $id ?? ''}}"
/>
<div class="form-group">
<label for="company_policy(policy_category_id)">Policy Category ID</label>
<select id="bit_app_policy_category_id" name="id" class="form-control">
<option value="{{null}}">--Select Category--</option>
</select>
</div>
i want the data value import into the option value which under the code line at --Select Category--我希望将数据值导入到--Select Category-- 代码行下的选项值中
categoryController类别控制器
class categoryController extends Controller
{ {
public function index(Request $request)
{
$codeSearch = $request->get('code');
$descriptionSearch = $request->get('description');
// //This line code is for sorting in ascending order or descending order
$field = $request->get('field') != '' ? $request->get('field') : 'id';
//This line code is for sorting in ascending order or descending order
$sort = $request->get('sort')!=''? $request->get('sort'):'asc';
$categories = Category::where('code', 'like', '%' . $codeSearch . '%')
->where('description', 'like', '%' . $descriptionSearch . '%')
->orderBy($field, $sort)
->paginate(6)
->withPath('?search=' . $codeSearch . '&description=' . $descriptionSearch . '&field=' . $field . '&sort=' . $sort);
foreach ($categories as $category) {
if ($policy = Category::find($category->parent_id, 'description')) {
$category->parent_id = $policy->description;
}
}
return view('category.index', ['category' => $categories]);
}
public function create()
{
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all();
return view('category.create', compact('parents'));
}
public function store(Request $request)
{
$this->validate($request, [
'code' => 'required',
'description' => 'required',
'status' => 'required',
]);
$category = new Category([
'id' => $request->get('id'),
'code' => $request->get('code'),
'description' => $request->get('description'),
'parent_id' => $request->get('parent_id'),
'status' => $request->get('status'),
]);
try {
$category->save();
} catch (\Illuminate\Database\QueryException $e) {
$errorCode = $e->errorInfo[1];
if($errorCode == '1062'){
$status1 = 'failed';
$statusMsg1 = 'Failed to Create, Duplication Code '.$request->get('code').'!';
return redirect()->back()->with($status1, $statusMsg1);
}
}
$category->save();
return redirect()->route('category.index')->with('success', 'Data has been successfully added into the system');
}
public function show($id)
{
//
}
public function edit($id)
{
$category = Category::find($id);
$parents = Category::all();
$parents=DB::table('bit_app_policy_category')->whereNotIn('id',[$id])->get();
return view('category.edit')-> with('parents', $parents)-> with('category', $category)->with('id', $id);
}
public function subRequest()
{
return view('subRequest');
}
public function subRequestPost()
{
$input = request()->all();
return response()->json(['success' => 'Got Submit Request.'])->setCallback($this)->input('callback');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'code' => 'required',
'description' => 'required',
'status' => 'required'
]);
$category = Category::find($id);
$category->code = $request->get('code');
$category->description = $request->get('description');
$category->parent_id = $request->get('parent_id');
$category->status = $request->get('status');
$category->save();
return redirect()->route('category.index')->with('success', 'Data Updated');
}
public function destroy($id)
{
$status = 'success';
$statusMsg = 'Data Deleted';
try {
$category = Category::find($id);
$category->delete();
} catch (\Illuminate\Database\QueryException $e) {
if($e->getCode() == "23000") {
$status = 'failed';
$statusMsg = 'Failed to Delete! This Record Already Been Referred!';
return redirect()->back()->with($status, $statusMsg);
}
else{
$status = 'failed';
$statusMsg = 'Database Error! Please Contact System Administrator!';
return redirect()->back()->with($status, $statusMsg);
}
}
return redirect()->route('category.index')->with($status, $statusMsg);
}
} }
For the policyController对于策略控制器
class policyController extends Controller {类策略控制器扩展控制器{
public function index(Request $request)
{
$codeSearch = $request->get('policy_category_id');
$descriptionSearch = $request->get('policy_title');
// //This line code is for sorting in ascending order or descending order
$field = $request->get('field') != '' ? $request->get('field') : 'id';
//This line code is for sorting in ascending order or descending order
$sort = $request->get('sort')!=''? $request->get('sort'):'asc';
//This line code is for searching description on the index page//
$policy = Policy::where('policy_category_id', 'like', '%' . $codeSearch . '%')
->where('policy_title', 'like', '%' . $descriptionSearch . '%')
->orderBy($field, $sort)
->paginate(6)
->withPath('?search=' . $codeSearch . '&description=' . $descriptionSearch . '&field=' . $field . '&sort=' . $sort);
return view('policy.index', ['policy' => $policy]);
}
public function create()
{
$parents = Policy::all();//DB::table("company_policy")->lists("name","id");
//Policy::all();
return view('policy.create', compact('parents'));
}
public function store(Request $request)
{
$this->validate($request, [
'policy_category_id' => 'required',
'policy_title' => 'required',
'version_no' => 'required',
'policy_details' => 'required',
'expiry_date' => 'required',
'file_path' => 'required',
]);
$policy = new Policy([
'id' => $request->get('id'),
'policy_category_id' => $request->get('policy_category_id'),
'policy_title' => $request->get('policy_title'),
'version_no' => $request->get('version_no'),
'policy_details' => $request->get('policy_details'),
'expiry_date' => $request->get('expiry_date'),
'file_path' => $request->get('file_path'),
'created_by'=> Auth::id(),
'modified_by'=> Auth::id(),
]);
try {
$policy->save();
} catch (\Illuminate\Database\QueryException $e) {
$errorCode = $e->errorInfo[1];
if ($errorCode == '1062') {
$status1 = 'failed';
$statusMsg1 = 'Failed to Create, Duplication Code ' . $request->get('code') . '!';
return redirect()->back()->with($status1, $statusMsg1);
}
}
$policy->save();
return redirect()->route('policy.index')->with('success', 'Data has been successfully added into the system');
}
public function show($id)
{
//
}
public function edit($id)
{
// $policy = Policy::find($id); // $policy = Policy::find($id);
$parents = Policy::all();
$parents=DB::table('company_policy')->whereNotIn('id',[$id])->get();
return view('policy.edit')-> with('parents', $parents)-> with('policy', $policy)->with('id', $id);
}
public function subRequest()
{
return view('subRequest');
}
public function subRequestPost()
{
$input = request()->all();
return response()->json(['success' => 'Got Submit Request.'])->setCallback($this)->input('callback');
}
public function update(Request $request, $id)
{
$this->validate($request, [
'policy_category_id' => 'required',
'policy_title' => 'required',
'version_no' => 'required',
'policy_details' => 'required',
'expiry_date' => 'required',
]);
$policy = Policy::find($id);
$policy->policy_category_id = $request->get('policy_category_id');
$policy->policy_title = $request->get('policy_title');
$policy->version_no = $request->get('version_no');
$policy->policy_details = $request->get('policy_details');
$policy->expiry_date = $request->get('expiry_date');
$policy->save();
return redirect()->route('policy.index')->with('success', 'Data Updated');
}
public function destroy($id)
{
$status = 'success';
$statusMsg = 'Data Deleted';
try {
$policy = Policy::find($id);
$policy->delete();
} catch (\Illuminate\Database\QueryException $e) {
if($e->getCode() == "23000") {
$status = 'failed';
$statusMsg = 'Failed to Delete! This Record Already Been Referred!';
return redirect()->back()->with($status, $statusMsg);
}
else{
$status = 'failed';
$statusMsg = 'Database Error! Please Contact System Administrator!';
return redirect()->back()->with($status, $statusMsg);
}
}
return redirect()->route('policy.index')->with($status, $statusMsg);
}
} }
Please see syntax请看语法
$results = CompanyPolicy::leftJoin('bit_app_policy_category', 'company_policy.id', 'bit_app_policy_category.policy_category_id')->get();
where CompanyPolicy is model for company_policy and please change your keys accordingly.其中 CompanyPolicy 是 company_policy 的模型,请相应地更改您的密钥。
use ajax.first of all get all categories from bit_app_policy_category table inside your method which you are using to show this view使用 ajax.first 首先从您用来显示此视图的方法中的 bit_app_policy_category 表中获取所有类别
//controller
if you did not create any model create model first
function function_name(){
$categories=\App\BitAppPolicyCategory:get();
return view('blade_file_name', compact('categories'));
}
//in blade file
<div class="form-group">
<label for="company_policy(policy_category_id)">Policy Category ID</label>
<select id="bit_app_policy_category_id" name="id" class="form-control">
<option value="" >--Select Category--</option>
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->code}}</option>
@endforeach
</select>
</div>
<div class="form-group">
<label for="company_policy)">Policy Category ID</label>
<select id="company_policy" name="company_policy" class="form-control">
<option value="" >--Select Policy--</option>
</select>
</div>
//ajax
$("#bit_app_policy_category_id").on('change', function () {
get_company_policy($(this).val())
});
function get_company_policy(industry_type) {
fetch(base_url + '/get_compny_policy_by_bit_policy_category_id/' + bit_app_policy_category_id)
.then(function (response) {
var html = '<option value="" selected="selected">Select Type</option>';
response.json().then(function (res) {
if (res.length > 0) {
$.each(res, function (i, v) {
html += '<option ' + (company_policy == v.id ? 'selected="selected"' : '') + ' value="' + v.id + '">' + v.policy_title + '</option>';
});
$("#company_policy").html(html);
}
});
})
.catch(function () {
alert('no data')
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.