public function isFavorited()
{
return !! $this->favorites->where('user_id', auth()->id())->count();
}
No query results for model [App\Thread] favorites
I have a thread where I can favorite the thread so I created a polymorphic relationship between the two of them(as replies of a thread also can be favorited).
I wrote a post and delete request for the thread in my vue:
methods: {
toggle(){
// check if thread is favorited
if(this.isFavorited){
axios.delete('/threads/' + this.thread.id + '/favorites').catch(function (error) {
if (error.response) {
// Request made and server responded
console.log(error.response.data);
console.log(error.response.status);
console.log(error.response.headers);
} else if (error.request) {
// The request was made but no response was received
console.log(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.log('Error', error.message);
}});
this.isFavorited = false;
this.count--;
}else{
console.log('/threads/'+this.thread.id+'/favorites');
axios.post('/threads/'+this.thread.id+'/favorites');
this.isFavorited = true;
this.count++;
}
}
}
And my routes in web.php:
//Post favourite to a thread
Route::post('/threads/{thread}/favorites','FavoritesController@storeThreads');
//Delete a favourited thread
Route::delete('/threads/{thread}/favorites','FavoritesController@destroyThreads');
in my controller:
public function storeThreads(Thread $thread){
$thread->favorite();
return back();
}
public function destroyThreads(Thread $thread){
$thread->unfavorite();
}
and in my Thread model:
public function favorites(){
return $this->morphMany('App\Favorite','favorited');
}
public function favorite(){
$attributes = ['user_id'=>auth()->id()];
if(! $this->favorites()->where($attributes)->exists()){
return $this->favorites()->create($attributes);
}
}
public function unfavorite(){
$attributes = ['user_id'=>auth()->id()];
if($this->favorites()->where($attributes)->exists()){
return $this->favorites()->where($attributes)->delete();
}
}
public function isFavorited(){
return $this->favorites->where('user_id',auth()->id())->count();
}
public function getIsFavoritedAttribute(){
return $this->isFavorited();
}
public function getFavoritesCountAttribute(){
return $this->favorites->count();
}
my Favourite model:
class Favorite extends Model
{
use RecordsActivity;
protected $guarded =[];
public function favorited(){
return $this->morphTo();
}
}
So when I send the post request to favorite the thread, it works as expected but when I unfavorite it, it does not delete the data from the db and says:
DELETE http://forum.test/threads/25/favorites 404 (Not Found)
No query results for model [App\Thread] favorites
From this error, it's clearly referring that it could not find the query for thread id=25. I checked my route:list and there is a delete route. I also checked my threads table and there is id 25 as well as favorites table as well.
Reference:
https://ibb.co/KKQVJTX (threads table)
https://ibb.co/NNLvpXK (favorites table)
I tried using php artisan tinker to delete it and it works though so I don't know what am I missing.
I am out of idea on how to fix this issue or I may have overlooked something. How do I solve this?
Please or to participate in this conversation.