我创建了一个表函数,用于从同一数据库中的另一个表添加项目。表为pekerjaan
和personil
(这两个表是有说服力的关系(一对一)),在表personil
中具有列status
,我将status
的默认值设置为tersedia,在pekerjaan
表中,如果status
为tersedia*,用户可以根据status
从personil
表中添加项目 * 则允许用户添加personil's status
并将其更改为tdkTersedia**。如果用户尝试添加状态为tdkTersedia的项目,则页面将返回一个错误消息,说明该项目不可用。如何实现?
下面是我在AdminController中尝试的方法:
public function update_personilpekerjaan(Request $request, $id){
$personil = Personil::find($id);
if($personil->status != 'tersedia'){
return redirect()->back();
}else{
$pekerjaan = Pekerjaan::find($id);
$pekerjaan->personil_id=$request->personil_id;
$personil->status='tdkTersedia';
$pekerjaan->update();
$personil->save();
return redirect()->route('datapekerjaan')->with('message','Personil Pekerjaan Berhasil diupdate!');
}
}
这是我的表单,用户可以在其中添加personil
表中的项
(...)
<div class="form-group">
<label for="exampleInputPassword1">Personil Pekerjaan</label>
<select name="personil_id" class="form-control select2bs4" style="width: 50%;">
<option value="" disabled selected>Pilih Personil</option>
@foreach ($personil as $personils)
<option value="{{$personils->id}}">
{{$personils->nama}}
</option>
@endforeach
</select>
</div>
(...)
以下是personil
表的表视图
<tbody>
@php $no = 1; @endphp
@foreach ($personil as $personils)
<tr>
<td>{{$no++}}</td>
<td>{{$personils->nama}}</a></td>
<td>{{$personils->lahirtgl}}</td>
<td>{{$personils->pendidikan}}</td>
<td>{{$personils->pengalaman}}</td>
<td>{{$personils->keahlian}}</td>
@if ($personils->status == 'tersedia')
<td class="text-success">Tersedia</td>
@elseif($personils->status == 'tdkTersedia')
<td>{{$personils->pekerjaan}}</td>
@endif
<td>
<a href="/editpersonil/{{$personils->id}}" type="button" class="btn btn-outline-primary">Edit</a>
<a href="#" type="button" class="btn btn-outline-danger delete" data-id="{{$personils->id}}" data-nama="{{$personils->nama}}">Hapus</a>
</td>
</tr>
@endforeach
</tbody>
这就是
Personil.php
class Personil extends Eloquent
{
use HasFactory;
protected $guarded = [];
public $timestamps = false;
public function pekerjaans(){
return $this->hasOne(Pekerjaan::class);
}
}
Pekerjaan.php
class Pekerjaan extends Eloquent
{
use HasFactory;
protected $guarded = [];
protected $dates = ['tanggal'];
public function penyedia(){
return $this->belongsTo(Penyedia::class, 'penyedia_id');
}
public function jeniskerja(){
return $this->belongsTo(Jeniskerja::class, 'jeniskerja_id');
}
public function personil(){
return $this->belongsTo(Personil::class, 'personil_id');
}
public function user(){
return $this->belongsTo(User::class, 'user_id');
}
}
1条答案
按热度按时间rryofs0p1#
在存储方法上只添加if语句
或者只是制定政策。