我正在尝试在管理员面板中创建页面,管理员可以从中更改网站设置,例如:site title
,default country
等。
我在没有ID的数据库settings
中创建了表,因为我真的不需要它。它将只会更新一行。没有插入新行。
我在route.php中添加了这一行
Route::get('settings', 'SettingsController@index')->name('admin.settings');
Route::post('settings/update', 'SettingsController@update')->name('admin.settings.update');
这是我在SettingsController.php中的更新功能
public function index()
{
$settings = Settings::all();
return view('backend.settings',compact('settings'));
}
public function update( Request $request)
{
$settings->settings_site_title = $request['site_title'];
$settings->settings_item_per_page = $request['item_per_page'];
$settings->settings_default_country = $request['default_country'];
$settings->settings_max_image_post = $request['max_image_post'];
$settings->save();
return redirect()->route('admin.settings')->with('alert-success', ' Success');
}
和表格
@foreach ($settings as $preferences)
{!! Form::open(['route' => 'admin.settings.update','files' => true , 'class' =>'form-horizontal']) !!}
<div class="form-group">
{!! Form::label('site_title', 'Site Name', array('class'=> 'col-sm-2 control-label')) !!}
<div class="col-sm-10">
{!! Form::text('site_title', $preferences->settings_site_title, ['class'=>'form-control', 'placeholder'=>'Enter site name', 'id'=>'site_title']) !!}
</div>
</div>
... some more inputs like ^
{!! Form::close() !!}
@endforeach
当我提交表单时,我遇到了这个错误:
从空值创建默认对象
更新:型号
class Settings extends Model
{
protected $table = 'settings';
public $timestamps = false;
protected $fillable = [
'settings_site_title',
'settings_item_per_page',
'settings_default_country',
'settings_max_image_post',
];
}
控制器
public function update( Request $request)
{
$settings = Settings::first(); //missed line
$settings->settings_site_title = $request['site_title'];
$settings->settings_item_per_page = $request['item_per_page'];
$settings->settings_default_country = $request['default_country'];
$settings->settings_max_image_post = $request['max_image_post'];
$settings->save();
return redirect()->route('admin.settings')->with('alert-success', ' Success');
}
答案 0 :(得分:2)
您忘记在$settings
方法
update
变量
public function update( Request $request)
{
$settings = Settings::first(); //missed line
$settings->settings_site_title = $request['site_title'];
$settings->settings_item_per_page = $request['item_per_page'];
$settings->settings_default_country = $request['default_country'];
$settings->settings_max_image_post = $request['max_image_post'];
$settings->save();
return redirect()->route('admin.settings')->with('alert-success', ' Success');
}
甚至更好更短
public function update( Request $request)
{
$settings = Settings::first()->update($request->all());
return redirect()->route('admin.settings')->with('alert-success', ' Success');
}
当然,只有当您使用需要批量分配的列填充fillable
模型的Settings
属性时,此功能才有效。
class Settings extends Model
{
// ...
protected $fillable = [
'site_title',
'item_per_page',
'default_country',
'max_image_post',
];
// ...
}
修改
试试这个
public function update( Request $request)
{
$settings = Settings::first(); //missed line
$settings->settings_site_title = $request->input('site_title');
$settings->settings_item_per_page = $request->input('item_per_page');
$settings->settings_default_country = $request->input('default_country');
$settings->settings_max_image_post = $request->input('max_image_post');
$settings->save();
return redirect()->route('admin.settings')->with('alert-success', ' Success');
}
修改
将此添加到您的Settings
型号
protected $primaryKey = null;
public $incrementing = false;
答案 1 :(得分:1)
if(count($settings)>0){
}
在foreach之前使用此if条件。我认为$ setting变量是空的。