У меня есть две таблицы SALARIES и POINTAGES, и между ними есть отношение hasMany ownTo, я хочу отобразить для каждой POINTAGE соответствующую SALARIES, но это дает мне пустую таблицу данных. консультант.blade.php
@foreach($pointages as $pointage)
<tr>
<td>{{ $pointage->datep }}</td>
<td>{{ $pointage->chantier }}</td>
<td>{{ $pointage->ouvrage }}</td>
<td>{{ $pointage->nbrj }}</td>
<td>{{ $pointage->solde }}</td>
<td>{{ $pointage->salarie->nom }}</td>
</tr>
@endforeach
Pointage.php
protected $fillable = [
'salarie_id', 'datep', 'solde', 'nbrj' , 'ouvrage' , 'chantier' , 'prime' ,
];
public function salarie(){
return $this->belongsTo('App\Salarie');
}
Зарплата.php
public function pointages(){
return $this->hasMany('App\Pointage');
}
Миграция точки:
public function up(){
Schema::table('pointages', function (Blueprint $table) {
$table->integer('salarie_id')->unsigned()->after('id');
$table->foreign('salarie_id')->references('id')->on('salaries');
});
}
SalarieController.php
public function consulter()
{
$salaries = Salarie::with('pointages')->get();
$pointages = Pointage::with(["salaries"])->has("salarie")->get();
return view('salarie.consulter', compact('salaries','pointages'));
}






Некоторые вещи, которые вы могли бы попробовать:
with() вот так Pointage::with("salaries")->has("salarie")->get();In the example above, Eloquent will try to match the user_id from the Phone model to an id on the User model. Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id. However, if the foreign key on the Phone model is not user_id, you may pass a custom key name as the second argument to the belongsTo method.
Вам необходимо определить явные функции отношений:
// app\Salarie.php
class Salarie extends Model
{
protected $fillable = ['nome'];
public function pointages(){
return $this->hasMany('App\Pointage','salarie_id','id');
}
}
// app\Pointage.php
class Pointage extends Model
{
protected $fillable = [
'salarie_id', 'datep', 'solde', 'nbrj' , 'ouvrage' , 'chantier' , 'prime' ,
];
public function salarie(){
return $this->belongsTo('App\Salarie');
}
}
И используйте, как показано ниже, чтобы проконсультироваться со всеми точками, связанными с таблицей зарплат:
// app\Http\Controllers\SalarieController.php
class SalarieController extends Controller
{
public function consulter()
{
// test your model with this simple query
// $salaries = Salarie::find(1);
// $pointages = $salaries->pointages()->get();
// return view('salarie.consulter', compact('pointages'));
// if the upon test runs well, the follow codes will work
$salaries_ids = Salarie::with('pointages')->pluck('id');
$pointages = Pointage::whereHas('salarie', function($query) use ($salaries_ids) {
$query->whereIn('salarie_id', $salaries_ids);
})->get();
return view('salarie.consulter', compact('pointages'));
}
}
Надеюсь, это поможет, спросите меня, если вам нужно!