php - Laravel views - passing multiple arrays to a view -
within laravel app, pass list of jobs completed view , send on number of messages have been sent job:
public function index() { $jobs = $this->job->getusersownjobs(auth::id()); $jobid = $jobs['id']; // example of i'm trying $count = $this->message->model->wherehas('conversation.job', function($q) use ($jobid) { $q->where('id', $jobid); }) ->count(); return view('myjobs.index', compact('jobs', 'count')); }
my database structured when user clicks on job , sends message, row in conversation table created holds id of job (my detailed schema can found here: http://www.laravelsd.com/share/8nbzmc)
the count function should dependent on id of job, however, how pass on id $jobs array, $count variable, load views? should using view composers though appear in 1 view?
edit: trying list jobs in view , display next them, respective messages count
it's not clear issue is, think want count using jobs ids, right?
so if that's case, here need:
public function index() { $jobs = $this->job->getusersownjobs(auth::id()); $jobsids = $jobs->lists('id'); // give array ids of jobs want. $count = $this->message->model->wherehas('conversation', function($q) use ($jobid) { $q->wherein('job_id', $jobsids); }) ->count(); return view('myjobs.index', compact('jobs', 'count')); }
edit:
if need have messages count each job, this:
job model:
public function messages() { return $this->hasmanythrough('message', 'conversation'); }
so in view can call messages directly:
foreach($jobs $job) { $job->messages()->count(); }
or, if eager loaded messages relation:
foreach($jobs $job) { $job->messages->count(); }
Comments
Post a Comment