Error calling table item in Laravel

Asked

Viewed 62 times

0

I have a View in the Laravel that shows the access key and the secret key of the api user, these two keys are pulled from the database to the View, i want to be able to edit only the access key when a change is needed, but I am having the following error in Views:

Undefined Property: stdClass::$api_secret_Key

Follow the controllers I’m using:

Controller that returns to the API view (shows keys):

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

Controller edit and update:

public function edit(User $user)
{
    return view('users.editApi', compact('user'));
}

public function update(UserRequest $request,  User $user)
{   
    $user->update($request->all());
    return redirect()->route('api.index');
}

how these controllers are called:

Edit:

@foreach($user as $user)
<input class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="api_access_key" id="input-name" type="text" value="{{ $user->api_access_Key }}" readonly="readonly required="true" aria-required="true"/>  
</div>
<a href="{{ route('api.edit', $user->id) }}" class="btn btn-primary">{{ __('Editar') }}</a>
@endforeach

Update:

<form method="post" action="{{ route('api.update', $user) }}" autocomplete="off">
  • 3

    The problem seems to occur to me in this passage value="{{ $user->api_access_Key }}", $user has this property api_access_Key ?, note that the K key is in capital letters

  • Please make the error and copy the full text. I want to know if the error is in the view, model or controller.

  • $id = Auth::user()->id; $user = DB::select("select * from users where id = '$id'"); these two lines are unnecessary, could use so $user = Auth::user() ...

1 answer

0


Correct ways to use your codes:

use Auth;
use App\User;
public function index()
{ 
    $id = Auth::user()->id;
    $users = DB::table('users')->where('id', $id)->first(); //Dessa forma pega primeiro registro caso queira todo troque por get()
    return view('users.API', compact('users'));
}

public function edit($id)
{
    $user = User::find($id);

    return view('users.editApi', compact('user'));
}

public function update(Request $request)
{   
    //Caso passe $id na rota do update só tirar $request->id e passar o $id que vem da rota.
    $user = User::find($request->id);
    $user->dados = $request->dados; // Exemplo de como usar continue com todos dados
    $user->save();
    return redirect()->route('api.index');
}

Example of foreach:

@foreach($users as $user)
<input class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="api_access_key" id="input-name" type="text" value="{{ $user->api_access_Key }}" readonly="readonly required="true" aria-required="true"/>  
</div>
<a href="{{ route('api.edit', $user->id) }}" class="btn btn-primary">{{ __('Editar') }}</a>
@endforeach

Browser other questions tagged

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