Laravel尝试添加到数据库

时间:2018-07-05 14:06:20

标签: php mysql laravel

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

示例:

  • 输入STEAM_ID:
  • 输入权限:

控制器:

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的新手,真的很喜欢它!谢谢大家的帮助:)

2 个答案:

答案 0 :(得分:3)

首先:为什么要在admins_add()路线中执行get?创建一个名为index()的函数,以便于理解,并告诉该函数返回视图,如

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(ServerAdmin生成php artisan make:model ServerAdmin模型,并在该模型内部将填充对象更改为

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

并设置

protected $table = 'server_admins';

编辑:正如N Mahurin在评论中提到的-将此解决方案与Laravel一起练习。批量分配具有很大的安全风险。在这里阅读: https://laravel.com/docs/5.6/eloquent#mass-assignment

答案 1 :(得分:0)

您的插入内容已关闭。它应该是键=>值对(键是数据库中的列名)。尝试

DB::table('server_admins')->insert([
    'auth' => Input::get('steam-id'),
    'access' => Input::get('access'),
    'password' => 'nopass',
    'flags' => 'ce',
    'added_by' => Input::get('added_by')
    ]);

有关更多信息,请参见https://laravel.com/docs/5.6/queries#inserts。您也可以走雄辩的路线,然后做:

$serverAdmin = new ServerAdmin();
$serverAdmin->auth = Input::get('steam-id'),
$serverAdmin->access = Input::get('access'),
$serverAdmin->password = 'nopass',
$serverAdmin->flags = 'ce',
$serverAdmin->added_by = Input::get('added_by')
$serverAdmin->save();