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

frebpwbc  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(319)

我有员工桌和煤气桌。每个员工都会有很多煤气。
我想通过填写详细信息(nama、kp、alamat、hubungan、tel、status)为特定员工添加新成员
在keluargas表中,有id、员工id、nama、kp、tel、alamat、satuts、hubungan油田。。
问题是,当我想将具有值的新行插入到keluargas时,由于staff\u id没有值,因此会出现错误。如何获取要插入到keluargas表中的职员的职员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中的资源控制器。
如何在本功能中添加职员标识,该标识是指选中要添加的职员?
要加载窗体的控制器:

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
9jyewag0

9jyewag01#

缺少变量 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` 你应该跟着你的员工表走。
ix0qys7i

ix0qys7i2#

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 这应该能解决你的问题了

相关问题