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
また、Laracastの2つのパートシリーズを見てみることもできます - https://laracasts.com/lessons/favorites-part-1 - ちょうどこれをカバーします – kerrin