2017-10-22 52 views
0

我想使用laravel 5.5在我的網站中製作喜歡和不喜歡的按鈕。當用戶登錄時,只有用戶可以喜歡或不喜歡其他方式,用戶會給出一條消息來登錄喜歡該帖子。如何在laravel 5.5中做出喜歡和不喜歡的東西?

在此先感謝您的幫助。

這裏是我的show.blade.php

@extends('layouts.app') 

    @section('content') 

     <div class="container" style="background-color: #fafafa;"> 
      <div class="col-md-12 col-lg-12 col-sm-12 col-xs-12"> 
         <div class="jumbotron" style="margin-top: 5%;"> 
         <h1> {{ $travels->title }} </h1> 
         <p class="lead">{{ $travels->description}}</p> 
         </div> 

       <br> 
      </div> 
     <button class="btn btn-default btn-xs" onclick="likeit('{{$travels->id}}', this)"><i class="fa fa-thumbs-o-up" aria-hidden="true"></i></button> 


@include('partials.comments') 

@guest 
    <p style="font-size: 1.5rem; color: #9a7fca;"><strong>To comment you have to login.</strong></p> 

@else 

<div class="row container-fluid"> 

<form method="post" action="{{route('comments.store')}}"> 
    {{ csrf_field() }} 


    <input type="hidden" name="commentable_type" value="App\Travel"> 
    <input type="hidden" name="commentable_id" value="{{$travels->id}}"> 


    <div class="form-group"> 
     <label for="comment-content">Comment</label> 
     <textarea placeholder="Enter comment" 
        style="resize: vertical" 
        id="comment-content" 
        name="body" 
        rows="3" spellcheck="false" 
        class="form-control autosize-target text-left"> 


        </textarea> 
    </div> 

    <div class="form-group"> 
     <input type="submit" class="btn btn-primary" 
       value="Submit"/> 
    </div> 
</form> 

</div> 
@endguest      

</div> 
@endsection 


@section('js') 
    <script type="text/javascript"> 
    function likeit($travelsID, elem){ 
     var csrfToken = '{{ csrf_token() }}'; 

     $.post("{{route('likeit')}}", {travelsID: travelsID,_token:csrfToken}, function(data){ 
     console.log(data); 
     }); 
    } 
    </script> 


@endsection 

這裏是我的LikeController.php

<?php 

namespace App\Http\Controllers; 

use App\Like; 
use App\LikeableTrait; 
use Illuminate\Http\Request; 
use Illuminate\Support\Facades\Input; 

class LikeController extends Controller 
{ 

    public function likeit(){ 
     $travelID = INPUT::get(key, 'travelID'); 
     $travels = Travel::find($travelID); 

     $travels->likeit(); 

     return response()->json(['status'=>'success']); 
    } 
} 

這裏是LikeableTrait

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class LikeableTrait extends Model 
{ 
    // 

    public function likes(){ 
     return $this->morphMany('Like', 'likeable'); 
    } 

    public function likeIt(){ 

    $like = new Like(); 

    $like->user_id=auth()->user()->id; 
    $this->likes()->save($like); 

    return $like; 
    } 
} 

這是我喜歡

<?php 

namespace App; 

// use App\User; 
use Illuminate\Database\Eloquent\Model; 

class Like extends Model 
{ 
    // 


    // protected $table = 'likeable'; 

    // In fillable we specify which fields are fillable 
    // In guarded we specify the fields which are not fillable 

    protected $guarded =[]; 



    /** 
    * Set method likeable to return polymorphic relationship 
    * 
    * @return \Illuminate\Database\Eloquent\Relations\MorphTo 
    */ 

    public function likeable() { 
     // morphTo mens to can be applied to any Model 

     return $this->morphTo(); 
    } 



    /** 
    * A Like belongs to a user. 
    * Set a relationship to see who liked something\ 
    * 
    * @return \Illuminate\Database\Eloquent\Relation\BelongsTo 
    */ 

    public function user(){ 
     return $this->belongsTo('User'); 
    } 
} 
+0

這是您的要求,請分享您的代碼和問題? – C2486

+0

你也可以看看Laracasts上的兩部分系列 - https://laracasts.com/lessons/favorites-part-1 - 它只包含這個 – kerrin

回答