0

I can't seem to update my user and school table anymore but was able to update hobby table now.

Keep getting error: implode(): Invalid arguments passed when updating data --> linking back to the question before

Controller:

        //update for user
        public function edit($id){
            $object = user::find($id);

            return view('edit', compact('object'));

        }

        public function update(Request $request, $id){
            $object       = user::find($id);
            $object->Name = $request->input('Name');

            $object->update();

            return redirect('/home');
        }

        //update for Schools table
        public function edit1($id){
           $object2 = school::find($id);

           return view('edit1', compact('object2'));
        }

        public function update1(Request $request, $id){
            $object2 = school::find($id);

            $test           = array();
            $test['School'] = implode(' , ', $request->School);
            $test['SDate']  = implode(' , ', $request->SDate);
            $test['EDate']  = implode(' , ', $request->EDate);

            $object2->update($test);
            return redirect('/home');
        }

        // The error starts here after putting this whole thing in.
        // (I tried putting it into another separate controller but the error
        // still continues)
        public function edit2($id) {
           $object3 = hobby::find($id);

           return view('edit2', compact('object3'));
        }

        public function update2(Request $request, $id){
            $object3 = hobby::find($id);

            $test2                  = array();
            $reading_book           = (array)$request->reading_book;
            $test2['reading_book']  = implode(' , ',$reading_book );
            $computer_game          = (array)$request->computer_game;
            $test2['computer_game'] = implode(' , ',$computer_game );

            $object3->update($test2);

            return redirect('/home');
        }

Hobby model:

   <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;
    use Eloquent;

    class hobby extends Eloquent
    {
        protected $fillable = array('reading_book','computer_game','user_id');

        public function users() {
            return $this->belongsTo('App\user, 'user_id', 'id');
        }
    }

Route: (currently using these for updating)

Route::get('/user/show/{id}/edit', 'HomeController@edit');
Route::put('/user/show/{id}','HomeController@update');


Route::get('/user/show/{id}/edit1', 'HomeController@edit1');
Route::put('/user/show/{id}','HomeController@update1');


Route::get('/user/show/{id}/edit2', 'HomeController@edit2');
Route::put('/user/show/{id}','HomeController@update2');
phaberest
  • 3,140
  • 3
  • 32
  • 40
blastme
  • 391
  • 7
  • 19
  • 37
  • Can you show us the route you're using? – phaberest Oct 20 '17 at 09:10
  • Is there any errors ?? – Maraboc Oct 20 '17 at 09:11
  • Did you get the answer in your previous question? [Keep getting error: implode(): Invalid arguments passed when updating data](https://stackoverflow.com/questions/46845065/keep-getting-error-implode-invalid-arguments-passed-when-updating-data/46845303?noredirect=1#comment80638799_46845303) – ljubadr Oct 20 '17 at 09:11
  • @maraboc There is no error but I can't seem to update any data – blastme Oct 20 '17 at 09:12
  • @ljubadr The answer had helped me abit but one of them told me to create another question as it is too messy there – blastme Oct 20 '17 at 09:13
  • can you provide the error? check `storage/logs/laravel.log` file – ljubadr Oct 20 '17 at 09:19
  • @ljubadr there is no error at all, it just return the way I want but not updating any of my data – blastme Oct 20 '17 at 09:21
  • in `update2()` method, can you add the `dd($request->all());` and post that here? – ljubadr Oct 20 '17 at 09:22
  • I had did that already all the data can be seen and their all in array – blastme Oct 20 '17 at 09:24
  • it's about what's the data itself, not if it's there. We can just guess what data did you sent from your view. Every bit helps to track down the bug – ljubadr Oct 20 '17 at 09:31
  • I got this "array:4 [▼ "_method" => "PUT" "_token" => "SoOsHkbd7fnih9XKskgq8SSgZ8yMnqmxqPlhWL3N" "computer_game" => array:1 [▼ 0 => "LOL" ] "reading_book" => array:1 [▼ 0 => "something" ] ] " – blastme Oct 20 '17 at 09:34

1 Answers1

5

The problem is in your routes :

Route::put('/user/show/{id}','HomeController@update');

Route::put('/user/show/{id}','HomeController@update1');

Route::put('/user/show/{id}','HomeController@update2');

It's the same route for three methods.

Just for testing you can do that :

Route::put('/user/show/{id}','HomeController@update');

Route::put('/user/showupdate1/{id}','HomeController@update1');

Route::put('/user/showupdate2/{id}','HomeController@update2');

And change in the view this will work perfectly :)

Maraboc
  • 10,550
  • 3
  • 37
  • 48
  • But I put it all in different method, so shouldn't it still work? because the first 2 update and update2 works just now. Hold on let me try – blastme Oct 20 '17 at 09:15
  • @blastme How can it know what route you want to access if you call it all in the same way? – phaberest Oct 20 '17 at 09:17
  • It will be a random behavior i think – Maraboc Oct 20 '17 at 09:17
  • 1
    It will trigger only the last one that overwrites the others – phaberest Oct 20 '17 at 09:18
  • Hi, I had tried your method already but the same result, I could only update hobby but not the rest of the data. There is no error being shown – blastme Oct 20 '17 at 09:20
  • @Maraboc I tried your method again but for some reason my page cannot be found. I check the view page " Edit" and I have changed it and I also already changed it inside the edit page "
    "
    – blastme Oct 20 '17 at 09:30
  • because you shoud update even this route in your case `Route::get('/user/show/{id}/edit1', 'HomeController@edit1');` to `Route::get('/user/showupdate1/{id}/edit1', 'HomeController@edit1');` or in the view do this `Edit` without changing the route ! – Maraboc Oct 20 '17 at 09:32
  • Oh ya, your right I forgotten about that *facepalm* Thank you so much for your help !!! – blastme Oct 20 '17 at 09:42
  • Happy to help ;) – Maraboc Oct 20 '17 at 09:44