php - Post Error 500 adding a new row using Eloquent -
i'm trying follow tutorial making social network, functionality of liking , disliking when no row available in database using ajax (adding new entry) broken.
tutorial i'm following - video creates controller:
https://www.youtube.com/watch?v=drm19vkbcgu&list=pl55riy5tl51olosgk5xdo2mgjpqc0bxgv&index=20
development enviroment:
- phpstorm v9.0
- xampp v3.2.2
- jquery v1.12.0
- google chrome
error image:
error image link
the whole cycle:
, dislike button (the view):
<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 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 post':'dislike' : 'dislike' }}</a> <script> var urllike = '{{ route('like') }}'; </script>
route:
route::post('/like', [ 'uses' => 'postcontroller@postlikepost', 'as' => 'like' ]);
listener + ajax call:
$('.like').on('click', function(event){ event.preventdefault(); postid = event.target.parentnode.parentnode.dataset['postid']; var islike = event.target.previouselementsibling == null; //console.log(postid+' '+islike); $.ajax({ method: 'post', url: urllike, data: {islike: islike, postid: postid, _token: token} }) .done(function(){ console.log(event.target.innertext); event.target.innertext = islike ? event.target.innertext == 'like' ? 'you post':'like' : event.target.innertext == 'dislike' ? 'you don\'t post':'dislike' ; if(islike){ event.target.nextelementsibling.innertext = 'dislike'; } else{ event.target.nextelementsibling.innertext = 'like'; } }); });
controller:
public function postlikepost(request $request){ $post_id = $request['postid']; $is_like = $request['islike'] === 'true'; $update = false; $post = post::find($post_id); if(!$post){ return null; } $user = auth::user(); $like = $user->likes()->where('post_id', $post_id)->first(); if($like){ $already_like = $like->like; $update = true; if($already_like == $is_like){ $like->delete(); return null; } }else{ $like->like = $is_like; $like->post_id = $post_id; $like->user_id = $user_id; } // working when controller updating broken when saving if($update){ $like->update(); } else{ $like->save(); } return null; }
p.s:
i'm new phpstorm , laravel, if know way debug/log/watch variable values in eclipse, appreciated.
from see ajax request using wrong url it's requesting localhost/public/likes
instead of localhost/your_project/public/like
$.ajax({ method: 'post', url: 'like', data: {islike: islike, postid: postid, _token: token} })
should work... or
<script> var urllike = '{{ url::to('like') }}'; </script>
not sure why route()
not working though
Comments
Post a Comment