I'm trying to create a json that collects all the information from the other columns, but the following error happens when I create a new record:
SQLSTATE[HY000]: General error: 1364 Field 'properties' doesn't have a default value
Migration
public function up()
{
Schema::create('hunters', function (Blueprint $table) {
$table->id();
$table->string('name_hunter', 50);
$table->integer('year_hunter');
$table->decimal('height_hunter', 3,2);
$table->decimal('weight_hunter', 5,2);
$table->string('type_hunter', 30);
$table->string('type_nen', 30);
$table->string('type_sangue', 3);
$table->timestamp('register_date')->useCurrent();
$table->timestamp('data_updated')->useCurrent()->useCurrentOnUpdate();
$table->json('properties');
});
}
HunterModel.php
protected $casts = [
'properties' => 'array'
];
HunterController.php
public function create()
{
return view('create');
}
public function store(HunterRequest $request)
{
$validations = $request->validated();
HunterModel::create($validations);
return redirect('/');
}
create.php
<form action="{{ url("create") }}" method="POST">
{{ csrf_field() }}
<div class="form_group">
<div for="nome_hunter">Name:
<input type="text" class="form-control" name="name_hunter" maxlength="50" value="{{ old('name_hunter') }}">
</div>
</div>
<br>
...
<div class="form_group">
<div for="type_blood">Type blood:
<select class="form-control" name="type_blood">
<option {{ old('type_blood') == '' ? 'selected' : ''}} value="">Choose the type blood</option>
<option {{ old('type_blood') == 'A+' ? 'selected' : ''}} value="A+">A+</option>
<option {{ old('type_blood') == 'A-' ? 'selected' : ''}} value="A-">A-</option>
<option {{ old('type_blood') == 'B+' ? 'selected' : ''}} value="B+">B+</option>
<option {{ old('type_blood') == 'B-' ? 'selected' : ''}} value="B-">B-</option>
<option {{ old('type_blood') == 'AB+' ? 'selected' : ''}} value="AB+">AB+</option>
<option {{ old('type_blood') == 'AB-' ? 'selected' : ''}} value="AB-">AB-</option>
<option {{ old('type_blood') == 'O+' ? 'selected' : ''}} value="O+">O+</option>
<option {{ old('type_blood') == 'O-' ? 'selected' : ''}} value="O-">O-</option>
</select>
</div>
</div>
<br>
</form>
What is the necessary change in the form so that it is possible for the JSON to collect the information?
You need a default value for the properties field. Add this to your migration and re-run it (be sure to revert the last migration).
$table->json('properties')->default(json_encode([]));
Related
in my form i have a select and if i edit i want keep select the option i try with isset but i dont know how to use it in a select
this is my select:
<div class="form-group">
<label for="id_raza">Raza</label>
<select class="form-control" id="id_raza" name="id_raza" placeholder="Raza">
#foreach ($razas as $raza)
<option value="{{$raza->id}}">{{ $raza->Nombre}}</option>
#endforeach
</select>
</div>
my controller:
public function edit($id)
{
//
$mascota=Mascota::findOrFail($id);
$razas = Raza::all();
$propietarios = Propietario::all();
return view('mascota.edit', compact('mascota', 'razas', 'propietarios'));
}
my table:
{
Schema::create('mascotas', function (Blueprint $table) {
$table->id();
$table->foreignId('id_raza')
->nullable()
->constrained('razas')
->nullOnDelete()
->cascadeOnUpdate();
$table->timestamps();
});
}
Since mascotas has a id_raza, this value is needed to be able to set the selected option.
#foreach ($razas as $raza)
#if (!empty($moscatas->id_raza) && $moscatas->id_raza == $raza->id)
<option value="{{$raza->id}}" selected>{{ $raza->Nombre}}</option>
#else
<option value="{{$raza->id}}">{{ $raza->Nombre}}</option>
#endif
#endforeach
You can also do it like this,
<option value="{{ $raza->id }}" {{
$raza->id == $moscatas->id_raza ?
'selected' : '' }}>
{{ $raza->Nombre}
</option>
when I tried to visit "Payout Settings" Navigation from my Instructor Panel. Then page shows 500 error. I tried to check why this happened. Then I found this "[previous exception] [object] (ErrorException(code: 0): Illegal string offset 'paytm_enable' at/home/ilanitrc/ilannoor.in/storage/framework/views/313714cd5aacd753115371d69242e5d524fa9876.php:27)
[stacktrace]" error. Why this happened?
Route
Route::get('add/settings', 'InstructorSettingController#instructor')->name('instructor.pay');
Controller
public function instructor()
{
$user = User::where('id', Auth::User()->id)->first();
return view('instructor.settings.pay_settings', compact('user'));
}
View
<div class="row">
<div class="col-md-6">
<label for="type">{{ __('adminstaticword.Type') }}:<sup class="redstar">*</sup></label>
<select name="type" id="paytype" class="form-control js-example-basic-single" required >
<option value="none" selected disabled hidden >{{ __('adminstaticword.ChoosePaymentType') }}</option>
#if($isetting['paytm_enable'] == 1)
<option {{ $user->prefer_pay_method == 'paytm' ? 'selected' : ''}} value="paytm">{{ __('adminstaticword.Paytm') }}</option>
#endif
#if($isetting['paypal_enable'] == 1)
<option {{ $user->prefer_pay_method == 'paypal' ? 'selected' : ''}} value="paypal">{{ __('adminstaticword.Paypal') }}</option>
#endif
#if($isetting['bank_enable'] == 1)
<option {{ $user->prefer_pay_method == 'banktransfer' ? 'selected' : ''}} value="bank">{{ __('adminstaticword.BankTransfer') }}</option>
#endif
</select>
</div>
</div>
That probably means that the variable $isetting in your view is of type string, and you tried to read it as an array (you probably expect it to be an array).
You should anaylze your application, and find out what might set $isetting to be a string. Perhaps somewhere you're missing JSON-array deserialization?
i want to edit user profile which has a country, country field is a select option, i want to get a user's country value in select field, i tried the answer in stack but it didn't work for me knowing there is relationship between user and country belongsTo hasMany.
edit.blade.php
<select id="country" name="country_id" class="form-control" >
<option value="" selected disabled>Select</option>
#foreach($countries as $country)
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
#endforeach
</select>
usersController.php
public function edit($id)
{
$users = Auth::user();
$countries = Country::all();
return view('users.edit')->with([
'users' => $users,
'countries' => $countries
]);
}
Instead of this:
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
I believe you want this:
<option value="{{$country->id}}"{{ $country->id == $users->country_id ? ' selected' : '' }}> {{ $country->name }}</option>
In my create.blade.php I have a dropdown list which is like that.
<div class="form-group">
<label for="company-content">Sexe</label>
<select name="sex" id="" class="form-control">
<option value="">Choice</option>
<option>Women</option>
<option>Man</option>
</select>
</div>
If I choose the option 2, that is to say the item man, in my edit.blade.php I will wish to get the item man.
However, when I want to change the item, my dropdown list is always on woman bij default. It's not practical...
Here is my code concerning the file edit.blade.php, do you have an idea please?
Thank you
<div class="form-group">
<label for="company-content">Sex</label>
<select name="sexe" id="" class="form-control">
<option>Women</option>
<option>Man</option>
</select>
</div>
Edit: 16/03/2019
Visibly, I should do several modifications on my Controller too?
My function edit
public function edit($id)
{
//
$candidats = Candidat::find($id);
$permis = Permis::all();
return view('admin.candidats.edit', compact('candidats', 'permis'));
}
My function update
public function update(Request $request, $id)
{
$request->validate([
'sexe' => 'required|string',
'fk_permis' => 'required'
]);
$candidats = Candidat::find($id);
$candidats->sexe = $request->get('sexe');
$candidats->fk_permis = $request->get('fk_permis');
$candidats->save();
return redirect()->route('candidats.index')
->with('success', 'mise à jour effectuée');
}
Where I should add this line please?
return view('admin.candidats.edit', ['data' => $data]);
Here is my edit.blade.php
<div class="form-group">
<label for="company-content">Sex</label>
<select name="sexe" class="form-control" >
<option value="Man" {{ $data['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $data['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
</div>
So, my problem is in my Controller?
for laravel from controller return view with data like
return view('edit', ['data' => $data]);
<select name="sexe" class="form-control" >
<option value="Man" {{ $data['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $data['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
and for php
<select name="sexe" class="form-control" >
<option value="Man" {{ $_GET['sexe'] == "Man" ? 'selected="selected"' : '' }}>Man</option>
<option value="Women" {{ $_GET['sexe'] == "Women" ? 'selected="selected"' : '' }}>Women</option>
</select>
Assuming you're POSTing your data in a form, and have included values as suggested by #Second2None, you need to tell your form to select the relevant option.
<option<?php if ($_POST['sexe'] == 'man') echo " selected"; ?>>Man</option>
// EDIT
It was a problem in my controller and it specific in my project. My bad, sorry
// END EDIT
I'm pretty lost with something and I need help, this project was not done by me, I just need to do a quick fix.
I have a select in a form :
{{ Form::label('option', 'Type de contrat') }}
{{ Form::select('option[]', $contracts->lists('option', 'id')->toArray(), old('option')[0], ['class' => 'form-control selectpicker']) }}
When my form is not fill correctly, I'm redirect to the form page and I want the select have the previous value. It should be work with the "old".
But it select the first value.
So I tried var_dump(old('option')[0]) which return me "16" (string variable) who is the id of the previous selected and the value of the option who need to be selected.
But if I try (replace the variable by hardcoded value):
{{ Form::label('option', 'Type de contrat') }}
{{ Form::select('option[]', $contracts->lists('option', 'id')->toArray(), 16, ['class' => 'form-control selectpicker']) }}
It's work and I don't understand why ...
I try :
(int)old('option')[0]
But doesn't work
Laravel 5.2
Why it's work with a hardcoded int but now with a variable who return a int number ?
Select generate :
<select class="form-control selectpicker" name="option[]" tabindex="-98">
<option value="13">CDD</option><option value="14">CDI</option>
<option value="15">Stage</option>
<option value="16">Apprentissage</option>
<option value="17">Freelance</option>
</select>
Since it's not a multi-select, you don't need the [] in the name of the input,
You need to use it like this:
{{ Form::select('option', $contracts->lists('option', 'id')->toArray(), old('option'), ['class' => 'form-control selectpicker']) }}
If you have more than one, then you can try it:
view
#for($i = 1; $i <= 2; $i++)
<input type="hidden" name="count_select[]">
<select class="form-control selectpicker" name="option{$i}" tabindex="-98">
<option value="13"{{ (old("option{$i}") == 13) ? ' selected' : '' }}>CDD</option>
<option value="14"{{ (old("option{$i}") == 14) ? ' selected' : '' }}>CDI</option>
<option value="15"{{ (old("option{$i}") == 15) ? ' selected' : '' }}>Stage</option>
<option value="16"{{ (old("option{$i}") == 16) ? ' selected' : '' }}>Apprentissage</option>
<option value="17"{{ (old("option{$i}") == 17) ? ' selected' : '' }}>Freelance</option>
</select>
#endfor
controller
$count_select = $request->input('count_select');
if(count($count_select) > 0){
for($a = 1; $a <= count($count_select); $a++){
$this->validate($request, [
"option{$a}" => 'required'
]);
}
}