1

在 SQLite 数据库中保存对象时遇到问题。在我插入非空值的表单中,请求具有正确的数据,但是当我将对象保存在数据库中时,只有空值。在表格中,我只提供了 1 个示例,但针对各种输入重复了该表格。

客户控制器.php

$data = request()->validate([
    'nome' => 'required|max:40|unique:users',
    'cognome' => 'required|max:40|unique:users',
    'cap' => 'required|min:5',
    'comune' => 'required|max:40',
    'via' => 'required|max:40',
    'civico' => 'required|max:40',
    'telefono' => 'required',
    'email' => 'email|unique:users',
]);
$customer = new Customer();
$customer->nome = $request->input('nome');
$customer->cognome = $request->input('cognome');
$customer->cap = $request->input('cap');
$customer->comune = $request->input('comune');
$customer->via = $request->input('via');
$customer->civico = $request->input('civico');
$customer->telefono = $request->input('telefono');
$customer->email = $request->input('email');

($customer->save());

return view('home');

网页.php

Route::get('customer/create', 'CustomerController@create')->middleware(CheckAdmin::class)->name('customer.create');

Route::post('customer/', 'CustomerController@store')->middleware(CheckAdmin::class)->name('customer.store');

形式

<form method="POST" action="/customer/">
    @csrf
    <div class="form-group row">
        <label for="nome" class="col-md-4 col-form-label text-md-right">{{ __('Nome') }}</label>

        <div class="col-md-6">
            <input id="nome" type="text" class="form-control @error('nome') is-invalid @enderror" name="nome" value="{{ old('nome') }}" required autocomplete="nome" autofocus>

            @error('nome')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>
    ...
</form>

模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    protected $fillable = ['*'];


    protected $table = 'customers';

    public $nome;
    public $cognome;
    public $cap;
    public $comune;
    public $via;
    public $civico;
    public $telefono;
    public $email;

}
4

1 回答 1

0

尝试改变你的模型

class Customer extends Model
{
  protected $fillable = [
    'nome', 'cognome', 'cap', 'comune', 'via', 'civico', 'telefono', 'email',
  ];

  protected $table = 'customers';
}
于 2020-06-18T15:08:55.783 回答