Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nagavinod424's avatar

How can i add is_liked property to videos api ?

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....

0 likes
5 replies
Nakov's avatar
Nakov
Best Answer
Level 73

@nagavinod424 you probably have authentication in place, so request()->user() is equal with auth()->user() have you tried that?

public function getIsLikedAttribute()
{
    if(auth()->check() && $user = auth()->user()){
        return $user->likes()->where('video_id', $this->id)->count() > 0;
    }
    return false;
}

This doesn't work?

nagavinod424's avatar

@nakov auth()->user() cannot be used while we are writing api calls it will through an error...

i tried it once...

thanks for the reply... but it is not suitable to me...

Nakov's avatar

@nagavinod424 so do you use Passport?

You can get the current user like this:

auth()->guard('api')->user()
nagavinod424's avatar

I got the issue now...

i was trying to get the user with an api which is not added with the middleware auth i.e. we can not get the user details when the route does not have "auth" middleware on it...

So when we are authenticated and want to access the requested user we can do like this :

$request->user('api');

// Or

auth('api')->user();

but we need to check that the user object is empty or not to validate the user login...

so the final code that we can use is :

public function getIsLikedAttribute()
    {
        $user = request()->user('api'); // to get the user details of the api token
        if(!empty($user)){
            return $user->likes()->where('video_id',$this->id)->count() > 0;
        }
        return false;
    }
nagavinod424's avatar

@nakov i am not using passport....

you are correct....

we can access the api user like that also...

Please or to participate in this conversation.