gpt4 book ai didi

php - 通过 Laravel/Eloquent 插入 MySQL 行时出现问题

转载 作者:行者123 更新时间:2023-11-29 09:24:43 26 4
gpt4 key购买 nike

我不太确定如何格式化名称,因为我已经做了一些故障排除,但仍然无法缩小目前问题的范围,以了解为什么我的代码没有插入我的 SQL 数据库中的一行。现在的代码位于 EventController 文件内,如下

    public function create(Request $request){
$this->validate($request, [
'name' => ['required', 'unique:events', 'string', 'max:50'],
'event_desc_short' => ['required', 'string', 'max:120'],
'event_desc' => ['required', 'string'],
'event_free' => ['required', 'boolean'],
'event_low_cost' => ['nullable', 'numeric', 'between:0,9999.99', 'required_if:event_free,false'],
'event_high_cost' => ['nullable', 'numeric', 'between:0,999999.99', 'required_if:event_free,false'],
'event_featured' => ['required', 'boolean'],
'event_feature_type' => ['nullable', 'string', 'required_if:event_featured,true', Rule::in(['yearly', 'monthly', 'weekly'])],
'event_daily_budget' => ['nullable', 'required_if:event_featured,true', 'numeric', 'between:0,999999.99'],
'event_picture_link' => ['nullable'],
'location' => ['nullable'],
]);

$event = new Event;
$event->name = $request->input('name');
$event->event_desc_short = $request->input('event_desc_short');
$event->event_desc = $request->input('event_desc');
$event->event_free = $request->input('event_free');
$event->event_posting_business = Auth::id();
$event->event_low_cost = $request->input('event_low_cost');
$event->event_high_cost = $request->input('event_high_cost');
$event->event_featured = $request->input('event_featured');
$event->event_feature_type = $request->input('event_feature_type');
$event->event_daily_budget = $request->input('event_daily_budget');
$event->location = $request->input('location');
$event->save();

return redirect('/userlanding')->with('success', 'Created Event Successfully!');
}

事件模型如下..

namespace App;

use Illuminate\Database\Eloquent\Model;

class Event extends Model
{
protected $table = 'events';

protected $fillable = [

"name", "event_desc_short" , "event_desc" , "event_free" , "event_low_cost" ,
"event_high_cost" , "event_featured" , "event_feature_type" , "event_daily_budget", "event_picture_link", "location"
];
}

形式如下

{!! Form::open(['action' => 'EventController@create', 'method', 'POST']) !!}
<div class="form-group text-left">
{{Form::label('name', 'Event Name', ['class' => 'text-left'])}}
{{Form::text('name', '', ['class' => 'form-control'])}}
</div>
<div class="form-group text-left">
{{Form::label('event_desc_short', 'Event Description (Short)', ['class' => 'text-left'])}}
{{Form::text('event_desc_short', '', ['class' => 'form-control'])}}
<small class="form-text text-muted">A short description to catch viewer's attention</small>
</div>
<div class="form-group text-left">
{{Form::label('event_desc', 'Event Description (Full)', ['class' => 'text-left'])}}
{{Form::text('event_desc', '', ['class' => 'form-control'])}}
<small class="form-text text-muted">Full description to be viewed on viewing</small>
</div>
<div class="form-group text-left">
{{Form::label('event_free', 'Is this event free to attend?', ['class' => 'text-left'])}}
{{Form::select('event_free', ['true' => 'Yes', 'false' => 'No'], ['class' => 'form-control', 'onchange' => 'openCostPanel()'])}}
</div>
<div id="not-free-event-options">
<div class="row">
<div class="col">
<div class="form-group text-left">
{{Form::label('event_low_cost', 'Minimum Cost to attend Event')}}
{{Form::number('event_low_cost', 'value', ['class' => 'form-control'])}}
</div>
</div>
<div class="col">
<div class="form-group text-left">
{{Form::label('event_high_cost', 'Maximum Cost to attend Event')}}
{{Form::number('event_high_cost', 'value', ['class' => 'form-control'])}}
</div>
</div>
</div>
</div>
<div class="form-group text-left">
{{Form::label('event_featured', 'Would you like to feature this event?')}}
{{Form::select('event_featured', ['false' => 'No', 'true' => 'Yes'], ['class' => 'form-control', 'onchange' => 'openFeaturedPanel()'])}}
</div>
<div id="featured-event-options">
<div class="row">
<div class="col">
<div class="form-group text-left">
{{Form::label('event_feature_type', 'What type of featuring would you like?')}}
{{Form::select('event_feature_type', ['yearly' => 'Yearly', 'monthly' => 'Monthly', 'weekly' => 'Weekly'], ['class' => 'form-control'])}}
</div>
</div>
<div class="col">
<div class="form-group text-left">
{{Form::label('event_daily_budget', 'How much would you like to spend to be featured?')}}
{{Form::number('event_daily_budget', 'value', ['class' => 'form-control'])}}
<small class="form-text text-muted">Bid amount.</small>
</div>
</div>
</div>
</div>
<div class="form-group text-left">
{{Form::label('event_location', 'Event Address', ['class' => 'text-left'])}}
{{Form::text('event_location', '', ['class' => 'form-control'])}}
</div>
{{Form::submit('Create Event', ['class' => 'btn btn-primary'])}}
{!! Form::close() !!}

仔细查看我的代码很长时间并搜索 Stack Overflow 后,我似乎遗漏了一些可能阻止其提交的 super 小步曲。

更奇怪的是,为了调试错误,我添加了一个应该显示错误的错误消息模板,如下所示

@if(count($errors) > 0)
@foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
@endforeach
@endif

@if(session('success'))
<div class="alert alert-success">
{{session('success')}}
</div>
@endif

@if(session('error'))
<div class="alert alert-danger">
{{session('error')}}
</div>
@endif

这包含在带有表单的模板中,提交表单时,它会成功重定向到页面/userlanding,但上面没有任何类型的消息(错误或成功)

我非常困惑,因为代码似乎没有任何问题。

最佳答案

尝试设置SESSION_DRIVER=file以使其正常工作

Seems related

不要忘记清除配置缓存

php artisan config:clear

编辑

验证规则

'event_free' => ['required', 'boolean']//boolean 表示接受 0 OR 1 ('true' OR 'false' 值被视为字符串)

'event_featured' => ['required', 'boolean'] 相同

所以你必须更新:

{{Form::select('event_free', ['1' => 'Yes', '0' => 'No'], ['class' => 'form-control', 'onchange' => 'openCostPanel()'])}}

{{Form::select('event_featured', ['0' => 'No', '1' => 'Yes'], ['class' => 'form-control', 'onchange' => 'openFeaturedPanel()'])}}

关于php - 通过 Laravel/Eloquent 插入 MySQL 行时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59804139/

26 4 0
文章推荐: bash - 单独执行目录中的所有文件
文章推荐: java - 为什么 jboss 从返回结构数组的 Coldfusion webservice 中看到一个空的 List