我有职员桌和keluargas桌。每个员工都会有很多keluargas。
我想通过填写详细信息(nama,kp,alamat,hubungan,tel,状态)将新成员添加到特定人员中
在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中使用资源控制器。
如何添加与要在此功能中添加的选定人员相关的staff_id?
控制器加载表格:
public function create()
{
return view('keluarga.add_keluarga');
}
要添加到数据库中的控制器:
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
答案 0 :(得分:0)
public function create(Staff $staff)
{
$data['staff'] = $staff->pluck('name','id');
return view('keluarga.add_keluarga',$data);
}
以您的形式
员工
{{ Form::select('staff_id', $staff,null, ['class' => 'form-control']) }}
现在在存储方法中,您可以使用$request->staff_id
现在应该可以解决您的问题
答案 1 :(得分:0)
您缺少变量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
,您应该按照工作人员表格进行更改。