Fixed it
Many to Many Relationship is not Working as it should
I am making an application for school and It is adding assignments. students are able to attend to the assignment with asking a interview trough the application. They might get the assignment. but the teacher first need to see who asks for an interview.
I try to show all assignments what the teacher made and check if there is an interview. but the sql seems to be correct but it only shows one interview even tho there are to intervies
Gesprek = Interview Opdracht = Assignent Docent = Teacher
** Interview check **
$opdrachten = Users::find(Auth::user()->id)->Opdracht->pluck('id');
foreach($opdrachten as $opdracht) {
$gespreken = Opdracht::find($opdracht)->TeamGesprek;
}
dd($gespreken);
return view('docent')->with('gespreken', $gespreken);
** Interview View Blade **
@isset($gespreken)
@foreach($gespreken as $gesprek)
@if(\App\TeamOpdracht::where('opdracht_id', '=', $gesprek->id) != null)
<div class="col-xs-12 col-sm-6 col-md-4">
<div class="image-flip">
<div class="mainflip">
<div class="frontside">
<div class="card">
<div class="card-body text-center">
<h4 class="card-title">Assignment: {{ $gesprek->title }}</h4>
@foreach($gesprek->pivot as $gesprek1)
@if(\App\Gesprek::find($gesprek1->pivot->id)->pluck('check')->first() != 1)
<p class="card-text">Interview with: {{ $gesprek1->name }}</p>
<form action="{{ route('gesprek/accept', $gesprek1->pivot->id) }}" method="GET">
@csrf
@method('GET')
<button class="btn btn-danger" type="submit"><i class="fa fa-check"> Interview Done</i></button>
</form>
@else
<p class="card-text">Interview is done!</p>
<form method="POST" action="{{ route('team/select') }}">
@method('POST')
<div class="form-group">
@csrf
<input type="hidden" value="{{$gesprek1->id}}" name="team_id" />
<input type="hidden" value="{{$gesprek->id}}" name="opdracht_id" />
<label class="@if($errors->has('coin')) text-danger @endif" for="name">* Assignments Coins:</label>
<input type="text" class="form-control @if($errors->has('coin')) text-danger border border-danger @endif" name="coin" value="{{ old('coin') }}"/>
<br>
@if ($errors->has('coin'))
<div class="alert alert-danger" style="padding: 2px !important; height: 30px;">
<ul>
@if ($errors->has('coin'))
<li>{{ $errors->first('coin') }}</li>
@endif
</ul>
</div>
@endif
</div>
<div class="form-group">
<label for="quantity" class="@if($errors->has('startdate')) text-danger @endif">* Assignment Start Date:</label>
<input type="date" class="form-control @if($errors->has('startdate')) text-danger border border-danger @endif" placeholder="yyyy-mm-dd" name="startdate" value="{{ old('startdate') }}"/>
<br>
@if ($errors->has('startdate'))
<div class="alert alert-danger" style="padding: 2px !important; height: 30px;">
<ul>
@if ($errors->has('startdate'))
<li>{{ $errors->first('startdate') }}</li>
@endif
</ul>
</div>
@endif
</div>
<div class="form-group">
<label for="quantity" class="@if($errors->has('enddate')) text-danger @endif">* Assignment Close Date:</label>
<input type="date" class="form-control @if($errors->has('enddate')) text-danger border border-danger @endif" placeholder="yyyy-mm-dd" name="enddate" value="{{ old('enddate') }}" />
<br>
@if ($errors->has('enddate'))
<div class="alert alert-danger" style="padding: 2px !important; height: 30px;">
<ul>
@if ($errors->has('enddate'))
<li>{{ $errors->first('enddate') }}</li>
@endif
</ul>
</div>
@endif
</div>
<button type="submit" class="btn btn-primary">Add team to assignment</button>
</form>
</div>
</div>
@endif
@endforeach
</div>
</div>
</div>
</div>
@endif
@endforeach
@endisset
** Assignent Model **
namespace App;
use Illuminate\Database\Eloquent\Model;
class Opdracht extends Model
{
protected $table = 'opdracht';
public $timestamps = true;
protected $fillable = array('title', 'description_id', 'start_date', 'end_date');
protected $visible = array('id','title', 'description_id', 'start_date', 'end_date');
public function Description()
{
return $this->belongsTo(Description::class);
}
public function Users()
{
return $this->belongsToMany(Users::class, 'user-opdracht');
}
public function TeamOpdracht()
{
return $this->belongsToMany(Team::class, 'team-opdracht')->withPivot('id');
}
public function TeamGesprek()
{
return $this->belongsToMany(Team::class, 'gesprek');
}
}
** Team Model **
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Users;
class Team extends Model
{
protected $table = 'team';
public $timestamps = false;
protected $fillable = array('name', 'point');
protected $visible = array('id','name', 'point');
public function Users()
{
return $this->belongsToMany(Users::class, 'team-user');
}
public function OpdrachtTeam()
{
return $this->belongsToMany(Opdracht::class, 'team-opdracht');
}
public function OpdrachtGesprek()
{
return $this->belongsToMany(Opdracht::class, 'gesprek');
}
}
** Interview Model **
namespace App;
use Illuminate\Database\Eloquent\Model;
class Gesprek extends Model
{
protected $table = 'gesprek';
public $timestamps = true;
protected $fillable = array('team_id', 'opdracht_id', 'check');
protected $visible = array('id','team_id', 'opdracht_id', 'check');
}
Please or to participate in this conversation.