1

I have a small problem with my Controller action. I can't update my "link" in Database, bt dd method work is correctly when I'm try to check data.

Form

<form class="col-lg-push-6" action="/admin/links/{{$link->id}}/update" method="POST">
            @csrf
            <div class="form-group bmd-form-group">
                <label class="bmd-label-floating">New Link Value</label>
                <input type="text" class="form-control" size="100" name="value">
                <button  class="btn btn-primary" type="submit">Update</button>
            </div>
        </form>

Controller

public function update(Request $request, $id)
{

    $this->validate($request, [
        'value' => 'required'
    ]);

    $link=RedirectUrl::AllLinks()->where('id', $id);
    $link->value = $request->input('value');

    



    return redirect()->back()->with('message', 'Link Updated!');
  }

Model

 public function scopeAllLinks($query){

    return $query->get();

}

Route

Route::prefix('admin')->middleware('auth')->group(function(){
Route::get('/', 'Admin\IndexController@index');
Route::get('dashboard', 'Admin\IndexController@index')->name('dashboard');
Route::get('links', 'Admin\LinkController@index')->name('links');
Route::get('links/{id}', 'Admin\LinkController@linkById');
Route::post('links/{id}/update', 'Admin\LinkController@update');

});
3
  • You never save it. Commented Nov 23, 2020 at 13:31
  • @aynber what I'm doing wrong? Commented Nov 23, 2020 at 13:31
  • Read laravel.com/docs/8.x/eloquent#updates : First, you only use where, so $link is a query builder object, not a single model. And you never save it. Commented Nov 23, 2020 at 13:34

1 Answer 1

1

Few things here:

  1. Your scopeAllLinks scope is incorrect, you don't call get inside a scope, instead you return the query builder instance.

  2. You can use find since you're passing in a record id:

    $link = RedirectUrl::find($id);

  3. You never call save or update on the record:

    $link->value = $request->input('value');

    $link->save(); // <--- add this

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.