gpt4 book ai didi

php - Laravel 试图添加到数据库

转载 作者:太空宇宙 更新时间:2023-11-03 11:29:24 25 4
gpt4 key购买 nike

我是 Laravel 框架的新手。我正在尝试从表单保存到数据库。

示例:

  • 输入STEAM_ID:
  • 输入权限:

Controller :

public function admins_add()
{
$serveradmins = DB::table('server_admins')->first();
$this->title('Add admins');
$this->pageView('servers::admins_add', ['serveradmins' => $serveradmins]);
DB::table('server_admins')->insert(
array(
$serveradmins->auth = Input::get('steam-id'),
$serveradmins->access = Input::get('access'),
$serveradmins->password = 'nopass',
$serveradmins->flags = 'ce',
$serveradmins->added_by = Input::get('added_by')
)
);
$serveradmins->save();
return Redirect::back();
}

观点:

<div class="page page-servers page-servers-admin-form">
<form method="POST" action="/admin/servers/admins/create" accept-charset="UTF-8" class="form-horizontal">
<div class="form-group ">
<label for="title" class="col-sm-3 control-label">STEAM ID</label>
<div class="col-sm-9">
<input name="steam-id" type="text" id="steam-id">
</div>
</div>

<div class="form-group ">
<label for="access" class="col-sm-3 control-label">Teisės</label>
<div class="col-sm-9">
<input name="access" type="hidden" value="" id="access">
<select id="access" name="access"><option value="" selected="selected">-</option>
<option value="abcdefghijklmnopqrstuv">Owner</option>
<option value="bcdfijmnopqruv">Admin</option>
<option value="3" disabled>PREMIUM</option>
</select>
</div>
</div>

<div class="form-actions">
<button type="submit" name="_form_submit" class="btn btn-default" value="1">
<i class="fas fa-save "></i>
SAVE!
</button>
<button type="button" onclick="document.location.href='/admin/servers/admins'" class="btn btn-default">
<i class="fas fa-times "></i> Cancel
</button>
</div>
</form>
</div>
</section>

还有我的路线:

    ModuleRoute::get('admin/servers/admins/create', 'AdminServersController@admins_add');
ModuleRoute::post('admin/servers/admins/create', 'AdminServersController@admins_add');

如果我的代码很糟糕,请不要生我的气,我是 laravel 的新手,真的很喜欢它!感谢大家的帮助:)

最佳答案

第一:为什么要在get 路由中执行admins_add()?创建一个名为 index() 的函数以便于理解,并告诉该函数返回像这样的 View

public function index() {
$serveradmins = DB::table('server_admins')->first();
return view('viewname')->with('serveradmins', $serveradmins);
}

那么你的admins_add()应该是这样的:

public function admins_add(Request $request)
{
ServerAdmin::create([
'auth' => $request->steam-id,
'access' => $request->access,
'password' => 'nopass',
'flags' => 'ce',
'added_by' => $request->added_by
]);

return Redirect::back();
}

在通过 CLI (php artisan make:model ServerAdmin) 执行生成 ServerAdmin 模型之前,在这个模型中,您将 fillables 更改为

protected $fillable = [
'auth',
'access',
'password',
'flags',
'added_by'
];

并设置

protected $table = 'server_admins';

编辑:正如 N Mahurin 在评论中提到的那样 - 采用此解决方案来练习 Laravel。批量分配存在严重的安全风险。在这里阅读: https://laravel.com/docs/5.6/eloquent#mass-assignment

关于php - Laravel 试图添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51193387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com