0

I've got 4 tables.

Clients Jobs Rounds Job_Rounds

I've created the following relationships:

Clients > Jobs
Jobs > Rounds

Here are my models

Client

class Client extends Model
{   

    protected $dates = [
        'created_at',
        'updated_at',
        'last_done'
    ];


    public function Jobs()
    {
        return $this->hasMany('App\Job','client_id');
    }
}

Job

class Job extends Model
{
    protected $dates = [
        'created_at',
        'updated_at',
        'last_done',
        'due_date'
    ];

    public function Round()
    {
        return $this->belongsTo('App\Round','round_id');
    }

    public function Client()
    {
        return $this->belongsTo('App\Client','client_id');
    }
}

Round

class Round extends Model
{
    protected $dates = [
        'created_at',
        'updated_at',
        'date_scheduled',
        'date_finished'
    ];

    public function Clients()
    {
        return $this->hasMany('App\RoundClients', 'round_id');
    }

    public function Jobs()
    {
        return $this->belongsToMany('App\Job', 'job_rounds', 'round_id', 'job_id');
    }
}

JobRound

class JobRound extends Model
{
    public $timestamps = false;
}

On my round view I'm outputting all the jobs not in the round and all the jobs in the round.

I'm doing this by creating a DB::table to perform the check - see my code:

public function show($id)
    {   
        $round = Auth::user()->rounds()->FindOrFail($id);

        $not = DB::table('jobs')->whereNotIn('id', function($query) use($round) {
        $query->select('job_id')
          ->from('job_rounds')
          ->where('round_id', $round->id);
        })->get();

        $are = DB::table('job_rounds')
        ->join('jobs', 'job_rounds.job_id', '=', 'jobs.id')
        ->select('job_rounds.*', 'jobs.*' ,'job_rounds.id as DAS')
        ->where('job_rounds.round_id',$round->id)
        ->get();   

        return view('app.round',compact('round','not','are'));
    }

The problem I have is in my view, the Client to Job relationship for outputting the client name won't work as I have created my own DB query, I'm outputting the client information on my view like

Jobs on the round

@foreach($are as $job)
  {{ $job->client->first_name }}
@endforeach

Jobs not on the round

@foreach($not as $job)
  {{ $job->client->first_name }}
@endforeach

I get the error:

Variable $client not found etc

5
  • have you defined the relationship on the models? can you post the model files for each one Commented Sep 8, 2016 at 15:08
  • @Carlos I'm not familiar with doing it in the relationship, i've included the models above. Commented Sep 8, 2016 at 15:14
  • why not using basic database table level relationships? Commented Sep 8, 2016 at 15:39
  • @Sachith because job can be related to many rounds. Commented Sep 8, 2016 at 15:40
  • if you use normal database relationship methods, each job has a client, then each job round has a job.so automatically job round and client relationship is made. Commented Sep 8, 2016 at 15:47

2 Answers 2

3

Your relationships methods starts with uppercase (which btw, you should change to start with lowercase for standard).

The problem is that your methods and the ones you call are not the same, you need to use the same case in both, like this...

@foreach($are as $job)
    {{ $job->Client->first_name }}
@endforeach

Update

Besides the fact of the case, you are not using Eloquent.
You are just using the database query builder directly.
In order to use Eloquent you must use the Model class static methods, like this:

Job::whereNoIn('id', .....

Instead of this:

DB::table('jobs')->whereNotIn('id', ....
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Nestor, I've tried that but I still get the Undefined property: stdClass::$Client
Ohh, I see, you are not using Eloquent, that is why. You are using the Database Query Builder. If you want to use Eloquent you have to call the Class static methods, like Job::whereNotIn(..... instead of DB::table('jobs'). If you don't use Eloquent, those relationships are not available.
I'd love to make to code look cleaner in my controller though :( ha.
2

DB::table does not return an App\Job class. For this reason you cannot use relationship. ( $job->Client... )

Try make the queries using Eloquent instead.For more details, follow this offical Laravel documantation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.