Hi Guys, I am little new to the Eloquent relationships kinda beginner, I am trying to implement a like and dislike functionality which working fine on the web
I have developed an api to fetch videos but i want to add a dynamic property is_liked in the api response
i am able to add the property but the boolean value passing is wrong i am unable to access the requested user of the user in the model or laravel
because of that it throwing false always...
my VideoController :
class VideoController extends Controller
{
/**
* Display a listing of the resource.
* @return VideoResource|Response
*/
public function index()
{
$videos = Video::where('is_active', true)->where('video_status','PUBLISHED');
$videos = $videos->with(['category', 'user','language'])->latest()->paginate(10);
return new VideoResource(['success' => true, 'message' => 'Videos Details..!', 'data' =>$videos]);
}
my Video Modal:
public function getIsLikedAttribute()
{
$user = request()->user(); // unable to access the user
if(!empty($user)){
return $user->likes()->where('video_id',$this->id)->count() > 0;
}
return false;
}
guys please give me any suggestion to solve this problem...
Thank you in advance....