如何在laravel 5.5中做出喜欢和不喜欢的东西?
问题描述:
我想使用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');
}
}
这是您的要求,请分享您的代码和问题? – C2486
你也可以看看Laracasts上的两部分系列 - https://laracasts.com/lessons/favorites-part-1 - 它只包含这个 – kerrin