0

I filled a column in my database with some information, but is filling all records with all elements in each space of the column, and I would like that each element take a place in the column forming a kind of list. This is the query that I'm using.

$data = DB::table('incidencias')
->join('clientes_incid', 'clientes_incid.rif', '=', 'incidencias.rif')
->select('incidencias.rif')
->distinct()
->get();

DB::table('incidencias')->update(['resultado' => $data]);

I want to store each element of the array in a diferent row but in the same column

Can somebody help me?

Oscar
  • 139
  • 1
  • 13
  • I don't understand what you're looking for. Can you edit your question with an example? – IGP Jan 13 '20 at 18:29
  • each record of the column in the table is filling with all the data from the array, I would like to each element of the array take a record in the column... that's all that I need – Oscar Jan 13 '20 at 18:33

1 Answers1

0

I think you want every index in $data should be save as new record in database. To do that you can do this using loop. Here edited

foreach ($data as $val) {
    $exist=DB::table('incidencias')->where('resultado', $val)->count();
    if($exist ==0){
        DB::table('incidencias')->insert(['resultado' => $val]);
    }
}

As in you query You are using update. To update you need to specify which record you want to update? You should provide some key to find record and update that. In your query you are geting record and updating all records.

Waqas Altaf
  • 392
  • 3
  • 16
  • yes, I want that... I want to save it as a new record in database, I'm using update because with insert each time that I refresh the page all records are recording in database again and I don't want that... I used your code but I got this error Call to undefined method Illuminate\Database\Query\Builder::create() – Oscar Jan 13 '20 at 18:54
  • You can use insert instead of create. – Waqas Altaf Jan 13 '20 at 18:57
  • `create` is not an available method here. Here are the [docs](https://laravel.com/docs/5.8/queries#inserts) for DB inserts in laravel. Have a look at the `updateOrInsert` method, it might better suit your needs. – Brian Thompson Jan 13 '20 at 18:58
  • You are trying to do that, get id's using join and store these id's against same id's in different column? – Waqas Altaf Jan 13 '20 at 19:00
  • no, I want to store each element of the array in a diferent row but in the same column – Oscar Jan 13 '20 at 19:04
  • I am not getting your point. In above comment you are saying that i want to update record as i refresh page it is creating new entry but here you want to add as a new record? – Waqas Altaf Jan 13 '20 at 19:15
  • Other thing you can add record if it is not existing. – Waqas Altaf Jan 13 '20 at 19:16
  • Yes, I want to store... but if in the database It exist the record I dont want to store the record again, that's why I use update – Oscar Jan 13 '20 at 19:20
  • But my problem is that all elements of the array are recorded in each row of my database, and I want to each element of the array take a diferent place in each row of the column – Oscar Jan 13 '20 at 19:22
  • I got this.. ErrorException Object of class stdClass could not be converted to string – Oscar Jan 13 '20 at 19:38
  • https://stackoverflow.com/questions/30260076/object-of-class-stdclass-could-not-be-converted-to-string-laravel – Waqas Altaf Jan 13 '20 at 19:42