【问题标题】:How to call out the "description" to replace the "parent_id" in laravel?laravel中如何调用“描述”来替换“parent_id”?
【发布时间】:2020-02-25 06:30:25
【问题描述】:

所以这是我的问题,我已经完成了所有工作,我的上级要求我现在将 parent_id 替换为描述,因为编码人员知道整数代表什么,但用户不知道。这是图片 My current view looks like !

正如您在红色列中看到的,有两个 id : 1.(999162,Testing3,Test3,999161,活动)和 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; 使用应用\类别;

class categoryController 扩展 Controller { /** * 显示资源列表。 * * @return \Illuminate\Http\Response */ 公共函数索引() {

    $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

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    假设您有一个 Category 模型,将自爆代码添加到您的 Category 模型类。

    public function parent()
    {
      return $this->belongsTo(Category::class, 'parent_id', 'id');
    }
    

    然后,将您的代码中的{{$row['parent_id']}} 替换为{{$row-&gt;parent-&gt;description}}

    【讨论】:

    • 公共函数 parent() 添加到 categoryController.php 或 category.php 内
    • category.php @andy
    • 我添加了一些图片,帮我看看,非常感谢
    • 你可以先按照我上面写的修改你的代码,有问题再问我。
    • Facade\Ignition\Exceptions\ViewException Trying to get property 'parent' of non-object (View: C:\xampp\htdocs\companypolicy\resources\views\category\index.blade.php)
    【解决方案2】:

    这就是答案

    {
    /**
     * 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'));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-11
      • 2011-03-20
      • 1970-01-01
      • 2021-04-24
      • 1970-01-01
      • 2022-01-22
      • 2013-08-13
      • 1970-01-01
      相关资源
      最近更新 更多