V
V
Verkan2021-10-30 13:01:28
SQL
Verkan, 2021-10-30 13:01:28

How to use where values ​​from another table?

Hello, I can't figure out how to compare values ​​from different tables.
I need to receive a Video where the company has the means to pay for the space.
Below is the full context.
Table schema (What needs to be compared is highlighted in color)
617d14e22c423645008621.png

Code snippet

return Video::whereHas('company',function (Builder $query) use ($themes){
                $query
                    ->where('status', true)
                    ->whereHas('companySchedules',function (Builder $query){
                        $query->where('weekday',now()->dayOfWeek);
                        $query->where('hour',now()->hour);})
                    ->whereHas('theme', function (Builder $query) use ($themes) {
                        $query->whereIn('id', $themes);}
                    )
                ;
            })->
            whereHas('videoDelay.themes',function (Builder $query) use ($themes){
                    $query->whereColumn('price','<=','company.balance'); //Тут должен подставиться баланс. С числом работает
                    $query->whereIn('id', $themes);}
                )
                ->moderation()
                ->orderByRaw("RAND()")
                ->first() ?? false;

*$themes is an array with id

Model settings:
video models

public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    public function videoDelay(): BelongsTo
    {
        return $this->belongsTo(VideoDelay::class);
    }

company model

public function videos(): HasMany
    {
        return $this->hasMany(Video::class);
    }


VideoDelay model

public function themes(): BelongsToMany
    {
        return $this->belongsToMany(Theme::class)->withPivot(['price']);
    }

    public function videos(): HasMany
    {
        return $this->hasMany(Video::class);
    }


Theme model

public function videoDelay(): BelongsToMany
    {
        return $this->belongsToMany(VideoDelay::class)->withPivot(['price']);
    }



PS if you have any questions, ask, I will answer promptly

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Verkan, 2021-10-30
@Verkan

Use join and that's it...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question