我试图为我的用户制作一个简单的联系表单,如果他们有一个订单ID将其放在一个字段中。或者我把它默认为" 0" (假设他们有问题)
所以这是我的代码
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\ContactUS;
class ContactUSController extends Controller
{
public function contactUS()
{
return view('contactUS');
}
public function contactUSPost(Request $request)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required|email',
'message' => 'required'
]);
ContactUS::create($request->all());
return back()->with('success', 'Thanks for contacting us! We will get back shortly.');
}
}
View
<div class="container">
<h1>Contact US Form</h1>
@if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
@endif
{!! Form::open(['route'=>'contactus.store']) !!}
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
{!! Form::label('Name:') !!}
{!! Form::text('name', old('name'), ['class'=>'form-control', 'placeholder'=>'Enter Name']) !!}
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{!! Form::label('Email:') !!}
{!! Form::text('email', old('email'), ['class'=>'form-control', 'placeholder'=>'Enter Email']) !!}
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group">
{!! Form::label('Order ID:') !!}
{!! Form::number('orderId', old('orderId'), ['class'=>'form-control no-spinners', 'placeholder'=>'Do you have and order id?']) !!}
</div>
<div class="form-group {{ $errors->has('message') ? 'has-error' : '' }}">
{!! Form::label('Message:') !!}
{!! Form::textarea('message', old('message'), ['class'=>'form-control', 'placeholder'=>'Enter Message']) !!}
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>
<div class="form-group">
<button class="btn btn-success">Contact US!</button>
</div>
{!! Form::close() !!}
Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ContactUS extends Model
{
public $table = 'contact_us';
public $fillable = ['name','email','message'];
}
Migration
class CreateContactUsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('contact_us', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->text('message');
$table->integer('orderId')->default('0');
$table->boolean('solved')->default(false);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('contact_us');
}
}
所以问题是当我没有在字段OrderId
上放置任何值时,它会采用默认值0
但是,如果我输入任何数字值1 or 2 etc
,它仍会将0
保存在我的数据库中。
知道为什么会出现这种反应?
答案 0 :(得分:5)
允许模型中orderId
字段的质量分配,
public $fillable = ['name','email','message','orderId'];