1

I am trying to insert the data in laravel where I want to insert 4 columns in DB - empid, name, in-time and out-time.

enter image description here

but i am getting syntax error 'unexpected =' for columns separated by '-' e.g: in-time, out-time.

Is there any way to allow inserting values for '-' separated columns in DB using laravel ?

Nikunj V
  • 271
  • 4
  • 18
  • Seems like this is only the text editor error, it shouldn't interfere with your saving method. – zlatan Nov 18 '19 at 13:52
  • check this https://stackoverflow.com/a/26016363/1281242 – AgeValed Nov 18 '19 at 13:56
  • You should never use hyphens in your code. Go for underscores. – Rehmat Nov 18 '19 at 13:57
  • also I STRONG recommend to don't use hyphen on your DB column fields! – AgeValed Nov 18 '19 at 13:57
  • @Rehmat Why never use hyphens/dashes? – brombeer Nov 18 '19 at 14:11
  • @kerbholz to avoid silly things like `$attendata->{'in-time'}`. What if he used underscores and updated the values with a simple `$attendata->in_time` rather using `$attendata->{'in-time'}`? Not only while saving the data, now he will have to do `$attendata->{'in-time'}` everywhere in the code where he needs this property's value. Makes sense? – Rehmat Nov 18 '19 at 16:06

2 Answers2

2

You should use underscores for attributes names but you can still use it like this:

$attendata->{'in-time'} = $col[8];
$attendata->{'out-time'} = $col[9];
N69S
  • 16,110
  • 3
  • 22
  • 36
1

Please remove hyphens(-) from your column fields. As the columns fields are treated as properties of the model which acts a variable. So if talk about the variable they can't have any special characters except (_). You can use in_time or out_time instead of using in-time or out-time.

Utkarsh
  • 635
  • 7
  • 14
  • `-` in properties are allowed though – brombeer Nov 18 '19 at 14:09
  • to use (-) you need to follow different way to assign values just like @N69S 's answer.You can't write it straightly like Nikunj V has written. The best practice is to use underscores – Utkarsh Nov 18 '19 at 14:27
  • I know, "_they can't have any special characters except (`_`)._" makes it sound like `-` isn't allowed, but it is – brombeer Nov 18 '19 at 14:30