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

Rjonwal's avatar

laravel getting data from two tables

Someone please help me ,

i want to get data from two tables

table first => contest

table second = > contest_details

relationship => one to many

contest hasMany ContestDetail

I'm using this function but getting error

    $referralContests = Contest::where([['type', '=', 'referral'], ['start_date', '>', $current_date]])->whereHas('contestDetail', function ($query) use ($this->id){
        $query->where('contest_id ', $this->id);
    })->get();

error : Symfony\Component\Debug\Exception\FatalThrowableError syntax error, unexpected '->' (T_OBJECT_OPERATOR), expecting ')' or ','

help me.

0 likes
9 replies
tisuchi's avatar

@rjonwal

Can you show more of your error in which line or file you are getting the error?

CliffordAtCaveoDotNL's avatar

The use ($this->id) is the problem:

$referralContests = Contest::where([['type', '=', 'referral'], ['start_date', '>', $current_date]])->whereHas('contestDetail', function ($query) {
        $query->where('contest_id ', $this->id);
    })->get();
Rjonwal's avatar

Yes , but what i do now i want to get all the records

what i have to replace at $this>id

please tell me

i'm new in laravel

my 2 days are wasted on this...

CliffordAtCaveoDotNL's avatar

I think what is going on here, you want only the contests which have contestDetails right? In that case (if you have set up your relations correctly) you only have to do this:

$referralContests = Contest::where([['type', '=', 'referral'], ['start_date', '>', $current_date]])
    ->whereHas('contestDetail')
    ->get();

Please or to participate in this conversation.