如何在Laravel 5.4中加入两个表

时间:2018-11-19 07:10:02

标签: php laravel laravel-5

我正在学习Laravel 5.4。但是我被困在一个地方。

我有两个表课程名称分支名称

分支名称表包含: ID,分支名称,课程ID

课程名称表包含: ID,课程名称

课程名称具有唯一的名称,而分支名称的course_id具有课程名称的ID,可以重复。

我的BranchNameController是:

public function index()
{
    $branchname = branchname::all();
    $coursename = coursename::all();
    return view('admin.branchname',compact('branchname','coursename'));
}

branchname.blade.php是:

<tbody>
   @foreach($branchname as $branchnames)
   <tr>
      <td>{{ $branchnames->branchname }}</td>
      <td>{{ $branchnames->course_id }}</td>
   </tr>
   @endforeach     
</tbody>

我要显示课程名称,而不是 {{$ branchnames-> course_id}} 。怎么做?

模型branchname.php是:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class branchname extends Model
{
//
}

模型coursename.php是:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class coursename extends Model
{
//
}

5 个答案:

答案 0 :(得分:4)

您可以使用Laravel关系来实现此目的。您的分支名称course_id列属于课程表。

在分支名称模型中添加以下关系:

public function course() {
    return $this->belongsTo(\App\coursename::class, 'course_id')
}

然后在您的控制器索引函数中使用:

public function index()
{
    $branchname = branchname::with('course')->get();
    $coursename = coursename::all();
    return view('admin.branchname',compact('branchname','coursename'));
}

然后在视图中,您可以将名称显示为:

<td>{{ isset($branchnames->course) ? $branchnames->course->coursename : "" }}</td>

答案 1 :(得分:3)

尝试一下
branchName模型    

namespace App;

use Illuminate\Database\Eloquent\Model;

class branchname extends Model
{
  public function course(){
    return $this->belongsTo(coursename::class, 'coursename_id');
  }
}

// courseName模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class coursename extends Model
{
  public function branch(){
    return $this->hasOne(branchname::class, 'coursename_id');
  }
}

答案 2 :(得分:2)

在laravel see中使用join方法

 public function index()
    {
        $branchname = branchname::join('coursenames','coursenames.id','=','branchnames.course_id')->select('coursenames.id as course_id','branchnames.branchname')->get();
        return view('admin.branchname',compact('branchname'));
    }

答案 3 :(得分:1)

 $branchname = branchname::join('coursenames','coursenames.id','=','branchnames.course_id')->select('coursenames.id as course_id','branchnames.branchname')->get();

现在使用口才:

在Branchname模型中

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class branchname extends Model
{

  // creating a one to many relationship with model coursename

  public function coursename(){

    return $this->hasMany('App\coursename');
  }

}

在课程名称模型中:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class coursename extends Model
{
   public function branchname(){

    return $this->belongsTo('App\branchname');

   }
}

在控制器中:

public function index()
{
    $branchname = branchname::all();
    return view('admin.branchname')->with('branchname',$branchname);

}

在视图branchname.blade中:

<tbody>
    @foreach($branchname as $branchnames)
     <tr>
      <td>{{ $branchnames->branchname }}</td>
      <td>{{ $branchnames->coursename->course_name }}</td>
     </tr>
    @endforeach     
</tbody>

答案 4 :(得分:0)

您可以尝试

-> branchname.php(模型)

namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class branchname extends Model
{
  public function course()
  {
      return $this->belongsTo(coursename::class,'course_id','id');
  }
}

-> BranchNameController.ph p

namespace App\Http\Controllers;
use App\Model\branchname;
use Illuminate\Http\Request;
use App\Model\coursename;

public function index()
{
   $branchname = branchname::orderBy('updated_at','DESC')->paginate(15);
    return view('branchname.branchname',['page'=>'Branchname','branchname'=>$branchname]);
}

-> branchname.blade.php

<tbody>
@foreach($branchname as $items)
<tr>
  <td>{{ $items->branchname }}</td>
  <td>{{ $items->course->id }}</td>
</tr>
@endforeach     
</tbody>