Use update method without using Edit, Laravel

Asked

Viewed 562 times

-1

I have a view q shows a user’s access key, in it I need to press a button q will update the access key randomly, the update returns to the view but does not modify the key. How can I fix this?

Controller update:

public function update(User $api)
{
    //Cria chave e veriica a existencia no banco de dados
    $permitted_chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';

    $hash = substr(str_shuffle($permitted_chars), 0, 40);
    $validator = \Validator::make(['hash' => $hash], ['hash' => 'unique:users,api_access_Key']);
    if ($validator->fails()) {
        return $this->randomId();
    }
    //fim

    $api->update([
        'api_access_key' => $hash,
    ]);
    return redirect()->route('user.index');
}

controller index:

    public function index(User $model)
{ 
    $id = Auth::user()->id;
    $userIndex = DB::select("select * from users where id = '$id'");
    return view('apis.API', compact('userIndex'));

}

as I call the method on the button:

<a href="{{ route('api.update', $api) }}" class="btn btn-primary">{{ __('Nova chave') }}</a>
  • Try to give a save after update: $api->update([&#xA; 'api_access_key' => $hash,&#xA; ])->save();

  • return $this->randomId(); What is this? Is it a method? Is he not going there ?

  • Return $this->randomId():, if the key already exists in the database the script is run again through this Return.

2 answers

1


The best way I think using the update and I think the only one is as follows:

public function update($id, Request $request){
         $dados = Model::find($id);

         $dados->dados = $request->dados;

         $dados->save();

         return redirect()->back();
}

0

Check in the model if the "api_access_key" field is in the array $fillable

protected $fillable = [
    'api_access_key'
];

if you don’t have or the array doesn’t exist, create it with the columns you want to change in the create or update methods

  • this array already exists.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.