在Laravel中标记实现逻辑

时间:2018-08-10 02:49:53

标签: eloquent laravel-5.4

每个问题都有一个以上学科(也称为标签),每个学科ID存储在我的问题表中的学科ID字段中;格式为2,6,3,5。我成功地获取了与问题相关联的纪律ID并对其进行了内爆。我想使用内含的ID来获取与学科ID关联的学科名称。

Schema::create('disciplines', function (Blueprint $table) {
        $table->increments('id');
        $table->string('discipline');
        $table->text('description');
        $table->timestamps();
    });


Schema::create('questions', function (Blueprint $table) {
        $table->increments('id');
        $table->text('questions');
        $table->integer('user_id');
        $table->string('discipline_id');
        $table->string('visibility_code')->default(false);
        $table->integer('status');
        $table->timestamps();
    });


//MY QUESTION MODEL I HAVE THIS RELATION 

 //a Question belogs to a discipline
 public function disciplines(){
    return $this->belongsToMany(Discipline::class);
}

//DISCIPLINE MODEL
  class Discipline extends Model
 {

 }



 class HomeController extends Controller  
 {
    public function index()
     {
       $questions = Question::where('visibility_code', 0)->get();

       $url = \Request::root();
       $title = 'Home - Scoreg';
       return view('welcome',compact('title','questions','url'));

    }

  }


//MY VIEW 
@foreach(explode(',',$question->discipline_id)  as $discipline)
    {{$discipline->disciplines->discipline}}
@endforeach

0 个答案:

没有答案