Laravel 5.2 PHP Build a social network - Likes & Dislikes (3/3 - Updating the View)

Поделиться
HTML-код
  • Опубликовано: 4 ноя 2024
  • НаукаНаука

Комментарии • 90

  • @llnowlp
    @llnowlp 8 лет назад

    These videos have been so helpful! Thank you so much. I could learn from you all day long. Clear and concise. So Awesome!

    • @academind
      @academind  8 лет назад

      So awesome to hear this, many thanks! :)

  • @DonHaul
    @DonHaul 7 лет назад

    you really like the ternary operator man ^^
    Great tutorial series!

    • @academind
      @academind  7 лет назад

      It's handy, yes ;). Happy to hear you liked the series!

  • @softpdf6369
    @softpdf6369 8 лет назад +2

    Sir, will you add notification system with this series.it'll be helpful for us.
    Thank you!

    • @academind
      @academind  8 лет назад +1

      +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 :)

  • @adityajokoutomo
    @adityajokoutomo 8 лет назад

    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 ;)

  • @bora514
    @bora514 8 лет назад

    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.

    • @academind
      @academind  8 лет назад +1

      +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.

    • @bora514
      @bora514 8 лет назад

      +Mindspace Thaaaank you alot Max! Alot alot alot! You explain things in such a simple yet effective way. Thanks once more! :)

    • @academind
      @academind  8 лет назад

      Awesome to hear that. Thank you!

  • @陳威洋-q8p
    @陳威洋-q8p 5 лет назад

    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?

  • @agil-j4n
    @agil-j4n 6 лет назад

    why don't you use jquery and just make like or dislike invisible and togled on click? that would be easier and handy

  • @rosswilliamson8958
    @rosswilliamson8958 7 лет назад

    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

  • @yaokouassijean-claude1499
    @yaokouassijean-claude1499 6 лет назад

    Thanks you very much!

    • @academind
      @academind  6 лет назад

      Thank you for your comment!

  • @AshrafulFerdousFariv
    @AshrafulFerdousFariv 7 лет назад

    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?

  • @yadunandanpillai2899
    @yadunandanpillai2899 7 лет назад

    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?

    • @academind
      @academind  7 лет назад

      Yes there is, this would be some extra step/check you'd need to implement.

    • @yadunandanpillai2899
      @yadunandanpillai2899 7 лет назад

      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?

  • @sarojmaharjan9888
    @sarojmaharjan9888 8 лет назад +3

    incase anyone wants to display total likes and dislikes:: {{ $post->likes->where('like',1)->count() }}

    • @MeryamsGh
      @MeryamsGh 7 лет назад

      can you please show me the whole code ? thank you

    • @djjeshl
      @djjeshl 6 лет назад

      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

  • @missbabyblush
    @missbabyblush 8 лет назад

    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. :)

    • @academind
      @academind  8 лет назад

      +Pam Austin
      Hard to say, though it certainly is some error in the PHP code. Could you post your controller code?

    • @missbabyblush
      @missbabyblush 8 лет назад

      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.

    • @academind
      @academind  8 лет назад

      Okay, great to hear that everything's working now!

    • @missbabyblush
      @missbabyblush 8 лет назад

      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. 

    • @academind
      @academind  8 лет назад

      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.

  • @moussamohamedraouf2298
    @moussamohamedraouf2298 8 лет назад

    Thats great :*

    • @academind
      @academind  8 лет назад

      +medraouf moussa
      Happy to hear that :)

  • @llnowlp
    @llnowlp 8 лет назад

    how would you go about showing a count of the likes and updating that count after the user likes it?

    • @academind
      @academind  8 лет назад

      I think this code should do it: Like::where('post_id', $post->id)>where('like', 1)>get()

    • @electrokuted1488
      @electrokuted1488 8 лет назад

      where do you place this code? Great series btw.

    • @academind
      @academind  8 лет назад

      At the place where you want to fetch the liked posts ;)

    • @sarojmaharjan9888
      @sarojmaharjan9888 8 лет назад +1

      $post->likes->where('like',1)->count()

  • @rebeccal7704
    @rebeccal7704 8 лет назад

    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??

    • @academind
      @academind  8 лет назад

      +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)

    • @rebeccal7704
      @rebeccal7704 8 лет назад

      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 ^^

    • @academind
      @academind  8 лет назад

      You're absolutely right, my fault. You would need to add a ->where('like',1) statement to the query.

    • @rebeccal7704
      @rebeccal7704 8 лет назад

      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?

    • @academind
      @academind  8 лет назад

      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.

  • @stefan4800
    @stefan4800 7 лет назад

    If your buttons don't work in Laravel 5.5, change the path in your master file for css and js from:
    and
    to

  • @kevintumbo2882
    @kevintumbo2882 8 лет назад

    yeea thank man.

  • @eudaimonist90
    @eudaimonist90 7 лет назад

    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...

  • @MakoBec
    @MakoBec 8 лет назад

    how can we make the like and dislike coding properly coded and neat?

    • @academind
      @academind  8 лет назад

      What do you mean with this?

    • @MakoBec
      @MakoBec 8 лет назад

      you said in the video that the functionality of dom must be in the controller

    • @academind
      @academind  8 лет назад

      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).

    • @MakoBec
      @MakoBec 8 лет назад

      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

    • @academind
      @academind  8 лет назад

      You can of course put the code into the controller and make sure that you're correctly passing it to the view then.

  • @borklazer4775
    @borklazer4775 7 лет назад

    Excelent Tutorial!!! But i have an Error App\Like not found :(

    • @ottomanempire3746
      @ottomanempire3746 5 лет назад

      Add this to the top of the controller : 'use App\Like'

  • @Durlanac888
    @Durlanac888 8 лет назад

    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);
    }
    }

    • @academind
      @academind  8 лет назад +2

      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.

    • @Durlanac888
      @Durlanac888 8 лет назад

      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.

  • @Coop9900
    @Coop9900 8 лет назад

    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

    • @academind
      @academind  8 лет назад

      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.

    • @Coop9900
      @Coop9900 8 лет назад

      Strange... I just tried it today and it worked. I know I refreshed last time. Anyway, thanks for the response.

    • @Mayurdusane
      @Mayurdusane 8 лет назад

      It is because of cache . If anyone have this problem then just clear your browsing data.

  • @dadaarno6194
    @dadaarno6194 8 лет назад

    Seems that innerText doesnt work on Firefox

    • @academind
      @academind  8 лет назад

      +Dada Arno
      Hi, you're right, that's a mistake on my side. Correct would be to use 'textContent'.

  • @vrj93
    @vrj93 5 лет назад

    OMG!

  • @Armin120382
    @Armin120382 8 лет назад

    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?

  • @ashwinkarki8112
    @ashwinkarki8112 7 лет назад

    r u frm germany???

    • @academind
      @academind  7 лет назад +1

      Yep, can't hide it I guess ;)

  • @bora514
    @bora514 8 лет назад +1

    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

    • @academind
      @academind  8 лет назад +1

      +Bora Manasijevic
      Hi, that's perfectly fine and I agree, that ternary operators can lead to some confusion. Thanks for sharing this!

  • @ottomanempire3746
    @ottomanempire3746 5 лет назад

    I can't hear you properly, your mike is too weak :(

    • @academind
      @academind  5 лет назад

      Sorry about that! Was one of the first videos, had to figure out stuff back then I guess ;)