LARAVEL 5.5 - 使用 Eloquent 模型将数据从 FORM 插入数据库

LARAVEL 5.5 - Insert Data into Database from FORM with Eloquent Model

我在将数据插入数据库时​​遇到问题。

到目前为止我所做的是:

通过控制器和迁移创建模型:

php artisan make:model Cars -mcr


所以,现在我所有的文件都是这样的:

汽车 - 型号

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cars extends Model
{

}

AddCar.blade.php - 查看

<form action="{{ action('CarsController@store') }}" method="post">
    {{ csrf_field() }}
    <input type="text" name="brand" placeholder="Marka">
    <input type="text" name="model" placeholder="Model">
    <input type="text" name="doors" placeholder="Ilość drzwi">
    <input type="text" name="priceHour" placeholder="Cena za godzinę">
    <input type="text" name="priceDay" placeholder="Cena za dzień">
    <input type="text" name="priceWeek" placeholder="Cena za tydzień">
    <input type="text" name="priceMonth" placeholder="Cena za miesiąc">
    <input type="text" name="priceYear" placeholder="Cena za rok">
    <input type="submit" value="Osadź">

</form>

CarsController - 控制器

namespace App\Http\Controllers;

use App\Cars;
use Illuminate\Http\Request;

class CarsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return "test";
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $cars = new Cars;
        $cars->brand = $request->input('brand');
        $cars->brand = $request->input('model');
        $cars->brand = $request->input('type');
        $cars->brand = $request->input('doors');
        $cars->priceHour = $request->input('priceHour');
        $cars->priceDay = $request->input('priceDay');
        $cars->priceWeek = $request->input('priceWeek');
        $cars->priceMonth = $request->input('priceMonth');
        $cars->priceYear = $request->input('priceYear');
        $cars->save();
        return redirect('admin.AddCar');
    }

web.php - 路由

Route::resource('/cars', 'CarsController');

迁移

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCarsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cars', function (Blueprint $table) {
            $table->increments('id');
            $table->string('brand');
            $table->string('model');
            $table->string('type');
            $table->integer('doors');
            $table->string ('priceHour')->nullable();
            $table->string('priceDay')->nullable();
            $table->string('priceWeek')->nullable();
            $table->string('priceMonth')->nullable();
            $table->string('priceYear')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('cars');
    }
}


我在填写所有字段并单击 "Osadz" = 提交后收到的错误是:

SQLSTATE[HY000]: General error: 1364 Field 'model' doesn't have a default value (SQL: insert into cars (brand, priceHour, priceDay, priceWeek, priceMonth, priceYear, updated_at, created_at) values (3, 3, 53, 3, 35, 3, 2018-01-30 09:36:57, 2018-01-30 09:36:57))



我的问题是,我的代码中缺少什么默认值?

"model"

您应该在您的汽车模型中添加字段 'model',可填写字段。

或者如果你一开始不想给它赋值,在迁移文件中,你可以在该字段中添加 ->nullable()。

您需要将 table 中的模型列定义为可为空或在迁移文件中为其指定默认值。错误表明缺少此输入:

<input type="text" name="model" placeholder="Model">

// examples for your migration
$table->string('model')->nullable();
// or
$table->string('model')->default('foo');

或者,如果这些都不起作用,则添加验证以确保将正确的数据提交给您的控制器。

在您的控制器中,您可以像这样定义规则:

$validatedData = $request->validate([
    'model' => 'required|string|max:255',
    'Brand' => 'required|string',
    ......
]);

谢谢你们,你们太棒了!

已经解决了问题,我错过了视图中的字段:

<input type="text" name "type" placeholder="Typ"/>

不能为NULL

并且我 "fix" 模型具有:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cars extends Model
{
    protected $fillable = [

        "brand", "model" , "type" , "doors" , "priceHour" ,
        "priceDay" , "priceWeek" , "priceMonth" , "priceYear"
        ];
}

谢谢你,祝你有美好的一天!

这里的QuartUpdatManufacturs是型号名,里面直接填值。$request有一些值。

$insert_id = Module::insert("QuartUpdatManufacturs", $request);