【问题标题】:Edit /Update values from 2 tables编辑/更新 2 个表中的值
【发布时间】:2021-09-21 00:25:14
【问题描述】:

我有一个表单,其数据来自 2 个表。

现在我需要一些帮助来制作编辑功能,因为我无法显示数据。 在编辑功能中,我需要做一些类似的事情: categorysemantic 中选择 *,其中 category.category_id=$category_id 和 semantic。 table_mr="Person"

对于更新,我还需要一些帮助来更新语义表(类别已经完成)。

编辑表单:

        <div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
      Edit category
        <form method="post" action="{{ url('category/update-category/') }}">
         @csrf
         @method('PUT')
          <input type="hidden" name="category_id" id="category_id">
           Category name: <input type="text" id="name" name="name">
           Table model: <input type="text" id="ontology" name="ontology">
          Class: <input type="text" id="seClass" name="seClass">
              <button type="submit" >Update</button>
              <button type="button" >Close</button>
            </form>
        </div>

    <script>
      $(document).ready(function(){
        $(document).on('click','.editbtn', function(){
            var category_id=$(this).val();
               $('#editModal').modal('show');
          $.ajax({
              type:"GET",
              url:"../category/edit-category/"+category_id,
              success: function(response){     
                  
                     $('#name').val(response.category.name);
                     $('#ontology').val(response.category.semanticOntology);
                     $('#seClass').val(response.category.semantiClass);
                    $('#category_id').val(category_id);        
              }
          });
        });
    }); 
    </script>

控制器:

    public function edit(**$category_id**){
       $category=category::with('semantic')->find($category_id);
     
 
    return response()->json([
     'category'=>$category,
    ]);  
    }

    public function update(Request $request){
        $category_id=$request->input('category_id');
        $category = category::find($category_id);
        $category->name = $request->input('name');
        $category->update ();
         // $semantic->Dont know how to make it
        return redirect()->back()->with('status', 'Updated with success');
    }

班级类别:

    class category extends Model{
        protected $table = "category";
        protected $primaryKey = 'category_id';
        public $timestamps = false;
        use HasFactory;
        protected $sql=['category_id','name'];
    
    
        public function semantic(){       
            return $this->belongsTo('App\models\semantic', 'category_id', 'idtable_record');      
        }
    }

【问题讨论】:

标签: javascript laravel crud


【解决方案1】:

如果你使用的是最新版本的 Laravel 框架(至少 v8.61.0):

 public function edit(**$category_id**){
   $category=category::withRelation('semantic', $category_id);
 

return response()->json([
 'category'=>$category,
]);  
}

查看此行:(类别)

$category = Category::find($category_id);

对于更新方法,您可以通过在路由 url 中传递参数来使用应用模型绑定。 示例:

Route::post('/category/{category}', [your action]);

【讨论】:

    猜你喜欢
    • 2022-01-12
    • 2020-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多