mohamedanis

mohamedanis

Member Since 1 Week Ago

Experience Points
50
Total
Experience

4,950 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
50 XP
May
20
1 week ago
Activity icon

Started a new Conversation Favorite Unfavorite Button Problem Laravel

I'm working on a Laravel project where user can favorite and unfavorite books, in the two cases the DB changes successfully without page reloading as i used ajax , but i have two problems:

-first problem : if there is no favorite books, favorite button disappears and i need to edit database to add some book to favorites table and show the button again. - second problem is that when more than one book is favorited when i refresh the page, the favorite button is duplicated .

-here is the code in my controller:

class FavoriteController extends Controller { public function bookFavBook(Request $request){ $book_id = $request['bookid'];

    $fav = DB::table('favorites')
    ->where('book_id', $book_id)
    ->where('user_id', Auth::user()->id)
    ->first();
    if(!$fav){
        $newfav = new Favorite;
        $newfav->book_id =$book_id; 
        $newfav->user_id = Auth::user()->id;
        $newfav->fav = 1;
        $newfav->save();
        $is_fav = 1;  
    }
    elseif ($fav->fav == 1){
        DB::table('favorites')
    ->where('book_id', $book_id)
    ->where('user_id', Auth::user()->id)
    ->delete();
    $is_fav = 0;
    }
    elseif ($fav->fav == 0){
        DB::table('favorites')
    ->where('book_id', $book_id)
    ->where('user_id', Auth::user()->id)
    ->update(['fav'=> 1] );
    $is_fav = 1;
    }
    $response = array(
        'is_fav'=>$is_fav,
    );
    return response()->json($response, 200);
} 

-and the code in my view :

@foreach( $books as $book)

        <div class="container-fluid column">
          <div class="row flex-row flex-nowrap">
            <div class="col-md-4">
              <div class="card card-block">
                <div>
                  <img
                    class="card-img-top"
                    src="/images/{{ $book-> book_img}}"
                    alt="Card image cap"
                  />
                </div>

                  <h5 class="card-title"> <a href="/books/{{ $book->id }}">{{ $book ->title}}</a> </h5>
                  <p class="card-text">
                    {{ $book-> description}}
                  </p>
                   <div class="card">

                  <div class="mb-3">
                    <span class="badge badge-pill badge-primary p-2 mr-4">
                      <span class="count_of_book">{{ $book-> amount }}</span>
                      copies available
                    </span>

query books from favorites table @php $getbook = DB::table('books') ->join('favorites','favorites.book_id','=', 'books.id' ) ->where('favorites.user_id','=',Auth::id()) ->get(); @endphp

                    @foreach($getbook as $fbook) 
                    @if($fbook->book_id == $book->id )
                    <i id="favorite" data-bookid="{{ $book-> id }}" class="fas fa-heart fa-2x text-danger"></i>
                    @else
                    <i id="favorite" data-bookid="{{ $book-> id }}" class="fas fa-heart fa-2x text-dark"></i>
                    @endi
                    @endforeach  
              </div>
            </div>
            @endforeach 
            </div>
            {{ $books->links() }}
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
var token = '{{ Session::token()}}';
var urlFav = '{{ route('favor') }}';
</script>

-and the ajax code in js file:

 $(".fa-heart").on("click", function (event){
bookid = $(this).data("bookid");
$.ajax({
    method: 'POST',
    url: urlFav,
    data: {bookid: bookid , _token:token},
    success: function(data){

        if(data.is_fav == 1){
            $(event.target).removeClass("text-dark").addClass("text-danger");

        }
        if(data.is_fav == 0){
            $(event.target).removeClass("text-danger").addClass("text-dark");
        }
    }

})