Hello , I am inserting data through a file excel
and I am implementing the method firstOrCreate
for one of my tables, the problem is that it does not insert the data.
public function model(array $row)
{
//TABLA INSTRUMENTOS
$tipo = tbl_tipo_instrumento::firstOrCreate([
'tipo_nombre' => $row[14]
]);
$marca = tbl_marca::firstOrCreate([
'mar_nombre' => $row[15]
]);
$id = Auth::id(); //trae el id del usuario logeado
return new tbl_instrumentos([
'ins_codigo' => $row[0],
'ins_observacionInicial' => $row[4],
'ins_claseOexactitud' => $row[6],
'ins_nSerie' => $row[1],
'ins_divOescala'=>$row[5],
'ins_claseOexactitud' =>$row[6] ,
'ins_consecutivoInterno' => $row[7],
'ins_observaciones' =>$row[8],
'ins_observacionFinal' => $row[9],
'ins_magnitud' => $row[10],
'ins_modelo' => $row[11],
'ins_codActividad' => $row[12],
'ins_area' => 'null',
'ins_usu_id' => $id,
'ins_tipo_id' => $tipo->tipo_id,
'ins_mar_id' => $marca->mar_id,
]);
}
That is the code that I am using and my model is the following:
class tbl_marca extends Model
{
use Authenticatable;
public $timestamps = false;
public $table="tbl_marca";
protected $primaryKey = 'mar_id';
protected $fillable = [ 'mar_nombre'];
}
If I enter the data manually in phpmyadmin
the search without problems, $row[15] y $row[14]
the names of each one are there. What could be the fault? I'm usinglaravel 6
Thank you
The method is supposed to
firstOrCreate
receive an array of vectors where:first
)create
)So I think you should write it like this:
Where:
$row[14]
that corresponds to the column or propertytipo_nombre