【问题标题】:Laravel : Update Database using Ajax. What's I should to do next?Laravel:使用 Ajax 更新数据库。接下来我该怎么办?
【发布时间】:2019-12-01 21:17:29
【问题描述】:

在使用 AJAX 时,我需要有人指导帮助我构建功能更新数据库。当我单击接受按钮时,Ajax 请求将发送到服务器,服务器将执行字段 post_status 的 UPDATE 值。一开始,字段 post_status 的值为“pending”,执行查询 UPDATE 后,该值将变为“accepted”。 接下来我该怎么办?请帮我。 非常感谢您的帮助。

  • Posts 表的数据库:
post_id user_id    title       content      DateTime      post_status
1          1      Title 1     Content 1       Time         pending
2          2      Title 2     Content 2       Time         pending
3          3      Title 3     Content 3       Time         pending
4          4      Title 4     Content 4       Time         pending
5          5      Title 5     Content 5       Time         pending
  • 观点:
<table class="table table-bordered">
    <thead class="thead-inverse">
        <tr>
            <th>Post ID</th>
            <th>User ID</th>
            <th>Name User</th>
            <th>Title</th>
            <th>Content</th>
            <th>Datetime</th>
            <th>Status</th>
            <th>Accept</th>
        </tr>
        </thead>
        <tbody>
            @foreach ($posts as $row)
            <tr id="{{ $row->post_id }}"> 
                <td>{{$row->post_id}}</td>
                <td>{{$row->user_id}}</td>
                <th>{{$row->users->name}}</th>
                <td>{{$row->post_title}}</td>
                <td>{{$row->post_content}}</td>
                <td>{{$row->post_datetime}}</td>
                <td>{{$row->post_status}}</td>      {{-- The status of post that I wanna change the value to "accepted"  --}}
            <td>
                <button class="btn btn-success accept-btn" data-postID="{{$row->post_id}}">Accept</button>
            </td>
            </tr>
            @endforeach
        </tbody>
</table>
  • 代码Javascript:
<script>
    $(document).ready(function () {        
        $('.accept-btn').click(function(){
            var postId = $(this).attr('data-postID');

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            $.ajax({
                type: "POST",
                url: "{{route('posts.list_post_ajax')}}",      
                data: { postId: postId },
                dataType: 'JSON',
                success :function(response) {
                    console.log(response);
                },
            });

        })
    });
</script>
  • Controller的list_post_ajax函数:
    public function list_post_ajax(Request $request)
    {
        if(request()->ajax()){
            $postId = $request->get('postId');
            return response()->json(['success'=>$postId]);   

            // What's I should do next?
            // Pls help me.
        }


    }

【问题讨论】:

    标签: ajax database laravel


    【解决方案1】:

    有很多方法可以做到这一点。这将按照您的代码工作。使用$myObject-&gt;save(); 保存新记录或更新旧记录。

    改变观点

    <td id="td{{$row->post_id}}">{{$row->post_status}}</td>
    

    在控制器上

    $postId = $request->get('postId');
    
    $post = App\Post::find($postId);
    
    $post->post_status= 'accepted';
    
    $post->save();
    
    return response()->json(['success'=>$postId,'message'=>'accepted']);
    

    在 JS 上

          $.ajax({
                type: "POST",
                url: "{{route('posts.list_post_ajax')}}",      
                data: { postId: postId },
                dataType: 'JSON',
                success :function(response) {
                    console.log(response);
                     $('td'+postId).html(response.message);
                },
            });
    

    编辑:我在控制器上更新了我的答案。你应该看看Eloquent文档

    【讨论】:

    • 谢谢,但我想在 Controller 的函数 list_post_ajax 上执行 Query UPDATE table posts SET status = "accepted"。更改 **post_status ** 的值是“已接受”。我不知道代码查询构建更新? ` public function list_post_ajax(Request $request) { if(request()->ajax()){ $postId = $request->get('postId'); return response()->json(['success'=>$postId,'message'=>'accepted']); // 接下来我应该做什么? // 请帮助我。 } } `
    • 我已经解决了这个问题
    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 2017-12-27
    • 2020-01-24
    • 2022-01-14
    • 2020-12-16
    • 1970-01-01
    • 2020-04-16
    • 2019-05-31
    相关资源
    最近更新 更多