Search code examples
phpoopmodel

Update function in my Model class how to make it can take into account lists (laravel simplified framework PHP)


I have an update function that I would like to use to update several data and I would like to know if it is possible to do it with an array.

function update in my Model Class :

    public static function update($column, $value, $where){
        $request = 'UPDATE '.self::_getTable().' SET '.$column.' = ? WHERE '.$where;
        return Connection::safeQuery($request, [$value]);
    }

And here is the part of code in my Controller, I would like that in my table what is between quotes is the name of the column and the values, the values ​​to be replaced thanks to the update and the siteId makes it apply to the wanted site therefore which serves as where

            $siteId = $website->getId();
            $createTest = [
                'nom'=> $nom,
                'url'=> $url,
                'prix'=> $prix,
                'casino'=> $casino,
                'cbd'=> $cbd,
                'google_news'=> $google_news,
                'categorie_1'=> $categorie_1,
                'categorie_2'=> $categorie_2,
                'categorie_3'=> $categorie_3,
                'TTF'=>$ttf,
                'TF' =>$tf,
                'CF' =>$cf,
                'Backlinks'=>$refDomains,

            ];

            Website::update($createTest, $createTest, $siteId);

when i run my code i get this error :

Warning: Array to string conversion in /home/usersio/code/AP/KicknTool_v2.1/KicknTool/data/Model.php on line 28

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Connection::safeQuery(), 2 passed in /home/usersio/code/AP/KicknTool_v2.1/KicknTool/data/Model.php on line 29 and at least 3 expected in /home/usersio/code/AP/KicknTool_v2.1/KicknTool/data/Connection.php:44 Stack trace: #0 /home/usersio/code/AP/KicknTool_v2.1/KicknTool/data/Model.php(29): Connection::safeQuery() #1 /home/usersio/code/AP/KicknTool_v2.1/KicknTool/control/ParametreController.php(103): Model::update() #2 /home/usersio/code/AP/KicknTool_v2.1/KicknTool/control/ParametreController.php(8): ParametreController::getDataApiOne() #3 /home/usersio/code/AP/KicknTool_v2.1/KicknTool/www/index.php(48): ParametreController::switchAction() #4 /home/usersio/.config/composer/vendor/cpriego/valet-linux/server.php(232): require('...') #5 {main} thrown in /home/usersio/code/AP/KicknTool_v2.1/KicknTool/data/Connection.php on line 44

I would like to be able to send my data thanks to update in my database in order to update them and do it in list given that I have several values ​​to update by row.

Model.php on line 28 :

$request = 'UPDATE '.self::_getTable().' SET '.$column.' = ? WHERE '.$where;

ParametreController.php(103): Model::update() #2 :

Website::update($createTest, $createTest, $siteId);

Solution

  • maybe this can help, you can use a foreach to iterate the keys of the single array you are passing and the matching value of that single key:

    public static function update($columnsValues, $where){
       foreach($columnsValues as $column => $value){
            $request = 'UPDATE '.self::_getTable().' SET '.$column.' = ? WHERE '.$where;
            Connection::safeQuery($request, [$value]);
        } 
    }
    

    I'm not familiar with laravel so i can't really help more, sorry about that.