gpt4 book ai didi

mysql - 如何在关系表(laravel)中自动插入用户 ID?

转载 作者:行者123 更新时间:2023-11-30 21:42:18 25 4
gpt4 key购买 nike

我有员工表和 keluargas 表。每个员工都会有很多keluargas。

我想通过填写详细信息(nama、kp、alamat、hubungan、电话、状态)将新成员添加到特定员工

在keluargas表中,有id, staff_id, nama, kp, tel, alamat, satuts, hubungan字段..

问题是当我想向 keluargas 插入具有值的新行时,它给出了错误,因为 staff_id 没有值。如何获取要插入keluargas表的员工的staff_id值?

这是在 keluargas 中插入新数据的代码。

   public function store(Request $request)
{


$keluarga = [
'nama' => $request->nama,
'kp' => $request->kp,
'tel' => $request->tel,
'pekerjaan' => $request->pekerjaan,
'alamat' => $request->alamat,
'hubungan' => $request->hubungan,
'status_kahwin' => $request->status_kahwin,
'status_undi' => $request->status_undii,
];

$save = keluargas::insert($keluarga);

if($save)
return redirect('keluarga');
else
return redirect()->back()->withInput();
}

要显示的数据来自这段代码:

   public function index()
{
if (Auth::user()->type == 0) {

$section = Auth::user()->section;

$data['data'] = DB::table('staffs')
->join('keluargas', 'staffs.id', '=', 'keluargas.staff_id')
->join('kahwin', 'keluargas.status_kahwin', '=', 'kahwin.KahwinId')
->join('undi', 'keluargas.status_undi', '=', 'undi.id')
->where('section','=', $section)
->get();


$counter = 1;

if(count($data) > 0)
{
return view('keluarga.index', $data);
}

else
{
return view('keluarga.index', $data);
}

//return view('home');

} else {

$data['data'] = DB::table('staffs')
->join('keluargas', 'staffs.id', '=', 'keluargas.staff_id')
->join('kahwin', 'keluargas.status_kahwin', '=', 'kahwin.KahwinId')
->join('undi', 'keluargas.status_undi', '=', 'undi.id')
->get();

return view('keluarga.index', $data);
}
}

此函数使用了 laravel 中的资源 Controller 。

如何在这个函数中添加staff_id,它指的是选择要添加的员工?

加载表单的 Controller :

 public function create()
{
return view('keluarga.add_keluarga');

}

要添加到数据库中的 Controller :

    public function store(Request $request)
{


$keluarga = [
'nama' => $request->nama,
'kp' => $request->kp,
'staff_id' => $request->$id,
'tel' => $request->tel,
'pekerjaan' => $request->pekerjaan,
'alamat' => $request->alamat,
'hubungan' => $request->hubungan,
'status_kahwin' => $request->status_kahwin,
'status_undi' => $request->status_undii,
];

$save = keluargas::insert($keluarga);

if($save)
return redirect('keluarga');
else
return redirect()->back()->withInput();
}

表格填写 keluarga 数据:

   @extends('layouts.appmenu')

@section('content')

        <div class="container">
<!-- Tajuk Kakitangan -->
<div class="row">
<div class="col-lg-10">
<ol class="breadcrumb">
<li class="active">
<i class="fa fa-user fa-fw"></i> Ahli Keluarga Baru
</li>
</ol>
</div>
</div>

{!! Form::open(['method' => 'POST', 'action' => 'KeluargaController@store']) !!}

<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('nama', 'Nama') }}
</div>
<div class="col-lg-8">
{{ Form::text('nama', '', ['class' => 'form-control', 'placeholder' => '']) }}
<!-- {{ Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title']) }} -->
</div>
</div>

<br>
<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('no_kp', 'No K/P') }}
</div>
<div class="col-lg-8">
{{ Form::text('kp', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('pekerjaan', 'Pekerjaan') }}
</div>
<div class="col-lg-8">
{{ Form::text('pekerjaan', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('tel', 'No Tel') }}
</div>
<div class="col-lg-8">
{{ Form::text('tel', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('hubungan', 'Hubungan') }}
</div>
<div class="col-lg-8">
{{ Form::textarea('hubungan', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('alamat', 'Alamat') }}
</div>
<div class="col-lg-8">
{{ Form::textarea('alamat', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('status', 'Status') }}
</div>
<div class="col-lg-8">
{{ Form::text('status_kahwin', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('status_undi', 'Status Daftar Mengundi') }}
</div>
<div class="col-lg-8">
{{ Form::text('status_undi', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<form action="{{ $id }}" method="POST">
<input type="hidden" name="_method" value="PUT">



</div>

<div class="row">
<div class="col-lg-4">
</div>
<div class="col-lg-4">

{{ Form::submit('Add New', ['class' => 'btn btn-primary']) }}

{!! Form::close() !!}

{!! link_to(URL::previous(),'Cancel', ['class' => 'btn btn-warning']) !!}

</div>
</div>



</div>





@endsection

最佳答案

您缺少变量 staff_id

 public function create()
{
$staffs = Staff::all();
return view('keluarga.add_keluarga', compact('staffs'));

}

和:

 <div class="container">
<!-- Tajuk Kakitangan -->
<div class="row">
<div class="col-lg-10">
<ol class="breadcrumb">
<li class="active">
<i class="fa fa-user fa-fw"></i> Ahli Keluarga Baru
</li>
</ol>
</div>
</div>

{!! Form::open(['method' => 'POST', 'action' => 'KeluargaController@store']) !!}

<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('staff', 'Staff') }}
</div>
<div class="col-lg-8">
<select name="staff_id">
@foreach($staffs as $staff)
<option value="{{$staff->staff_id}}">{{$staff->staff_name}}</option>
@endforeach
</select>
{{ Form::text('nama', '', ['class' => 'form-control', 'placeholder' => '']) }}

</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('nama', 'Nama') }}
</div>
<div class="col-lg-8">
{{ Form::text('nama', '', ['class' => 'form-control', 'placeholder' => '']) }}
<!-- {{ Form::text('title', '', ['class' => 'form-control', 'placeholder' => 'Title']) }} -->
</div>
</div>

<br>
<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('no_kp', 'No K/P') }}
</div>
<div class="col-lg-8">
{{ Form::text('kp', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('pekerjaan', 'Pekerjaan') }}
</div>
<div class="col-lg-8">
{{ Form::text('pekerjaan', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('tel', 'No Tel') }}
</div>
<div class="col-lg-8">
{{ Form::text('tel', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>

<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('hubungan', 'Hubungan') }}
</div>
<div class="col-lg-8">
{{ Form::textarea('hubungan', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('alamat', 'Alamat') }}
</div>
<div class="col-lg-8">
{{ Form::textarea('alamat', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('status', 'Status') }}
</div>
<div class="col-lg-8">
{{ Form::text('status_kahwin', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<div class="form-group">
<div class="row">
<div class="col-lg-1">
{{ Form::label('status_undi', 'Status Daftar Mengundi') }}
</div>
<div class="col-lg-8">
{{ Form::text('status_undi', '', ['class' => 'form-control', 'placeholder' => '']) }}
</div>
</div>
</div>


<form action="{{ $id }}" method="POST">
<input type="hidden" name="_method" value="PUT">



</div>

<div class="row">
<div class="col-lg-4">
</div>
<div class="col-lg-4">

{{ Form::submit('Add New', ['class' => 'btn btn-primary']) }}

{!! Form::close() !!}

{!! link_to(URL::previous(),'Cancel', ['class' => 'btn btn-warning']) !!}

</div>
</div>



</div>





@endsection

$staff->staff_id, $staff->staff_name 你应该按照你的员工表来改变。

关于mysql - 如何在关系表(laravel)中自动插入用户 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51015742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com