如何更新记录laravel 5

时间:2016-06-07 01:49:29

标签: php mysql laravel

我的数据库中有相关的表格,并且我能够成功创建包含它的外键的记录,但我无法弄清楚如何编辑和更新该记录

以下是我的商店代码,它从我的表单中获取输入:

    public function store(Request $request){
    $account = new Account;
    $type = new Type;
    $agency = new Agency;
    $client = new Client;

    $agency->name = $request->input('agency_name');
    $agency->save();
    $type->name= $request->input('type_name');
    $type->save();
    $client->name= $request->input('client_name');
    $client->save();

    $account->client()->associate($client);
    $account->type()->associate($type);
    $account->agency()->associate($agency);

    $account->username = $request->username;
    $account->password = $request->password;
    $account->save();

    return redirect('accounts');
    }

上面成功存储了记录,但我不知道如何编辑它,下面在我的编辑字段中提取帐户数据,但是doenst显示account_id字段,type_id和client_id字段它们是空白的:

public function edit(Request $request, $id)
{
    $account=Account::find($id);
    return view('accounts.edit',compact('account'));
}

我的更新代码,这里有什么?

public function update(Request $request, $id)
{
    // ???
    return redirect('accounts');

}

1 个答案:

答案 0 :(得分:1)

  

所以,你应该改变你的商店功能 -

public function store(Request $request)
{
    $account = new Account;

    $type = Type::whereName($request->type_name);
    if (!is_null($type)) {
        $type = $type->first();
    }else{
        $type = Type::create(['name' => $request->type_name]);
    }
    $agency = Agency::whereName($request->agency_name);
    if (!is_null($agency)) {
        $agency = $agency->first();
    }else{
        $agency = Agency::create(['name' => $request->agency_name]);
    }
    $client = Client::whereName($request->client_name);
    if (!is_null($client)) {
        $client = $client->first();
    }else{
        $client = Agency::create(['name' => $request->client_name]);
    }

    $account->client()->associate($client);
    $account->type()->associate($type);
    $account->agency()->associate($agency);

    $account->site_url = $request->site_url;
    $account->login_url = $request->login_url;
    $account->username = $request->username;
    $account->password = $request->password;
    $account->server_ip = $request->server_ip;
    $account->database = $request->database;
    $account->other = $request->other;
    $account->save();
    return redirect('accounts');
}
  

现在,添加帐户并打开修改表单后,您应该可以看到类型代理商&的客户端

至于更新方法,您需要执行以下操作(假设您正在传递代理商类型& 客户端 -

public function update(Request $request, $id)
{
    $account = Account::find($id);
    $type = Type::whereName($request->type['name']);
    if (!is_null($type)) {
        $type = $type->first();
    }else{
        $type = Type::create(['name' => $request->type['name']]);
    }
    $agency = Agency::whereName($request->agency['name']);
    if (!is_null($agency)) {
        $agency = $agency->first();
    }else{
        $agency = Agency::create(['name' => $request->agency['name']]);
    }
    $client = Client::whereName($request->client['name']);
    if (!is_null($client)) {
        $client = $client->first();
    }else{
        $client = Agency::create(['name' => $request->client['name']]);
    }

    $account->client()->associate($client);
    $account->type()->associate($type);
    $account->agency()->associate($agency);

    $account->site_url = $request->site_url;
    $account->login_url = $request->login_url;
    $account->username = $request->username;
    $account->password = $request->password;
    $account->server_ip = $request->server_ip;
    $account->database = $request->database;
    $account->other = $request->other;
    $account->save();

    $request->session()->flash('alert-success', 'Account successfully updated');
    return redirect('accounts');
}