Answer the question
In order to leave comments, you need to log in
How to pass null from form to model?
Hello everyone, tell me the correct implementation of passing parameters from the form to the controller from the controller to the model and write it to the database. There was a problem with the firm_id field, when writing to the database, the value is obtained by the string 'null' and not by the NULL value and gives an error:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: 'null' for column 'firm_id' at row 1
(SQL: update `tdauction` set `status` = 0, `firm_id` = null where `idauction` = 4025)
<tr>
<td>Выигравшая фирма</td>
<td>
<select class="form-control" name="firm_id" id="firm_id" >
@foreach($firms as $firm)
<option @if($auction->firm_id == $firm->id) {{'selected'}} @endif
value="{{$firm->id}}" >{{$firm->name}}</option>
@endforeach
<option @if( is_null($auction->firm_id)) {{'selected'}} @endif value="null" ></option>
</select>
</td>
</tr>
public function postEditWinAuction(Request $request){
if (isset($_POST['idauction'])) {
$idauction = $_POST['idauction'];
} else {
return null;
}
$value =
[
'idauction' => $idauction,
'protokol' => $_POST['protokol'],
'kontrakt' => $_POST['kontrakt'],
'firm_id' => $_POST['firm_id'],
'status' => $_POST['status']
];
return $id = \App\Auction::editWinAuction($value);
}
public static function editWinAuction($value){
if(is_null($value['idauction'])){
return null;
}
else{
$auction = self::find($value['idauction']);
}
//Сделал пока так что бы не выходила ошибка
if ($value['firm_id']== 'null'){
$firm_id = null;
}
else{
$firm_id = $value['firm_id'];
}
$auction->protokol = $value['protokol'];
$auction->kontrakt = $value['kontrakt'];
$auction->firm_id = $firm_id;
$auction->status = $value['status'];
$auction->save();
$id_name = $auction->primaryKey;
return $id = $auction->$id_name;
}
Answer the question
In order to leave comments, you need to log in
This is normal, can be more compact
UPD
Why are you using $_POST? The $request is passed to the controller and is not used in any way. And where is the validation? Why are you assigning $id before retrurn?
1) it is better to use Request - documentation https://laravel.ru/docs/v5/requests
2) You need to add validation - https://laravel.ru/docs/v5/validation
3) Unlike the previous commentator, I will say that this is completely not the norm.
Replace the code in the controller with this:
public function postEditWinAuction(Request $request){
$this->validate($request, [
'idauction' => 'required|integer', // Если должен быть обязательным
// Тут условия для остальных полей
]);
$values = $request->only([
'protokol',
'kontrakt',
'firm_id',
'status',
]);
return Auction::find($request->idauction)->update($values);
}
class Auction extends Model
{
protected $fillable = [
'protokol',
'kontrakt',
'firm_id',
'status',
];
}
Schema::create('auctions', function($table)
{
$table->increments('id');
$table->string('firm_id')->nullable();
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question