我是Laravel的新手,我收到此错误消息"从空值"创建默认对象。我应该更新当前的设施,但我没有做任何好事。有人说,当我点击SAVE按钮(edit_facilities.blade.php)时,它会更新数据库中的数据,但我认为我做错了。有人可以帮我解决如何改进或纠正我的代码的问题。谢谢。 这是我的 CONTROLLER :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Facilities;
use View;
use Redirect;
use Alert;
use Validator;
use Input;
use App\Providers\SweetAlertServiceProvider;
class FacilitiesController extends Controller
{
public $restful = true;
public function update_facilities($id){
$facility = Facilities::find($id);
$facility->facility_name = Input::get('facility_name');
$facility->category = Input::get('category');
$facility->save();
Alert::success('Successfully Updated', 'Congratulations');
return view('hotelier/facilities');
}?>
这是我对 view_facilities.blade.php 的看法:
<h2>Hotel Facilities</h2>
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Facility Name</th>
<th>Category</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="facility_list">
@foreach($facilities as $facility)
<tr id="facility{{$facility->id}}">
<td>{{ $facility->facility_name }}</td>
<td>{{ $facility->category }}</td>
<td><a href="<?php echo 'edit_facilities/'.$facility->id ?>" value="{{$facility->id}}" class="btn btn-info open-modal">Edit</a>
<a id="delete<?php echo $facility->id ?>" href="<?php echo 'delete_facilities/'.$facility->id ?>" class="btn btn-danger" onclick="delete_fac(this)" value="<?php echo $facility->facility_name ?>">Delete</a>
</tr>
@endforeach
</tbody>
</table>
这是我的 edit_facilities.blade.php的视图
<div class="container">
<h2>Edit Hotel Facility</h2>
<div class="col-md-4 form-horizontal">
<form action="/update_facilities/'.$facility->id" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<label>Facility Name</label>
<input type="text" class="form-control" value="{{ $facility['facility_name'] }}" name="facility_name" id="facility_name" autocomplete="off"></input>
<br />
<label>Category</label>
<input type="text" class="form-control" value="{{ $facility['category'] }}" name="category" id="category" autocomplete="off"></input>
<br />
<button type="submit" class="btn btn-info">Save</button>
</form>
</div>
我的路线:
Route::post('/update_facilities/{id}', 'FacilitiesController@update_facilities');
答案 0 :(得分:2)
更改表单标记和update_facilities功能:
<form action="{{ action('FacilitiesController@update_facilities', $facility->id") }} method="POST" enctype="multipart/form-data">
public function update_facilities($id){
$facility = Facilities::find($id);
if(count($facility) > 0){
$facility->facility_name = Input::get('facility_name');
$facility->category = Input::get('category');
$facility->save();
Alert::success('Successfully Updated', 'Congratulations');
return view('hotelier.facilities');
} else {
Alert::error('Facilities not found', 'Error');
return view('hotelier.facilities');
}
}