所以这是我的问题,我已经做了所有事情,我的上级要求我现在将parent_id替换为描述,因为编码员知道什么是整数,但用户不知道。这是图片 My current view looks like !
您可以在红色栏中看到两个id: 1.(999162,Testing3,Test3,999161,active)和 2.(999163,testing4,test,999162,active)
我想要的输出是1.(999161调用了999161描述而不是id)。 让我们以999163为例:所需的输出应类似于999163,testing4,test,test3,active。
我不知道该如何调用说明来替换parent_id,有人可以帮忙吗?
<div class="row">
<div class="col-md-12">
<br />
<h3 align="center">Category Data</h3>
<br />
@if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
@endif
<div align="right">
<a href="{{route('category.create')}}" class="btn btn-primary">Add</a>
<br />
<br />
</div>
<table class="table table-bordered table-striped">
<tr>
<th>Id</th>
<th>Code</th>
<th>Description</th>
<th>Parent</th>
<th>Status</th>
<th>Action</th>
<th>Action</th>
</tr>
@foreach($category as $row)
<tr>
<td>{{$row['id']}}</td>
<td>{{$row['code']}}</td>
<td>{{$row['description']}}</td>
<td>{{$row['parent_id']}}</td>
<td>{{$row['status']}}</td>
<td><a href="{{action('categoryController@edit', $row['id'])}}" class="btn btn-warning">Edit</a></td>
<td>
<form method="post" class="delete_form" action="{{action('categoryController@destroy',$row['id'])}}">
{{ csrf_field() }}
{{ method_field('DELETE')}}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</table>
</div>
</div>
这是我的categoryController.php编码
<?php
命名空间App \ Http \ Controllers;
使用Illuminate \ Http \ Request; 使用App \ Category;
class categoryController扩展了Controller { / ** *显示资源清单。 * * @return \ Illuminate \ Http \ Response * / 公共功能index() {
$category =Category::all()->toArray();
return view('category.index',compact('category'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all();
return view('category.create',compact('parents'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'code' => 'required',
'description' => 'required',
'parent_id' => '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'),
]);
$category->save();
return redirect()->route('category.create')->with('success', 'Data Added');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$category = Category::find($id);
return view('category.edit', compact('category','id'));
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all();
return view('category.create',compact('parents'));
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all
return view('category.edit',compact('parents'));
}
public function subRequest()
{
return view('subRequest');
}
public function subRequestPost()
{
$input = request()->all();
return response()->json(['success'=>'Got Submit Request.']);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request,[
'code' =>'required',
'description' =>'required',
'parent_id'=>'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');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$category = Category::find($id);
$category->delete();
return redirect()->route('category.index')->with('success','Data Deleted');
}
}
Category.php图片 Category.php picture
答案 0 :(得分:0)
假设您有一个Category
模型,请向Category
模型类中添加自定义代码。
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id', 'id');
}
然后,在代码中将{{$row['parent_id']}}
替换为{{$row->parent->description}}
。
答案 1 :(得分:0)
这是答案
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$tmp =Category::all()->toArray();
$category = array();
foreach ($tmp as $key => $row) {
$policy = Category::find($row['parent_id']);
$tmpResult = new Category();
$tmpResult-> id =$row['id'];
$tmpResult-> code =$row['code'];
$tmpResult-> description =$row['description'];
$tmpResult-> parent_id =$policy['description'];
$tmpResult-> status =$row['status'];
array_push($category, $tmpResult);
}
return view('category.index',compact('category'));
}