P
P
part_os2018-06-01 20:48:27
Laravel
part_os, 2018-06-01 20:48:27

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)

In base firm_id type int is null by default, null is allowed.
How to make a bunch of controller model?
<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>

Controller:
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);
    }

Model:
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

2 answer(s)
Y
Yan-s, 2018-06-01
@part_os

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?

P
pLavrenov, 2018-06-04
@pLavrenov

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);
    }

For Model::update() to work in the model, you need to allow fields for bulk filling
class Auction extends Model
{
    protected $fillable = [
        'protokol',
        'kontrakt',
        'firm_id',
        'status',
    ];
}

Delete the part that is in the Model. There should not be saved and stored other functions for this, you can use traits.
For all fields that can be null in the migration, add ->nullable()
Schema::create('auctions', function($table)
{
  $table->increments('id');
  $table->string('firm_id')->nullable();
});

Then the option that I wrote will work correctly even if firm_id does not come in the data.
In general, I recommend reading the documentation in its entirety. Then there will be an idea of ​​what goodies Laravel has out of the box.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question