简体   繁体   中英

Select desired columns data from a many to many relation

I have three tables employs , courses and employ_course (pivot table). I have many columns in employ (ex name, emp no, salary etc) and my course table has only id and course_name columns, and my pivot table has id , employ_id and course_id .

This is my view:

<table border="1">
   <tr>
     <th>Full name</th>
     <th>Emp no</th>
     <th>salary</th>
     <th>course</th>
   </tr>
   @foreach($data as $item) 
     <tr>
       <th>{{$item['Full_name']}} </th>
       <th>{{$item['emp_no']}}</th>
       <th>{{$item['salary']}}</th>
       <th>{{$item['course_name']}}</th>
     </tr>
    @endforeach
 </table>

This is my employ model class:

class employ extends Model
{
    use HasFactory;
    public $timestamps = false;

    protected $guarded = [];

    public function courses() {
        return $this->belongsToMany(course::class, 'employ_course');
    }
}

This is my course model class:

class course extends Model
{
    use HasFactory;
    public $timestamps = false;

    protected $guarded = [];

    public function employs() {
        return $this->belongsToMany(employ::class, 'employ_course');
    }
}

And this is my employ_course model class:

class employcourse extends Model
{
    use HasFactory;

    protected $guarded = [];
}

This is the code in my controller:

public function show()
{
    $data = employ::with('course')->get();
    return view('teacher.teacher_data', compact('data'));
}

I only want to show some specific columns from employ table like fullname , salary , emp no etc and course name only in my view blade in the form of table.

Problems

You need to fix your models: When creating a many to many relationship you should pay attentions Which class belongs and which class has many relations:

Employ model belongsTo()

class employ extends Model
{
    use HasFactory;
    public $timestamps=false;

    protected $guarded=[];

    public function courses(){
        return $this->belongsToMany(course::class, 'employ_course'); // The employ belongs to course
    }
    
}

Course model hasMany()

class course extends Model
{
    use HasFactory;
    public $timestamps=false;

    protected $guarded=[];


    public function employs(){
        return $this->hasMany(employ::class, 'employ_course'); // courses has many employees
    }
}

Update your controller to this:

public function show()
    {
        $datas = employ::with('course')->select('fullname', 'salary', 'emp_no')->get();
        return view('teacher.teacher_data', compact('datas'));
    }

the select() method will extract the specific field of targeted model directly. ie, it will select the desired fields from employ having relation with course read more about select

Update

Select the columns from other table and return the collection instead of calling contact() :

public function show()
    {
        $datas = employ::with(['course' => function ($query) {
            $query->select('column_from_course_table');
            }])
        ->select('fullname', 'salary', 'emp_no')->get();
        return view('teacher.teacher_data', ['datas' => $datas]); // return
    }

the query inside this with() will select the column from course table. In your results now you will be able to fetch this by following this syntax in your view:

$dats->course->column_from_course_table // ie, course_name

As updated syntax :

@foreach($datas as $item) 
     <tr>
       <th>{{$item->Full_name}} </th>
       <th>{{$item->emp_no}}</th>
       <th>{{$item->salary}}</th>
       <th>{{$item->course->course_name}}</th> // Edit this line
     </tr>
@endforeach

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM