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

Kam*_*ski 6 database database-connection model eloquent laravel-5

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

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

通过以下方式创建带有控制器和迁移的模型:

php artisan make:model Cars -mcr
Run Code Online (Sandbox Code Playgroud)


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

汽车-模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Cars extends Model
{

}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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');
    }
Run Code Online (Sandbox Code Playgroud)

web.php-路由

Route::resource('/cars', 'CarsController');
Run Code Online (Sandbox Code Playgroud)

移民

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');
    }
}
Run Code Online (Sandbox Code Playgroud)


填写所有字段并单击“ Osadz” =提交后,我收到错误消息:

SQLSTATE [HY000]:常规错误:1364字段'模型'不具有默认值(SQL:插入到carsbrandpriceHourpriceDaypriceWeekpriceMonthpriceYearupdated_atcreated_at)的值(3,3,53,3,35,3,2018- 01-30 09:36:57,2018-01-30 09:36:57))



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

小智 3

“模型”

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

或者,如果您不想首先为其赋值,则可以在迁移文件中向该字段添加 ->nullable() 。