Home > Enterprise >  Laravel 8 form select option dropdown problem
Laravel 8 form select option dropdown problem

Time:10-17

Am having problem here I have two tables

Department table with fields id dept_code dept_name

I also have Persons table with fields

id  Persons_names Dept_name   Position_held

I have a data entry form to enter data to the Persons_table the problem am having is I want to create select option to get Dept_name From Department_table but am always getting undefined value error.

this is my form

   {!! Form::open(['url' => 'persons_form/store']) !!}
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-6">
  {{Form::label('FullNames', 'Fullnames')}}
  {{Form::text('names', '',['class'=>'form-control','placeholder'=>'Persons Fullnames'])}}
</div>

<div class="form-group col-md-6">
{{Form::label('Department', 'Department')}}


@foreach ($depts as $dept) 
{{
Form::select('department', $dept->department_name, null, ['class'=>'form-control','placeholder' => 'Select Department'])
}}
@endforeach

</div>
<div class="form-group col-md-12">
  {{Form::label('Position', 'Position')}}
  {{Form::text('level', '',['class'=>'form-control','placeholder'=>'Departmental Position'])}}
</div>
</div>
<div>
  {{Form::submit('Save Data',['class'=>'btn btn-outline-primary text-center',])}}
</div>
{!! Form::close() !!}

this is my personsController

     <?php

    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;
    use App\Models\People;
    class PeopleController extends Controller
   {
  
    public function index()
    {
        $depts = DB::table('departments')->select('department_name')->get();

        return view('strategic_plan.people_form', ['depts' => $depts]);
    }

  
    public function create()
    {
    $depts = DB::table('departments')->pluck('department_name');

    
  
    }

   
    public function store(Request $request)
    {
        $this->validate($request,[
            'names'=>'required',
            'department'=>'required',
            'level' =>'required'    
          ]);
      
          $persons =new People;
          $persons->names=$request->input('names');
          $persons->department=$request->input('persons');
          $persons->level=$request->input('level');
          $dept->save();
      
          return redirect('/people')->with('message','Person  Added Succesifully');
    }


  
    public function show()
    {
        $persons = People::all()->sortByDesc("id");
        return view('strategic_plan.people',compact('persons'));

    }

   
    public function edit($id)
    {
    
    }

    public function update(Request $request, $id)
    {
        //
    }

   
    public function destroy($id)
    {
        //
    }
    }

When I try to open the Form am getting

$depts is undefined 

CodePudding user response:

Try using compact, And get @dd in your blade of $depts and share the code.

CodePudding user response:

Use

return view('strategic_plan.people_form', ['depts' => $depts]);

instead of

return view('strategic_plan.people_form', compact('depts');

write it down

  • Related