hi, nice tutorials :D i really enjoyed it how you explain it step by step briefly, it's really help me that's still new on using Laravel framework hehe well it's not went smooth sometimes when following your tutorial, because sometimes there's a bug on my project because there's some space on my html code lol well it's worthed and relieving though if we can solved our bugs, eventhough we should trace the codes thoroughly line to line lol well, keep on awesome bro hope you'll always be success on your life :D cheers ;)
Hey,one short simple question. What is the difference,for the example,when you make call like this: Auth::user()->likes()->somMethod() or Auth::user()->likes->someMethod(). What is the point of using "()" when for example fetching like for that particular user.
+Bora Manasijevic Hi Bora, the difference is, that using () returns a query builder object, i.e. it allows us to continue building the query. Using 'likes' without () accesses the returned results of a database query (i.e. all the likes of a user in this case). So you use 'likes()' if you want to continue building your db query (i.e. chain where conditions or other relationship-calls onto it) and you use 'likes' if you want to get an array of all the likes for example.
Sorry, I want to ask a question. My code runs smoothly, but when I click on it, Count's number needs to be rearranged before the number changes. How can I make it update immediately?
i have noticed you are making a query inside a foreach loop does this mean your query will loop aswell because if so this will cause some major problems down the road i mean say you had a 1000 posts thats a 1000 quries to your database
I have added {{ DB::table('likes')->where([['like','=','1'], ['post_id', '=', '$post->id']])->count() }} this in views but it always showing 0. Why? Did I do anything wrong?
Is there a security implementation that prevents a user from changing DOM after getting ID of post belonging to different user and editing that post as well despite being unauthorised?
Thanks for your response, just to confirm, will this be similar to what we did with delete, or is edit a bit different in terms of security implementation?
This seems to be assuming that like is part of $post which it is not. I would like a good answer for this and when I find one I will post. I am working in 5.4 though
Hi, for some reason when everything is done, it throws a 500 internal server error on the console and lists the $.ajax method on the app.js file, along with some of the jquery files. Do you have any idea of possible reasons? Thanks so much for the tutorial btw, very detailed and easy to understand, I will be starting the Laravel course on Udemy tomorrow. :)
Hi, I fixed the issue now, I forgot to include the 'use App\Like' at the top of the controller...thanks a lot. I have a question regarding the blog on the Udemy course and I'm not sure if I should be asking here but basically I want to implement this project into the blog project and have this project as a basic forum page or a comment section for blog posts, but basically users need to be signed in to comment and I'm not sure how to do that as the Auth is set on for just the Admin on the blog.
I have a question regarding the blog on the Udemy course and I'm not sure if I should be asking here but basically I want to implement this project into the blog project and have this project as a basic forum page or a comment section for blog posts, but basically users need to be signed in to comment and I'm not sure how to do that as the Auth is set on for just the Admin on the blog.
You would set up the Auth mechanism the same way (you might name the model 'User', it makes more sense than 'Admin', but that does not change the way it is done). Then you would protect routes (like the comment route) the same way, routes are protected in the Admin area in the Udemy course project.
+Rebecca Lau Hi Rebecca, you would need to extract it from the database like this (assuming you have the post already stored in a variable $post): $number_likes = $post->likes->groupBy('count'). Or simply just get the likes like this: $number_likes = count($post->likes)
Hi thanks for replying me that quickly, the code works but it don't separate the like and dislike. It just count the totally number of like and dislike in a post. To separate like and dislike what should i do? maybe use groupby ?? groupby 1 for like and groupby 0 for dislike? Anyway many thanks for your tutorial bought the one in Udemy as well helps me a lots ^^
Sorry to bother you again but i try {{$number_likes=count($post->likes->where('like',1)}} and {{$number_likes=$post->likes->where('like',1)->groupBy('count')}} both not working. Where should I exactly add the statement to the query?
Try the following: $number_likes = count($post->whereHas('likes', function($query) { $query->where('like', '=', '1'); }); Sorry for all the fragments. I'm currently at the airport and therefore can't test the code, but this should work. There would be other ways to achieve this, too, but this should work.
hello everyone. i have a 500 internal server error when i press the 'You like this post' button or 'You don't like this post'. Everything else works great...
i want this part {{ 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' }} to be in the controller script
So I came back to this video as a reference where we use AJAX to call function for likes and dislikes. I have something similar, but I have stuck my self in some problems. I want to call a function that gets logged in status of all users and display that status on the screen. Function is to be called every 60 seconds. problem is next: everything is fine after function is called first time, but every next time it returns 500 error. Please help me here, I'm starting to lose my mind :) javascript function: var chatPersons = document.querySelectorAll('.users-list-container ul li a span'); for(var i = 0; i < chatPersons.length; i++) { var person = chatPersons[i], user_id = chatPersons[i].dataset.userid; updateUserStatus(user_id, person); } function updateUserStatus(user_id, person) { console.log('Triggered') $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } }); $.ajax({ url: checkIfLoggedURL, type: 'POST', data: { user_id: user_id } }) .done(function(msg){ if(msg['loggedin'] == 'true') { $(person).removeClass('disconnected').addClass('connected'); } else { $(person).removeClass('connected').addClass('disconnected'); } }); setTimeout(updateUserStatus, 60000); } Here is the Route: Route::post('/isloggedin', [ 'uses' => 'UserController@isLoggedIn', 'as' => 'isloggedin' ]); somewhere in the file i declare variable for route checkIfLoggedURL = '{{ route('isloggedin') }}'; and token: and here is the php function that shall return the status of users public function isLoggedIn(Request $request) { $user_id = $request['user_id']; $user = User::find($user_id); if($user->logged_in) { return response()->json(['loggedin' => 'true'], 200); } else { return response()->json(['loggedin' => 'false'], 200); } }
I can't really dive super-deep into this as I can't offer great support beyond the scope of my videos. Simply due to time restrictions. But the first step to debug this error, is to go to the dev tools in your browser, open the network tab, find the failing requests (should be red), click on them and then click on "response". You should see the real error, Laravel is sending you. That might be a good starting point to understand what's going wrong.
I managed to solve it. For the sake of others who encounter the similar problem: So i was calling php function for each user individually, and that's probably the reason for 500 error. What I did is i changed the function and now I'm calling it once for all of the users and then extracting the online statuses from the array in javascript.
I've now copied over all my code to exactly yours but it still won't auto update.. for some reason my code never reaches the app.js click event. I have no errors but I try to console.log in the .done function and nothing
Hm ... do you get some 404 error that app.js is not found? Did you try to call another (dummy) method in the app.js file to simply see if that's the issue to begin with.
how can i set to change not text? I wanna change with gliphicons getbootstrap.com/components/#glyphicons But when i am writing a conditional: {{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 1 ? 'You like this post' : '' : '' }} So i have only button with text '' What should i do to display html?
Me personally, I got confused with alot of those ternary operators chained one inside other, so I did it with if() and switch statement. I know that the code is a way longer in this way and its not that practical,,but its easier for me like that, so, anyway, if someone wants to see,here ya go: pastebin.com/dJCgZNRY
These videos have been so helpful! Thank you so much. I could learn from you all day long. Clear and concise. So Awesome!
So awesome to hear this, many thanks! :)
you really like the ternary operator man ^^
Great tutorial series!
It's handy, yes ;). Happy to hear you liked the series!
Sir, will you add notification system with this series.it'll be helpful for us.
Thank you!
+Soft Pdf
Hi, actually I will wrap up this series with the next video but no worries: I do have plans for new Laravel content :)
hi, nice tutorials :D
i really enjoyed it how you explain it step by step briefly, it's really help me that's still new on using Laravel framework hehe
well it's not went smooth sometimes when following your tutorial, because sometimes there's a bug on my project because there's some space on my html code lol
well it's worthed and relieving though if we can solved our bugs, eventhough we should trace the codes thoroughly line to line lol
well, keep on awesome bro
hope you'll always be success on your life :D
cheers ;)
Hey,one short simple question. What is the difference,for the example,when you make call like this: Auth::user()->likes()->somMethod() or Auth::user()->likes->someMethod(). What is the point of using "()" when for example fetching like for that particular user.
+Bora Manasijevic
Hi Bora,
the difference is, that using () returns a query builder object, i.e. it allows us to continue building the query. Using 'likes' without () accesses the returned results of a database query (i.e. all the likes of a user in this case).
So you use 'likes()' if you want to continue building your db query (i.e. chain where conditions or other relationship-calls onto it) and you use 'likes' if you want to get an array of all the likes for example.
+Mindspace Thaaaank you alot Max! Alot alot alot! You explain things in such a simple yet effective way. Thanks once more! :)
Awesome to hear that. Thank you!
Sorry, I want to ask a question.
My code runs smoothly, but when I click on it, Count's number needs to be rearranged before the number changes.
How can I make it update immediately?
why don't you use jquery and just make like or dislike invisible and togled on click? that would be easier and handy
i have noticed you are making a query inside a foreach loop does this mean your query will loop aswell because if so this will cause some major problems down the road i mean say you had a 1000 posts thats a 1000 quries to your database
then what do you suggest?
Thanks you very much!
Thank you for your comment!
I have added {{ DB::table('likes')->where([['like','=','1'], ['post_id', '=', '$post->id']])->count() }} this in views but it always showing 0. Why? Did I do anything wrong?
bro... did u solved the problem ??
Is there a security implementation that prevents a user from changing DOM after getting ID of post belonging to different user and editing that post as well despite being unauthorised?
Yes there is, this would be some extra step/check you'd need to implement.
Thanks for your response, just to confirm, will this be similar to what we did with delete, or is edit a bit different in terms of security implementation?
incase anyone wants to display total likes and dislikes:: {{ $post->likes->where('like',1)->count() }}
can you please show me the whole code ? thank you
This seems to be assuming that like is part of $post which it is not. I would like a good answer for this and when I find one I will post. I am working in 5.4 though
Hi, for some reason when everything is done, it throws a 500 internal server error on the console and lists the $.ajax method on the app.js file, along with some of the jquery files. Do you have any idea of possible reasons? Thanks so much for the tutorial btw, very detailed and easy to understand, I will be starting the Laravel course on Udemy tomorrow. :)
+Pam Austin
Hard to say, though it certainly is some error in the PHP code. Could you post your controller code?
Hi, I fixed the issue now, I forgot to include the 'use App\Like' at the top of the controller...thanks a lot.
I have a question regarding the blog on the Udemy course and I'm not sure if I should be asking here but basically I want to implement this project into the blog project and have this project as a basic forum page or a comment section for blog posts, but basically users need to be signed in to comment and I'm not sure how to do that as the Auth is set on for just the Admin on the blog.
Okay, great to hear that everything's working now!
I have a question regarding the blog on the Udemy course and I'm not sure if I should be asking here but basically I want to implement this project into the blog project and have this project as a basic forum page or a comment section for blog posts, but basically users need to be signed in to comment and I'm not sure how to do that as the Auth is set on for just the Admin on the blog.
You would set up the Auth mechanism the same way (you might name the model 'User', it makes more sense than 'Admin', but that does not change the way it is done). Then you would protect routes (like the comment route) the same way, routes are protected in the Admin area in the Udemy course project.
Thats great :*
+medraouf moussa
Happy to hear that :)
how would you go about showing a count of the likes and updating that count after the user likes it?
I think this code should do it: Like::where('post_id', $post->id)>where('like', 1)>get()
where do you place this code? Great series btw.
At the place where you want to fetch the liked posts ;)
$post->likes->where('like',1)->count()
Hi, thanks for your tutorial just want to ask if I want to show the number of like and dislike in a post how to do it??
+Rebecca Lau
Hi Rebecca,
you would need to extract it from the database like this (assuming you have the post already stored in a variable $post):
$number_likes = $post->likes->groupBy('count').
Or simply just get the likes like this:
$number_likes = count($post->likes)
Hi thanks for replying me that quickly, the code works but it don't separate the like and dislike. It just count the totally number of like and dislike in a post. To separate like and dislike what should i do? maybe use groupby ?? groupby 1 for like and groupby 0 for dislike?
Anyway many thanks for your tutorial bought the one in Udemy as well helps me a lots ^^
You're absolutely right, my fault. You would need to add a ->where('like',1) statement to the query.
Sorry to bother you again but i try {{$number_likes=count($post->likes->where('like',1)}} and {{$number_likes=$post->likes->where('like',1)->groupBy('count')}} both not working. Where should I exactly add the statement to the query?
Try the following:
$number_likes = count($post->whereHas('likes', function($query) {
$query->where('like', '=', '1');
});
Sorry for all the fragments. I'm currently at the airport and therefore can't test the code, but this should work. There would be other ways to achieve this, too, but this should work.
If your buttons don't work in Laravel 5.5, change the path in your master file for css and js from:
and
to
yeea thank man.
hello everyone. i have a 500 internal server error when i press the 'You like this post' button or 'You don't like this post'. Everything else works great...
how can we make the like and dislike coding properly coded and neat?
What do you mean with this?
you said in the video that the functionality of dom must be in the controller
Hm, really not sure what you mean... The DOM is manipulated via JS - there's no way Laravel could do this (besides sending views of course).
i want this part
{{ 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' }}
to be in the controller script
You can of course put the code into the controller and make sure that you're correctly passing it to the view then.
Excelent Tutorial!!! But i have an Error App\Like not found :(
Add this to the top of the controller : 'use App\Like'
So I came back to this video as a reference where we use AJAX to call function for likes and dislikes. I have something similar, but I have stuck my self in some problems.
I want to call a function that gets logged in status of all users and display that status on the screen. Function is to be called every 60 seconds.
problem is next: everything is fine after function is called first time, but every next time it returns 500 error. Please help me here, I'm starting to lose my mind :)
javascript function:
var chatPersons = document.querySelectorAll('.users-list-container ul li a span');
for(var i = 0; i < chatPersons.length; i++) {
var person = chatPersons[i],
user_id = chatPersons[i].dataset.userid;
updateUserStatus(user_id, person);
}
function updateUserStatus(user_id, person) {
console.log('Triggered')
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
url: checkIfLoggedURL,
type: 'POST',
data: { user_id: user_id }
})
.done(function(msg){
if(msg['loggedin'] == 'true') {
$(person).removeClass('disconnected').addClass('connected');
} else {
$(person).removeClass('connected').addClass('disconnected');
}
});
setTimeout(updateUserStatus, 60000);
}
Here is the Route:
Route::post('/isloggedin', [
'uses' => 'UserController@isLoggedIn',
'as' => 'isloggedin'
]);
somewhere in the file i declare variable for route
checkIfLoggedURL = '{{ route('isloggedin') }}';
and token:
and here is the php function that shall return the status of users
public function isLoggedIn(Request $request)
{
$user_id = $request['user_id'];
$user = User::find($user_id);
if($user->logged_in) {
return response()->json(['loggedin' => 'true'], 200);
} else {
return response()->json(['loggedin' => 'false'], 200);
}
}
I can't really dive super-deep into this as I can't offer great support beyond the scope of my videos. Simply due to time restrictions. But the first step to debug this error, is to go to the dev tools in your browser, open the network tab, find the failing requests (should be red), click on them and then click on "response". You should see the real error, Laravel is sending you. That might be a good starting point to understand what's going wrong.
I managed to solve it. For the sake of others who encounter the similar problem:
So i was calling php function for each user individually, and that's probably the reason for 500 error.
What I did is i changed the function and now I'm calling it once for all of the users and then extracting the online statuses from the array in javascript.
I've now copied over all my code to exactly yours but it still won't auto update.. for some reason my code never reaches the app.js click event. I have no errors but I try to console.log in the .done function and nothing
Hm ... do you get some 404 error that app.js is not found? Did you try to call another (dummy) method in the app.js file to simply see if that's the issue to begin with.
Strange... I just tried it today and it worked. I know I refreshed last time. Anyway, thanks for the response.
It is because of cache . If anyone have this problem then just clear your browsing data.
Seems that innerText doesnt work on Firefox
+Dada Arno
Hi, you're right, that's a mistake on my side. Correct would be to use 'textContent'.
OMG!
how can i set to change not text? I wanna change with gliphicons getbootstrap.com/components/#glyphicons
But when i am writing a conditional: {{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 1 ? 'You like this post' : '' : '' }}
So i have only button with text ''
What should i do to display html?
r u frm germany???
Yep, can't hide it I guess ;)
Me personally, I got confused with alot of those ternary operators chained one inside other, so I did it with if() and switch statement. I know that the code is a way longer in this way and its not that practical,,but its easier for me like that, so, anyway, if someone wants to see,here ya go:
pastebin.com/dJCgZNRY
+Bora Manasijevic
Hi, that's perfectly fine and I agree, that ternary operators can lead to some confusion. Thanks for sharing this!
I can't hear you properly, your mike is too weak :(
Sorry about that! Was one of the first videos, had to figure out stuff back then I guess ;)