3

I have three classes Student, Session and student classes it has a relationship (Student with Enrollment, Session with Enrollment and student Class with enrollment), when I do echo it show the result [{"id":1,"school_id":1,"class_id":1,"student_id":1,"session_id":1,"created_at":null,"updated_at":null}] ,i have plucked value from student class

{{implode(',',$en->school()->get()->pluck('school_name')->toArray())}}

Note it is working


same from Session it is also working but when I plucked from student class and need class_name it not show an error but not the plucked value from studentclass here is the same type of query in blade.php

{{implode(',',$en->studentclass()->get()->pluck('class_name')->toArray())}}

here is the relationship one to many in StudentClass Controller

public function enrollments()
    {
        return $this->hasMany(Enrollement::class);
    }

Enrollement Class

public function studentclass()
    {
        return $this->belongsTo(StudentClass::class);
    }

how to pick the class_name value from table using this relationship. thanks

inasar
  • 51
  • 1
  • 8

3 Answers3

3

Firstly, note that you can use the implode method on the illuminate/collection method:

{{ $en->school()->get()->pluck('school_name')->implode(', ') }}

which can be further shortened to

{{ $en->school->pluck('school_name')->implode(', ') }}

The studentclass can be plucked like so

{{ $en->studentclass->pluck('enrollments.class_name')->implode(', ') }}

using dot syntax to access the class name.

to prevent this from creating a n+1 issue and reduce the number of queries ran, you should lazy load the relationship:

{{ $en->load('studentclass.enrollments')->studentclass->pluck('enrollments.class_name')->implode(', ') }}

you could also load it in advance:

$en = Enrollment::with('studentclass.enrollments')->where('id', $id)->first();
Andrew Willis
  • 2,289
  • 3
  • 26
  • 53
  • {{$en->school()->pluck('school_name')->implode(', ') }} or {{implode(',',$en->school()->get()->pluck('school_name')->toArray())}} Both work but it not work to pick the class_name from studentclass function even Relationship is OK , if i echo the $en it get the class_id from Enrollement class – inasar Jul 05 '20 at 21:21
  • echo $en show the following result [{"id":1,"school_id":1,"class_id":1,"student_id":1,"session_id":1,"created_at":null,"updated_at":null}] it mean we have class_id why we cannot pick the class_name using relationship function studentclass in Enrollement Model – inasar Jul 05 '20 at 21:28
  • If you see the second example in my answer, it shows how to achieve this. – Andrew Willis Jul 05 '20 at 22:04
0

You can pluck the relationship's column using simple . notation as given below.

{{ implode(',', $en->studentclass()->pluck('studentclass.class_name')->toArray()) }}

Here, it forms as, relationship.column

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
  • {{$en->school()->pluck('school_name')->implode(', ') }} or {{implode(',',$en->school()->get()->pluck('school_name')->toArray())}} Both work but it not work to pick the class_name from studentclass function even Relationship is OK , if i echo the $en it get the class_id from Enrollement class – inasar Jul 05 '20 at 21:22
  • echo $en show the following result [{"id":1,"school_id":1,"class_id":1,"student_id":1,"session_id":1,"created_at":null,"updated_at":null}] it mean we have class_id why we cannot pick the class_name using relationship function studentclass in Enrollement Model – inasar Jul 05 '20 at 21:28
0

I have changed the studentclass function in Enrollment Model

public function studentclass()
    {
        return $this->belongsTo(StudentClass::class,'id');
    }

just by adding, 'id' and pluck from view

{{implode(',',$en->studentclass()->get()->pluck('class_name')->toArray())}}
inasar
  • 51
  • 1
  • 8