获取已经发布的post字段并使用jquery更新它们

getting already posted post fields and updating them using jquery

本文关键字:jquery 更新 字段 post 获取      更新时间:2023-09-26

我正在开发laravel,想编辑有多个字段要编辑的帖子。我已经用我的帖子的身体领域成功地做到了这一点,想用其他两个来实现

相关controller代码:

$post->body=$data['body'];
$post->state = $data['state'];
$post->problem_domain = $data['problem_domain'];
$post->update();
return response()->json(['new_body'=>$post->body],500);

blade需要从中提取早期数据:

<article class="post" data-postid="{{ $post->id }}">
    <p> {{ $post->body }} </p>
    <p> <b> Region: </b> {{ $post->state }} &nbsp <b> Problem Domain: </b>
    {{ $post->problem_domain}}</p>
    <div class="info">
       posted by <b>{{ $post->user->name }}</b>  on 
       <b>{{ $post->created_at }}</b>
    </div>
    <div class="interaction">
      <a href="#" class="like">{{ Auth::user()->likes()->where('post_id',$post->id)->first() ? Auth::user()->likes()->where('post_id',$post->id)->first()->like == 1 ? 'You like this post':'Like':'Like'}} </a>
      <a href="#" class="like">{{ Auth::user()->likes()->where('post_id',$post->id)->first() ? Auth::user()->likes()->where('post_id',$post->id)->first()->like == 0 ? 'You don''t like this post':'Dislike':'Dislike'}}</a>
      @if(Auth::user() == $post->user)
        <a href="#" class="edit"  >Edit</a>

最后是我的js file,请看看body的格式是正确的,并希望对其他两个也这样做

var postId=0;
var postBodyElement=null;
$('.post').find('.interaction').find('.edit').on('click',function(event){
    event.preventDefault();
    postBodyElement=event.target.parentNode.parentNode.childNodes[1];
    var postBody=postBodyElement.textContent;
    postId=event.target.parentNode.parentNode.dataset['postid'];
    $('#post-body').val(postBody);
    $('#edit-modal').modal();
});
$('#modal-save').on('click',function()
    {  
        $.ajax(
            {
            method : 'POST',
            url :  urlEdit,
            data: {body: $('#post-body').val(), state: $('#post-state').val(), 
            problem_domain: $('#post-problem_domain').val(), postId: postId, _token: token}
         })
        .done(function(msg)
             {
                 $(postBodyElement).text(msg['new_body']);
                 $('#edit-modal').modal('hide');
             });
     }
);

在您的控制器上,您只返回以下行的主体:

return response()->json(['new_body'=>$post->body],500);

您应该返回完整的模型或其他属性,例如:

return response()->json($post, 500);

以及在您的刀片中更新其他属性。