G
G
GaserV2017-08-18 23:12:04
Laravel
GaserV, 2017-08-18 23:12:04

Why does belongsToMany always return 1?

You need to check if the user applied for the job. But relation always returns 1. What is the problem?

public function applied($job_id)
    {
      $this->belongsToMany('App\Job', 'job_applies');
    }

And in the view, depending on this, show\hide the response button:
{{count($user->applied($job->id))}}
In applied(), for now, I just return the result for the test, but here I stalled. count() gives 1 when it should be 3.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
miki131, 2017-08-19
@GaserV

First, you declared a parameter $job_idbut you don't use it anywhere.
Secondly,

// User model
public function jobs()
{
    return $this->belongsToMany('App\Job', 'job_applies');
}
public function applied($job_id)
{
    return $this->jobs()->where('job_id', $job_id)->count() > 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question