【问题标题】:(Internal Server Error) laravel and ajax(内部服务器错误)laravel 和 ajax
【发布时间】:2016-06-10 15:04:39
【问题描述】:

我是 laravel 的新手,使用 ajax 时出现内部服务器错误。 我发现当我想在数据库中保存数据(评论对象)时,我在写这一行时遇到了这个错误:

$request->Post()->comments()->save($comment);

在控制器文件中。我在此处附上了所有必要的代码。请问有没有人可以帮助我?

route.php

Route::post('/savecomment',[
  'uses'=>'CommentController@postCreateComment',
  'as'=>'comment.save'
  ]);

commentController.php

public  function postCreateComment(Request $request)
    {
        $this->validate($request,[
            'commentBody'=>'required|max:1000'
            ]);
         $comment=new Comment();
         $comment->body=$request['commentbody'];
         $comment->post_id=$request['postId'];
         $message ='there was an error';
         $request->Post()->comments()->save($comment);
         $message='comment successfuly created';
         //return redirect()->route('dashboard')->with(['message'=>$message]);  

    }

jquery

$('[id^=comment-save]').on('click', function (event) {  
event.preventDefault();
  postId = $(this).parents('.post').attr('data-postId');
  $.ajax({
    method :'POST',
    url: urlComment,
    data:{postId:postId,_token:token,commentBody:$('#comment-body'+postId ).val()}
      })
  .done(function(){
    //change the page
  });

html部分

<section class="row posts">
    <div class="cod-md-6 col-md-offset-3">
        <header><h3>what is other`s idea</h3></header>
        @foreach($posts as $post)
        <article class="post" data-postId='{{ $post->id}}'>
            <p>
                {{$post->body}}
            </p>
            <div class="info">
                posted by {{$post->user->first_name}} in {{$post->created_at}}
            </div>
            <div class="interaction">
                <a class="like" href="#">Like</a>|
                <a class="dislike" href="#">Dislike</a>|                
                <a data-toggle="collapse"  href="#collapseComment{{ $post->id }}" aria-expanded="false" aria-controls="">
                    comment
                </a>
                @if(Auth::user() == $post->user)
                |<a data-toggle="modal" href="#edit-model" class="edit" >Edit</a>|
                <a href="{{ route('post.delete',['post_id'=>$post->id])}}">Delete</a>
                @endif              
            </div>
            <div class="collapse" id="collapseComment{{ $post->id }}">
                <div class="card card-block">
                    <input type="test" name="comment-body{{ $post->id }}" id="comment-body{{ $post->id }}" >
                    <button type="button" id="comment-save{{ $post->id }}" class="btn btn-primary">comment</button>
                </div>
            </div>
        </article>
        @endforeach     
    </div>  
</section>
<script>
    var token = '{{ Session::token() }}';
    var urlComment = '{{ route('comment.save') }}';    
</script>  

评论类

 namespace App;    
use Illuminate\Database\Eloquent\Model;    
class Comment extends Model
{        
    public function post()
    {
        return $this->belongsTo('App\Post');
    }    
}

发布课程

    namespace App;        
    use Illuminate\Database\Eloquent\Model;        
    class Post extends Model 
    {            
        public function user()
        {
            return $this->belongsTo('App\user');
        }          
        public function comments()
        {        
            return $this->hasMany('App\Comment');
        }           
    }

【问题讨论】:

  • “内部服务器错误”是指查看您的错误日志以了解详细信息。
  • 如果需要帮助调试,隔离问题并创建minimal reproducible example。
  • 在日志文件中写入:[2016-06-13 09:40:38] local.ERROR: 异常 'BadMethodCallException' 带有消息 'Method Post 不存在'。在 H:\wamp\www\sport\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php:81 但正如你在我的代码中看到的,我有 post() 方法

标签: php jquery ajax laravel


【解决方案1】:

我认为 $comment->save();会好的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-11-05
  • 2021-08-25
  • 2017-04-12
  • 2020-02-12
  • 2017-03-07
  • 2018-04-16
  • 2016-03-24
  • 1970-01-01
相关资源
最近更新 更多