php 如何根据Laravel中的状态更新表格

t1rydlwq  于 2022-12-21  发布在  PHP
关注(0)|答案(1)|浏览(117)

我创建了一个表函数,用于从同一数据库中的另一个表添加项目。表为pekerjaanpersonil(这两个表是有说服力的关系(一对一)),在表personil中具有列status,我将status的默认值设置为tersedia,在pekerjaan表中,如果statustersedia*,用户可以根据statuspersonil表中添加项目 * 则允许用户添加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');
}
}
rryofs0p

rryofs0p1#

在存储方法上只添加if语句

if(auth()->user()->personil()->status == 'whatYouNeed') {
// code goes here
} else {
return abort(403);
}

或者只是制定政策。

相关问题