我正在尝试在laravel中更新事件表,但是由于我是php和laravel的新手,所以我不知道该怎么做,我已经可以将数据存储在db中,只需要找到一种方法即可。我也有一个错误“此集合实例上不存在属性[id]。
我的刀片文件
<div class="table-responsive">
<table id="default_order" class="table table-striped border display" style="width:100%">
<thead>
<tr>
<th>Event Name</th>
<th>Start Time</th>
<th>End Time</th>
<th>Status</th>
<th>Manage</th>
</tr>
</thead>
<tbody>
@foreach ($event as $ev)
<tr>
<td>
{{$ev->event_name}}
</td>
<td>
{{$ev->event_start}}
</td>
<td>
{{$ev->event_finish}}
</td>
<td>
{{$ev->event_status}}
</td>
<td>
<button type="button" class="btn btn-info" data-
toggle="modal" data-target="#eventModal{{$event->id}}"
data-whatever="{{$event->id}}"> View</button>
<div class="modal fade" id="eventModal{{$event->id}}"
tabindex="-1" role="dialog" aria-
labelledby="eventModalLabel{{$event->id}}">
<div class="modal-dialog modal modal-dialog-centered"
role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="eventModalLabel{{$event-
>id}}">Event Details:</h4>
<button type="button" class="close" data-
dismiss="modal" aria-label="Close"><span aria-
hidden="true">×</span></button>
</div>
<div class="modal-body">
<form method="POST" action="{{ route('event.update',
$event->id) }}">
@method('PATCH')
@csrf
<div class="row">
<div class="col-md-5">
<div class="form-group">
<label for="event-name" class="control-
label">Event Name:</label>
<input type="text" class="form-control"
name="event_name" value="{{$event-
>event_name}}">
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<label for="event-start" class="control-label">Start
Time:</label>
<input type="text" class="form-control"
name="event_start" value="{{$event-
>event_start}}">
</div>
</div>
</div>
<div class="row">
<div class="col-md-5">
<div class="form-group">
<label for="event-finish" class="control-label">End Time:
</label>
<input type="email" class="form-control"
name="event_finish" value="{{$event->event_finish}}">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-
dismiss="modal">Close</button>
<input type="submit" class="btn btn-success" value="Edit">
</div>
</form>
</div>
</div>
</div>
</td>
</tr>
@endforeach
此行我有问题
<button type="button" class="btn btn-info" data-
toggle="modal" data-target="#eventModal{{$event->id}}"
data-whatever="{{$event->id}}"> View</button>
因为它输出错误“此集合实例上不存在属性[id]。”
迁移表
public function up()
{
Schema::create('events', function (Blueprint $table) {
$table->increments('id');
$table->string('event_name');
$table->string('event_desc')->nullable();
//0 = Ongoing; 1 = Upcoming; 2 = Finished;
$table->integer('event_status')->nullable();
$table->dateTime('event_start');
$table->dateTime('event_finish');
$table->integer('evcat_id')->unsigned();
$table->integer('user_id')->unsigned();
});
Schema::table('events', function($table) {
$table->foreign('evcat_id')->references('id')->on('event_categories')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
EventController更新功能
public function update(Request $request, $id)
{
$validator = $request->validate([
'event_name' => 'required',
'event_start' => 'required',
'event_finish' => 'required',
]);
return redirect()->route('event.sched');
/**
$ev = event::find($id);
$ev->event_name = $request->input("event_name");
$ev->save();
Session::flash('success', 'Event successfully updated.');
return redirect()->route('event.sched');
**/
}
事件模型
class Event extends Model
{
//
protected $primaryKey = 'id';
public function eventCategory()
{
return $this->belongsTo('App\EventCategory', 'evcat_id');
}
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
protected $fillable = [
'event_name', 'event_desc', 'event_status', 'event_start', 'event_finish', 'evcat_id', 'user_id',
];
public $timestamps = false;
}
答案 0 :(得分:2)
在您的foreach loop
中,您将变量命名为$ev
,但在您提供的代码段中,您引用了$event
<button type="button" class="btn btn-info" data-
toggle="modal" data-target="#eventModal{{$ev->id}}"
data-whatever="{{**$ev**->id}}"> View</button>
<input type="email" class="form-control"
name="event_finish" value="{{$ev->event_finish}}">
<input type="text" class="form-control"
name="event_start" value="{{$ev-
>event_start}}">
这应该可以解决您遇到的id错误。
至于更新方法(我不使用您的路线),它应该分配模型(使用route model binding),因此您可以执行以下操作:
public function update(Request $request, Event $event)
{
$validator = $request->validate([
'event_name' => 'required',
'event_start' => 'required',
'event_finish' => 'required',
]);
$event->update($request->all());
return redirect()->route('event.sched');
}
还要看看From request validation,使用它您可以在创建和更新方法中使用相同的验证器,而无需在控制器方法中定义验证器。