The error says 'QueryException Array to string conversion'.
Find my code below:
CreateProject.php
<label for="client_id">Select Client*</label>
<select name="client_name_array" id="">
#foreach ($clientsArray as $option)
<option value="{{$option}}">{{$option}}</option>
#endforeach
</select>
controller:
$projects = new Project([
'client_name' => $request->input('client_name_array'),
]);
$projects->save();
return redirect('/projects')->with('status', 'Project updated!');
Please let me know if you need any further info regarding this query. Thanks in advance.
I am not sure but this could be the answer.
In CreateProject.php you have used
#foreach ($clientsArray as $option)
<option value="{{$option}}">{{$option}}</option>
#endforeach
Here $option is also an array which is being passed as a value to the name client_name_array.
When you submit the form, at the controller, $request->input('client_name_array') returns an array. But, the variable client_name seems to be of string type in the database. The array type cannot be changed to string type this way. Hope you got my point.
You can try Attribute Casting eloquent model feature.
In your Project class add
protected $casts = [
'client_name_array' => 'array',
];
So the array data will be automatically transformed to string on save and to array on retrieve.
See Laravel: Attribute Casting
I have added this to previous answer but then decided to make my own.
Also change this:
<select name="client_name_array" id="">
to
<select name="client_name_array[]" id="">
Related
I am trying to store specific value in laravel. Here Select User not required field but in foreach loop hidden field proj_id must has value. Suppose there are 5 rows and I want to store only 2nd and 5th user along with proj_id from hidden field. Here I want to mention that in controller I also kept delete operation so that I can remove previously inserted SAME PROJECT ID related record. For example If I want to store 2nd and 3rd user, that time only 2nd and 3rd users record will remove first then insert. In my code there are logical error but could not find solution. Thanks in advance
<form action="{{ url('/save-project') }}" method="POST">
<tr>
#foreach($projects as $val)
<td>
<input type="hidden" name="proj_id[]" value="{{$val->id}}">
<select name="user_id[]">
<option value="">Select User</option>
<option value="2">x</option>
<option value="4">y</option>
</select>
</td>
#endforeach
</tr>
<input type="submit">
</form>
Controller
$countUserID = count($user_id);
assign_project::where('flag','Y')
->WhereIn('proj_id',$request->proj_id)
->delete();
for($i=0;$i<$countUserID;$i++){
$assign_project = new assign_project();
$assign_project->proj_id = $request->proj_id[$i];
$assign_project->user_id = $request->user_id[$i];
$assign_project->save();
}
JSON fields are gaining more popularity since they became officially supported in MySQL 5.7.8. Even the popular Spatie Laravel Medialibrary package use them, so why shouldn’t we?
To create a JSON field, all we need to do in Laravel migration is use ->json() method:
$table->json('array_data');
Next, you need to tell your model to cast that column from JSON to an array automatically:
class Product extends Model
{
protected $casts = [
'array_data' => 'array'
];
}
This way, you will receive $array_data as array and don’t need to do json_decode() at all.
So, I have an assistant and enfant tables. I made a dropdown list to affect an assistant from DB while the registration of an enfant. I don't know whats wrong I keep getting this error "Type error: Too few arguments to function App\Http\Controllers\EnfantsController::create(), 0 passed and exactly 1 expected". So this is my Enfants Controller :
public function create($nom)
{
$assistants =Assistant::all();
return view('enfants.create' ,['ass'=>$assistants, 'nom'=>$nom]);
}
And this is my create.blade.php :
<select class="form-control select2" id="ass_name" name="ass_name" required>
#foreach($ass as $value)
<option value="{{ $value->nom }}">{{ $value->nom }}</option>
#endforeach
</select>
And, in the option values , I want to have the name of the assistant and then get the id of the selected name and add it to the foreign key "assistant_id" in my enfant table. I hope you get what i want to do, i'm new to Laravel. Any help will be appreciated.
The issue is in your EnfantsController#create method. As you can see its expecting an argument which is a query param that you mention in your routes file. You need to pass value for $nom parameter when you make call to the create method.
using Laravel 5.6.28 my route on web.php file looks like this:
Route::get('kitysoftware/besttrades', 'SectorsController#besttradesview');
My controller with bestrasdeview function:
class SectorsController extends Controller
{
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
foreach ($sectors as $sector) {
echo $sector->SectorName;
}
//passing variable sectors1 to my view (is = $sector)
return view('besttradesview', ['sectors1' => $sector]);
}}
My view Bestradesview.blade.php is:
<form method="GET">
<div class="selectsector">
<Select class="selectsector" name = "sectors">
<option value="{{ sectors1 }}"></option>
<!-- test#2: <option value="{{ $sectors1->sector[] }}"></option> -->
<!-- test#3: <option value="{{ $sectors1->sector }}"></option> -->
</form>
</select>
And i get this error: Use of undefined constant sectors1 - assumed
'sectors1' (this will throw an Error in a future version of PHP)
When testing #2 commented line instead i get another error: Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Cannot use [] for reading
When testing #3 commented line without [] i get another error: Use of undefined constant sectors1 - assumed 'sectors1' (this will throw an Error in a future version of PHP)
Probably it's pretty simple but it's driving me nuts because i can't see why the variable is not passing to the view.
I know it's reading my table because if i remove the last return view call line on my controller, it echoes out all the values from my SectorName column, but i want it on a select dropdown menu.
I have been reading the docs, forums and watching laracast videos without luck. Any insight or just pointing me out to where to learn the proper sintax solution will be appreciatted.
Thanks in advance
It looks like you're trying to populate the select options from the query. If that's the case, your loop is in the wrong place.
Take the loop out of the controller method, and pass the $sectors collection directly to the view.
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
return view('besttradesview', ['sectors1' => $sectors]);
}
Then loop in the view to output the options.
<select class="selectsector" name="sectors">
#foreach($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
</select>
I assumed you wanted some text in the option. If you use the sector name as the option text it will also be used as the option value by default. If you want to use something else for the value, it would be like this, for example:
<option value="{{ $sector->id }}">{{ $sector->SectorName }}</option>
Change,
return view('besttradesview', ['sectors1' => $sector]);
To,
return view('besttradesview', ['sectors1' => $sectors]);
Notice the s in $sectors ($sectors = DB::table('Sectors')->get();) and in the actual view, it should be $sectors1 not just sectors1.
You also need to loop through actual sector:
#foreach ($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
Also, I would definitely suggest creating a model for your Sectors table rather than using the DB:: facade. You should make use of the M in MVC.
I need to pass more than "name" and "id" to select box.
my model has id, name and price.
So I pass it to the view as:
$parts = Part::all()->lists('name','id);
I'd like to have select box options like:
<option value='id' data-price='price'>name</option>
My guess is try to pass array as first parameter in lists() method, but then I don't know is there way to use Form helper.
$parts = Part::all()->lists('["name"=>name, "price"=>price]','id');
Any suggestions?
Try to do something like
$parts = Part::all()->get(array('id', 'name', 'price'))->toArray();
that should give you only the wanted columns in an associative array :)
lists() is not for building a select, it just creates an array out of a collection. You have to pass the full model to the view and then build the select manually:
<select name="part">
#foreach($parts as $part)
<option value="{{ $part->id }}" data-price="{{ $part->price }}">
{{ $part->name }}
</option>
#endforeach
</select>
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']) }}