我在将数据插入数据库时遇到问题。
到目前为止我所做的是:
通过以下方式创建带有 Controller 和迁移的模型:
php artisan make:model Cars -mcr
所以,现在我所有的文件看起来都是这样的:
汽车 - 模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Cars extends Model
{
}
AddCar.blade.php - View
<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 - Controller
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))
我的问题是,我的代码中缺少什么默认值?
最佳答案
“模型”
您应该在汽车的模型中添加字段“模型”,可填写字段。
或者如果你一开始不想给它赋值,在迁移文件中,你可以在字段中添加 ->nullable()。
关于database - LARAVEL 5.5 - 使用 Eloquent 模型将数据从 FORM 插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48518463/