Object of class Collection could not be converted to int - php

I didn't get the select option from controller
it's my Edit page
{!! Form::select('channel_id',[''=>'Select A Channel'] + $channels,null,['class'=>'form-control']) !!}
Below is my Controller
public function edit($id)
{
$channels = Channel::all() ;
$d = Discussion::findOrFail($id);
return view('discussion.edit',compact('channels','d'));
}

Form::select is only meant to take a an array of key value pairs for setting the options.
What you can do is use the pluck() method:
[''=>'Select A Channel'] + $channels->pluck('name', 'id')->toArray()
or
$channels->pluck('name', 'id')->prepend('Select A Channel', '')->toArray() // I'm not sure if you will need `->toArray()` here
If you're not going to be using the $channels for anything else on the page then you'll be able to use the pluck() in the controller instead:
$channels = Channel::pluck('name', 'id');
Then in your blade file you can do:
[''=>'Select A Channel'] + $channels->toArray()
or
$channels->prepend('Select A Channel', '')->toArray()
You will need to change the name and id placeholders I've used with pluck() to match the column names from your channels table.
Lastly, depending on the behaviour you want from the placeholder for your <select> you can actually set it by doing:
{!! Form::select('channel_id', $channels->pluck('name', 'id'), null, ['class'=>'form-control', 'placeholder' => 'Select A Channel']) !!}
Notice the 'placeholder' => 'Select A Channel'] in the last array:
https://laravelcollective.com/docs/5.3/html#drop-down-lists

Related

Laravel 5.5 correct way for a CRUD form select

So I have this CRUD where I use the same form to create and edit entries.
I need in several form selects, when creating (no data present for that particular field yet) my select to show the placeholder, but when editing, my select to show whatever is stored on database for that particular id filed. So I have:
Controller:
...
public function create()
{
$house = houses::pluck('name', 'id');
//$thisclient = null;
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
return view('prospects.create', compact('house', 'clients', 'reps'));
}
...
public function edit($id)
{
$house = houses::pluck('name', 'id');
//$thisclient = user::whereId($id)->first();
$clients = client::pluck('last_name', 'id');
$reps = user::where('role_id', 5)->orderBy('first_name')->get()->pluck('full_name', 'id');
$prospect = Prospect::findOrFail($id);
return view('prospects.edit', compact('prospect', 'house', 'clients', 'reps'));
}
and my view form:
Working for create:
{!!Form::select('client_id', $clients, null, ['class' => 'form-control', 'placeholder' => 'Please Select'] ) !!}
Working for edit:
{!! Form::select('client_id', $clients, $prospect->client_id, ['class' => 'form-control'] ) !!}
I'm having 2 troubles here, if I have null as my selected field, it won't bring the selected data on edit, if I have $prospect->client_id , it will return a error on create as there's no data yet.
I tried to solve this by creating a variable $thishouse on controller and passing it to view on return view('prospects.create', compact('house', 'thisclient','clients', 'reps')); and view Form::select('client_id', $clients, $thisclient, ['class' => 'form-control'] ) !!} but seems a bit dirty whne having several form selects...
The second trouble is if I leave a placeholder on Edit, it will show the placeholder, not $prospect->client_id itself.
What's the best and simplest way to achieve all of this and use the same form for create and edit?
Thanks
You can use Form::open and Form::model to create and edit. As an example, you can set in your view:
#if(isset($prospect))
{!! Form::model($prospect, ['action' => ['ProspectController#update', $prospect->id], 'method' => 'patch']) !!}
#else
{!! Form::open(array('action' => 'ProspectController#store', 'method' => 'POST')) !!}
#endif
And then you can create the select like this:
{!! Form::select('client_id', $clients, old('client_id'), ['class' => 'form-control'] ) !!}
So, when you are editing, Laravel will select the attribute from the variable on model function.
And since you are using Laravel 5.5, you could also use #isset instruction.

Laravel get value of Select from Controller (It is just returning 0 for me)

I have run into a roadblock with getting the value of the Select.
Please have a look at my form:
{!! Form::open(array('action' => 'MasterController#datamaker')) !!}
{!! Form::select('Area', array('Science', 'Arts',)); !!}
{!! Form::input('date', 'Year') !!}
{!! Form::submit('Add') !!}
Here is my (simplified) method in the controller:
public function datamaker() {
$input = Request::all();
$Database = new Database;
$Database -> Area = Request::get('Area');
$Database -> Year = $input['Year'];
$Database -> save();
return (Request::get('Area')); <----- This is returning "0" instead of the values declared in the form
}
I have checked other sources and the solutions they have provided have not worked. Whatever I have tried, it always just returns "0" instead of the values declared in the form.
Can anyone point me in the right direction?
Thanks!
The reason why you are getting 0 instead of Science is that Form component, when generating a select, uses array keys as values of available options and array values as string that is displayed in the select. You don't provide array keys, so the keys are 0 for Science and 1 for Arts.
If you want to get Science/Arts as the value from Request::get('Area') you need to pass an array to Form::select() where values and keys are the same strings, e.g.
Form::select('Area', ['Science' => 'Science', 'Arts' => 'Arts']);

Laravel select form default value

I'm having problem on setting the default value of "Please Select" on my select form from laravel. here comes the code.
$user = \App\User::where('role_id','=','3')->orderBy('name', 'ASC')->lists('name','staff_id');
and here's on my blade
{!! Form::select('requestby', $user, Input::old('requestby'), array('class' => 'form-control')) !!}
i have tried to put the array_merge but it seems like it is overwriting the <option> value from staff_id to index value. what should i do now?
array_merge will re-index the array when merging. You can use + for this -
$user = array('' => 'Please Select') + $user;
The indexs will not be changed.

Laravel 5 | Select to variable

I've created a very simple form that has some select values and when you select one of the items from the list and hit submit, it goes to another page that imports a file template to confirm your selection. For some reason on the next page, instead of displaying the item name that was selected, only the row ID of the select value pops up. Is there an additional option that I need to pass through to get the value displayed in the list?
Here's my create.blade.php
<div class="form-group">
{!! Form::label('candy_flavors', 'Candy Flavors:') !!}
{!! Form::select('candy_flavors', array('' => 'Select Flavor') + $candy, null, ['class' => 'form-control'])!!}
</div>
Here's my CandyController.Php
public function create()
{
$candy = Candy::all()->lists('name');
return view('candy.create', compact ('candy'));
}
public function confirm(Requests\PrepareCandyRequest $request, Guard $auth)
{
$candytemplate = $this->compileCandyRequestTemplate($request->all(), $auth);
return view('candy.confirm', compact('candytemplate'));
}
public function compileCandyRequestTemplate($data, Guard $auth)
{
$data = $data + [
'name' => $auth->user()->name,
'email' => $auth->user()->email,
];
return view()->file(app_path('Http/Templates/candytemplate.blade.php'), $data);
}
Here's my candytemplate.blade.php
#extends ('master')
#section ('content')
This is your candy selection: {{ $candy }}
#endsection
In confirm you can just call:
$request->get('candy_flavors')
However it will return blank, because it is attempting to return you the "value", not the "display value" of the select box. And in your case you are passing in an array of empty keys. array('' => 'Please select', '' => 'name 1', etc).
The form builder class uses the keys of the array to fill in the values.
$candy=['apple'=>'apple','banana'=>'banana','mango'=>'mango'];
and now use
<div class="form-group">
{!! Form::label('candy_flavors', 'Candy Flavors:') !!}
{!! Form::select('candy_flavors', array('' => 'Select Flavor') + $candy, null, ['class' => 'form-control'])!!}
</div>
you have to specify key and value otherwise select will set value from 0
So what I decided to do was make the call from my controller, and pass through the label twice. This did the trick
$accountexecutive=AccountExecutive::lists('flavor', 'flavor');
return view('candy.create', compact ('flavors'));

Laravel Select Box

I'm trying to figure out how I can structure my Laravel select menu so that it shows up as this for a final render. Has anyone done such a thing.
The location is a property of the arena object.
<option value="arena_id">Arena Name - Location</option>
{{ Form::select('arena_id', [ null => 'Please Select'] + $arenas, null, ['id' => 'arena_id']) }}
I asssume $arenas comes from something like Arena::where('foo', bar)->get(), but with get() you will get an instance of Illuminate\Database\Eloquent\Collection instead of an actual array which is what you want in Form::select.
So what you need to do is to use lists($field, $key), it will fetch you rows and return it as an array.
$arenas = Arena::where('foo', bar)->lists('name', 'id');
There is a code example here with some comments from users if you want to learn more.
You can use pluck function for getting the results as array
refer https://laravel.com/docs/5.1/collections#method-pluck
$select = $this->all()->pluck('title', 'id');
Then you can use below sample code for creating select box with selected option in blade template
{{ Form::select('name',$select,'selected option id',['class' => 'form-control']) }}

Categories