【发布时间】:2021-09-21 00:25:14
【问题描述】:
我有一个表单,其数据来自 2 个表。
现在我需要一些帮助来制作编辑功能,因为我无法显示数据。 在编辑功能中,我需要做一些类似的事情: 从 category 和 semantic 中选择 *,其中 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');
}
}
【问题讨论】:
-
如果您想保存您的模型及其所有关联关系,您可以使用
push方法。 laravel.com/docs/5.7/eloquent-relationships#the-push-method
标签: javascript laravel crud