3

I am create complaint For that while inserting department for their complaint I am using pluck() method to retrieve data from department table and display complaint in select dropdown as array but the problem is it is not working as it says

Array to string conversion (View: C:\xampp\htdocs\test\resources\views\complaint\create.blade.php)

ComplaintController

 $department = Department::pluck('name','id')->all();

    return view('complaint.create',compact('department'));

create.blade.php

<strong>Department : </strong>
{!! Form::select('dep_id',$department,null,['class'=>'form-control']) !!}

Please help!

8
  • Department::pluck('name','id'); try this Commented Nov 27, 2017 at 9:33
  • this same error "Array to string conversion" Commented Nov 27, 2017 at 9:35
  • $department->toArray() use this in your view Commented Nov 27, 2017 at 9:37
  • array:2 [▼ 1 => "IT" 2 => "HK" ] Commented Nov 27, 2017 at 9:38
  • Check this question : stackoverflow.com/q/29508297/2815635 Commented Nov 27, 2017 at 9:41

2 Answers 2

1

If it is not working try this:-

$department = Department::select('id','name')->get();
return view('complaint.create')->with(compact('department'));

Now your view like this:-

<strong>Department : </strong>
<select class="form-control" name="any-name">
@foreach($department as $dept)
 <option value="{{$dept->id}}">{{$dept->name}}</option>
@endforeach

Hope it helps!

Sign up to request clarification or add additional context in comments.

4 Comments

Department table having data and echo all data see its coming or not?
this data from dd($department) array:2 [▼ 1 => "IT" 2 => "HK" ]
Now try to compact on that blade file see my code how i compact
-1
{!! Form::select('dep_id',$department,old('dep_id'),['class'=>'form-control', 'placeholder'=>'Select Any name']) !!}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.