1

I am trying to update variables at About Us table, but i am getting this error. What is wrong?

AboutUsController:

   /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\AboutUs $aboutUs
     * @return \Illuminate\Http\Response
     */
    public function edit($aboutUs_id)
    {
        $aboutUs = AboutUs::find($aboutUs_id);
        return view('auth.aboutus.form',compact('aboutUs'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\AboutUs $aboutUs
     * @return \Illuminate\Http\Response
     */
    public function update(AboutUsRequest $request, AboutUs $aboutUs)
    {
        $params = $request->all();

        $aboutUs->update($request->all($params));

        return redirect()->route('aboutUs.index')->with('success','Post updated successfully');
    }

Here is my AboutUsRequest, where i am getting this error:

    public function rules()
    {
        $rules = [
            'title1' => 'required|min:5',
            'body1' => 'required|min:5',
        ];

        if ($this->route()->named('aboutUs.update')) {
            $rules['title1'] .= ',' . $this->route()->parameter('aboutUs')->id;
        }

        return $rules;
    }
}

web.php looks like this:


        Route::group([
            'namespace' => 'Admin',
            'prefix' => 'admin',
        ], function () {
            Route::group(['middleware' => 'is_admin'], function () {
                Route::get('/contacts', 'ContactController@index')->name('emails.contactus');
                Route::get('/orders', 'OrderController@index')->name('home');
                Route::get('/orders/{order}', 'OrderController@show')->name('orders.show');
            });
            Route::resource('blogs', 'BlogController');
            Route::resource('aboutUs', 'AboutUsController');
            

but my route list show me that destroy, update and edit using {aboutU} instead of {aboutUs}:

 App\Http\Middleware\SetLocale                           |
|        | GET|HEAD  | admin/aboutUs/{aboutU}/edit                                         | aboutUs.edit             | App\Http\Controllers\Admin\AboutUsControl
amir
  • 19
  • 4
  • Please, attach the full error message that you're facing. You can take a screenshot of the error page. – Andrii H. Mar 13 '22 at 09:43
  • Also, attach the content of your routes/web.php file. – Andrii H. Mar 13 '22 at 09:43
  • 1
    I'm assuming `$this->route()->parameter('aboutUs')` is a string, throwing the non-object error – DannyXCII Mar 13 '22 at 09:45
  • There might be an issue with an incorrect usage of route model binding feature. Perhaps, the parameter defined in the routes file differs from the parameter name in the update function. – Andrii H. Mar 13 '22 at 09:54
  • I don't know why, but my update,edit and destroy route looks like ` | | | | | App\Http\Middleware\SetLocale | | | PUT|PATCH | admin/aboutUs/{aboutU} | aboutUs.update | App\Http\Controllers\Admin\AboutUsControl ` – amir Mar 13 '22 at 09:58
  • @amir can you add the code (from your routes file) where you actually declare the route please as an edit to your original post. It will help you get answers quicker – DannyXCII Mar 13 '22 at 10:04

1 Answers1

0

I am assuming you are want to update a unique field which is => title1. If that's is right you can set rule for the title1 in your AboutUsRequest like this :

required|unique:table_name,column_name,'.$id,

You can also look at this solution. Laravel: Validation unique on update

Wahyu Kristianto
  • 8,719
  • 6
  • 43
  • 68
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 13 '22 at 12:17