我正在使用AJAX和jQuery在一个非常简单的Laravel项目上练习。我试图编辑帖子和它的元信息,我之前添加了。我尝试了console.log,它用元信息显示了我的帖子。Post来自Post表,元信息来自post_metas表,其中我添加了post_id作为forign键。Post数据显示在编辑模式,但我不能把元信息在他们的特定领域编辑模式。这是我的PostController.php
public function edit($id)
{
$post = Post::with('meta')->find($id);
if ($post) {
return response()->json([
'status' => 200,
'post' => $post,
]);
} else {
return response()->json([
'status' => 404,
'message' => 'Post Not Found',
]);
}
}这里是Index.blade.php (jQuery AJAX代码)
$(document).on('click', '.edit_post_btn', function(e) {
e.preventDefault();
var post_id = $(this).val();
var route_url = "{{ route('blog.edit', ':id') }}";
route_url = route_url.replace(':id', post_id);
$.ajax({
type: "GET",
url: route_url,
success: function(response) {
if (response.status === 200) {
console.log(response.post);
$('#edit_title').val(response.post.title);
$('#edit_excerpt').val(response.post.excerpt);
$('#edit_content').val(response.post.content);
$('#edit_min_to_read').val(response.post.min_to_read);
$('#edit_meta_description').val(response.post.meta_description);
$('#edit_meta_keywords').val(response.post.meta_keywords);
$('#edit_meta_robots').val(response.post.meta_robots);
} else {
console.log(response.message);
}
}
});
});这是我的路线:
Route::get('/edit/{id}', [PostController::class, 'edit'])->name('blog.edit');请参阅档案:

发布于 2022-10-02 11:05:33
元信息在元对象中:
$('#edit_meta_description').val(response.post.meta.meta_description);
$('#edit_meta_keywords').val(response.post.meta.meta_keywords);
$('#edit_meta_robots').val(response.post.meta.meta_robots);https://stackoverflow.com/questions/73925268
复制相似问题