【问题标题】:Laravel - How to display child goal type name on select dropdown on edit formLaravel - 如何在编辑表单的选择下拉列表中显示子目标类型名称
【发布时间】:2020-10-08 19:57:45
【问题描述】:

在我的 Laravel-5.8 项目中,我试图在编辑表单的选择列表中添加子名称(文本)。

我有这张桌子:

CREATE TABLE `goal_types` (
  `id` int(11) NOT NULL,
  `name` varchar(200) NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `max_score` int(11) DEFAULT 0,
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

型号

class GoalType extends Model
{
    public $timestamps = false;

    protected $table = 'goal_types';

    protected $primaryKey = 'id';
    protected $fillable = [
                  'name',
                  'parent_id',
                  'max_score',
              ];

    protected $casts = [];

    public function children()
    {
      return $this->hasMany('App\Models\GoalType', 'parent_id');
    }

    public function parent()
    {
        return $this->hasOne(App\Models\GoalType::class, 'id', 'parent_id');
    }
}

控制器

    public function index()
    {     
            $categories = GoalType::with('children')->whereNull('parent_id')->get();
            return view('goal_types.index')->with('categories', $categories);
    }

    public function create()
    {       
        return view('goal_types.create');
    }

    public function store(StoreGoalTypeRequest $request)
    {
            $data = GoalType::create([
                'name'                      => $request->name,
                'parent_id'                 => $request->parent_id,
                'max_score'                 => $request->max_score,
            ]);

        Session::flash('success', 'Goal Type is created successfully');
        return redirect()->route('goal_types.index');      
    }

我有一个用于编辑的模态表单

查看

      <div class="row">

        <div class="col-md-8">

          <div class="card card-secondary">
            <div class="card-header">
              <h3 class="card-title">Goal Type(s)</h3>
            </div>
            <div class="card-body">
              <ul class="list-group">
                @foreach ($categories as $category)
                  <li class="list-group-item">

                    @if ($category->children)
                      <ul class="list-group mt-2">
                        @foreach ($category->children as $child)
                          <li class="list-group-item">
                            <div class="d-flex justify-content-between">
                                {{ $child->name }} 

                              <div class="button-group d-flex">
                               @can('goal_type_edit')
                                <button type="button" class="btn btn-sm btn-primary mr-1 edit-category" data-toggle="modal" data-target="#editCategoryModal" data-id="{{ $child->id }}" data-name="{{ $child->name }}">Edit</button>
                               @endcan
                              </div>
                            </div>
                          </li>
                        @endforeach
                      </ul>
                    @endif
                  </li>
                @endforeach
              </ul>
            </div>
          </div>
        </div>


        <div class="modal fade" id="editCategoryModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
          <div class="modal-dialog" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h4 class="modal-title">Edit Goal Type</h4>

              <form action="" method="POST">
                @csrf
                @method('PUT')

               <div class="modal-body">
                <div class="form-group">
                  <label class="control-label"> Parent Goal Type:</label>
                  <select class="form-control select2bs4" data-placeholder="Choose Parent Goal Type" tabindex="1" name="parent_id" style="width: 100%;">
                    <option value="">Select Goal Type</option>
                    @foreach ($categories as $category)
                      <option value="{{ $category->id }}">{{ $category->name }}</option>
                    @endforeach
                  </select>
                </div>
                  <div class="form-group">
                     <label class="control-label"> Name:<span style="color:red;">*</span></label>
                    <input type="text" name="name" class="form-control" value="" placeholder="Category Name" required>
                  </div>
                <div class="form-group">
                    <label class="control-label"> Max. Weight (%):</label>
                  <input type="number" name="max_score" class="form-control" value="" step="0.01" placeholder="Enter maximum weight here: 15, 50, 75 etc" style="width: 100%;">
                </div>                       
               </div>

                <div class="modal-footer">
                  <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                  <button type="submit" class="btn btn-primary">Update</button>
                </div>
              </div>
              </form>
          </div>
        </div>

</div>

当我从索引中单击编辑时,应弹出一个模式表单以显示子名称,其中 parent_id 等于父 ID,因为它在数据库中。但是,直到我单击选择下拉列表时,选择下拉列表值/文本才显示任何内容。

如何修改

                    @foreach ($categories as $category)
                      <option value="{{ $category->id }}">{{ $category->name }}</option>
                    @endforeach

在编辑中实现这一点(在下拉列表中显示选择子项的名称)?

谢谢

【问题讨论】:

    标签: laravel


    【解决方案1】:

    在您的编辑视图中,您必须在表单操作中传递您的类别 ID,例如 action="{{ route('admin.categories.update' , $category-&gt;id) }}",然后将其与 parent_id 进行比较,在您的类别模型中,您必须传递子项。
    类别模型:

    public function children()
    {
        return $this->hasMany(GoalType::class, 'parent_id' , 'id')->with('children');
    }
    

    edit.blade.php 视图

    <div class="col-md-12">
       <label for="parent_id" class="form-control-label">parents category name</label>
       <select class="form-control" data-toggle="select" data-live-search="true" name="parent_id" id="parent_id">
          <option value="0" {{ $category->parent_id === 0 ? 'disabled' : '' }} selected> - Default</option>
             @foreach(\App\Models\Category::latest()->get() as $cate)
                <option value="{{ $cate->id }}" {{ $cate->id === $category->parent_id ? 'selected' : '' }} {{ $cate->id === $category->id ? 'disabled' : '' }} {{ $cate->id === $category->parent_id ? 'disabled' : '' }}>{{ $cate->name }}</option>
             @endforeach
       </select>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2022-01-08
      • 2013-09-02
      • 2021-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-08
      相关资源
      最近更新 更多