В моем приложении Laravel -5.8 я пытаюсь просмотреть динамическую c входную запись по индексу.
Это блейд индексного представления:
public function index()
{
$userCompany = Auth::user()->company_id;
$leavetypes = HrLeaveType::where('company_id', $userCompany)->get();
return view('hr.leave_types.index')->with('leavetypes', $leavetypes);
}
ТАБЛИЦЫ:
CREATE TABLE `hr_leave_types` (
`id` int(11) UNSIGNED NOT NULL,
`company_id` int(11) DEFAULT NULL,
`leave_type_name` varchar(100) NOT NULL,
`leave_type_code` varchar(20) DEFAULT NULL,
`description` longtext DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `hr_leave_types`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `hr_leave_types_uniq1` (`company_id`,`leave_type_name`),
ADD UNIQUE KEY `hr_leave_types_uniq2` (`company_id`,`leave_type_code`);
ALTER TABLE `hr_leave_types`
MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT;
CREATE TABLE `hr_leave_type_details` (
`id` int(11) NOT NULL,
`leave_type_id` int(11) NOT NULL,
`company_id` int(11) NOT NULL,
`employment_type_id` int(11) NOT NULL,
`no_of_days` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `hr_leave_type_details`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `hr_leave_type_details_uniq1` (`company_id`,`leave_type_id`,`employment_type_id`);
ALTER TABLE `hr_leave_type_details`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
I am developing a dynamic input field. This involves two tables:
class HrLeaveType extends Model
{
public $timestamps = false;
protected $table = 'hr_leave_types';
protected $primaryKey = 'id';
protected $fillable = [
'company_id',
'leave_type_name',
'number_of_days',
'leave_type_code',
];
public function leavetypedetail()
{
return $this->hasMany('App\Models\Hr\LeaveTypeDetail');
}
}
class HrLeaveTypeDetail extends Model
{
public $timestamps = false;
protected $table = 'hr_leave_type_details';
protected $primaryKey = 'id';
protected $fillable = [
'leave_type_id',
'company_id',
'employment_type_id',
'no_of_days',
];
protected $casts = [
'data' => 'array',
];
public function leavetype()
{
return $this->belongsTo('App\Models\Hr\HrLeaveType');
}
public function employmenttype()
{
return $this->belongsTo('App\Models\Hr\HrEmploymentType');
}
}
view
<tbody>
@foreach($leavetypes as $key => $leavetype)
<td>
{{$leavetype->leave_type_name ?? '' }}
</td>
<td>
{{$leavetype->leave_type_code ?? '' }}
</td>
<td>
{!! Str::words($leavetype->description, 20, ' ...') !!}
</td>
<td>
@foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->employmenttype->employment_type ?? '' }}
</li>
</ul>
@endforeach
</td>
<td>
@foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->employmenttype->employment_type ?? '' }}
</li>
</ul>
@endforeach
</td>
<td>
@foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->no_of_days ?? '' }}
</li>
</ul>
@endforeach
</td>
</tr>
@endforeach
</tbody>
При попытке просмотра блейда индексного представления я получил эту ошибку:
SQLSTATE [42S22]: столбец отсутствует найдено: 1054 Неизвестный столбец 'hr_leave_type_details.hr_leave_type_id' в 'предложении where' (SQL: выберите * из hr_leave_type_details, где hr_leave_type_details.hr_leave_type_id = 1 и hr_leave_type_details.hr_leave_ll * 101
1016 Я удаляю эту часть кода:
<td>
@foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->employmenttype->employment_type ?? '' }}
</li>
</ul>
@endforeach
</td>
<td>
@foreach($leavetype->leavetypedetail as $key => $leavetypedetail)
<ul class="list-unstyled">
<li>
{{$key+1}}. {{$leavetypedetail->no_of_days ?? '' }}
</li>
</ul>
@endforeach
</td>
Ошибка исчезла.
Идентификатор_доставки_в таблице указан в таблице. Я не знаю, откуда он получает hr_leave_type_id от
Как мне решить эту проблему?
Спасибо