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

Posted

技术标签:

【中文标题】LARAVEL 5.5 - 使用 Eloquent 模型将数据从 FORM 插入数据库【英文标题】:LARAVEL 5.5 - Insert Data into Database from FORM with Eloquent Model 【发布时间】:2018-07-09 03:56:09 【问题描述】:

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

到目前为止,我所做的只是:

使用控制器和迁移创建模型:

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]:一般错误:1364 字段“模型”没有 默认值 (SQL: 插入cars (brand, priceHour, priceDay, priceWeek, priceMonth, priceYear, updated_at, created_at) 值 (3, 3, 53, 3, 35, 3, 2018-01-30 09:36:57, 2018-01-30 09:36:57))

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

【问题讨论】:

【参考方案1】:

“模型”

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

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

【讨论】:

是的,如果你有,问题就出在字段上。查看您正在发送到数据库的查询-“插入汽车(品牌、价格小时、价格日、价格周、价格月、价格年、更新的_at、创建的_at”。您根本不尝试添加“模型”字段。这是问题 $cars->brand = $request->input('brand'); $cars->brand = $request->input('model'); $cars->brand = $request->input('type'); $cars->brand = $request->input('doors'); 你使用了 $cars->brand 4 次,将 $cars->brand 改为 $cars->model【参考方案2】:

谢谢你们,你们太棒了!

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

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

不能为NULL

然后我“修复”模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cars extends Model

    protected $fillable = [

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

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

【讨论】:

【参考方案3】:

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

<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',
    ......
]);

【讨论】:

【参考方案4】:

这里QuartUpdatManufacturs是模型名,直接插入值。$request有一些值。

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

【讨论】:

以上是关于LARAVEL 5.5 - 使用 Eloquent 模型将数据从 FORM 插入数据库的主要内容,如果未能解决你的问题,请参考以下文章

未定义的属性:Illuminate\Database\Eloquent\Relations\BelongsTo::$status。 Laravel 5.5 关系

Laravel 会话立即过期(Laravel 5.5)

laravel 5.5 elasticsearch/elasticsearch 插件安装及使用

[ Laravel 5.5 文档 ] 数据库操作 —— 在 Laravel 中轻松实现分页功能

Laravel 5.5 检索嵌套关系中的第一条记录

如何在 Laravel 5.5 中实现多个多对多关系?