Я пытаюсь заставить работать любимую систему Laravel (https://github.com/ChristianKuri/laravel-favorite)). Я установил ее, как сказано в readme.
Когда я нажимаю кнопку «Мне нравится», она возвращает успех, но, похоже, функция не выполняется.
Я пробовал аякс так:
$('.btn-heart').on('click', function(){
$.ajax({
type: "GET",
url: 'http://localhost/view/<?php echo $post->post_id; ?>',
data: {post_id: '<?php echo $post->post_id; ?>'},
success: function(data){
alert('working')
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
})
или как это
$('.btn-heart').on('click', function(){
$.ajax({
type: "GET",
url: 'http://localhost/view/',
data: {post_id: '<?php echo $post->post_id; ?>'},
success: function(data){
alert('working')
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Status: " + textStatus); alert("Error: " + errorThrown);
}
});
})
Я должен пояснить, что post_id - это мой уникальный post_id из моей таблицы сообщений.
My view.blade.php:
<div class="card-body no-padding">
<div class="card-image-wrapper">
<div class="img-card-img">
<img src="{{ $post->post_url }}" class="card-image"/>
</div>
<div class="overlay-img"></div>
</div>
<div class="card-padding-user">
<div class="float-left">
<button class="btn btn-default btn-heart"><i class="far fa-heart icon-middle icon-thre"></i><i class="fa fa-heart icon-middle icon-non"></i> <span class="align-middle ml-2">0 Likes</span></button>
</div>
<div class="clearfix"></div>
</div>
</div>
Мой web.php:
Route::get('/view/{post_id}',"PostController@like");
Мой постКонтроллер:
public function like($post_id)
{
$post = Post::where('post_id', $post_id)->first();
$user = Auth::user();
$user->toggleFavorite($post);
return response()->json(['status' => 1]);
}
Моя пользовательская модель:
use Notifiable;
use Favoriteability;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'avatar', 'full_name'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function posts() {
return $this->hasMany(Post::class, 'creator','name');
}
Модель "Мой пост":
use Favoriteable;
protected $fillable = [
'creator', 'post_url', 'game', 'likes', 'created_at'
];
public function user() {
return $this->belongsTo(User::class);
}
Каким-то образом функция вообще не работает, в любимую таблицу ничего не вставляется.