dimanche 10 septembre 2017

Laravel: insert data in table

i am totally new to laravel n i am trying to build an e-commerce website admin panel for practice. the panel will allow the user to create new categories n set their attributes. E.g user decides to add a category "led" he can add the attribute screen size,color,brand. on submitting the data laravel will create a led table in db with attributes id,timestamp and screen size,color,brand. (i have done this much)

now since each category will have different attributes so i wrote a function getatname($tableName) to get the attribute names of the needed table. now below is my store function

        public function store(Request $request)
                {
                    $cat= $request->categoryName; //to get the name of the table in which the data is about to saved
                    $att = getatname($cat); // to get the column names of that table
                    $product= $request->name; // to get the values stored in the input fields



                    $j=0;
    for($i=3; $i<count($att); $i++){// i=3 because the first 3 columns are id, created-at, updated_at which i want to skip
              $att1[$j]=$att[$i]; // creating new array with skipped attributes
              $j++;
            }

// THIS IS WHAT I TRIED (n its not working)
                    for($i=0; $i<count($att); $i++){ 
                    DB::table($cat)->insert(
                       [$att1[$i] => $product[$i]]
                       );
                     }

the issue i am facing is that the data does get saved but on different rows (writing the loop inside the insert query gives syntax error) As u can see here

i have tried my best to solve the problem but i am totally out of ideas now.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire