syntax error, unexpected identifier enum laravel - php

I am using LARAVEL 9. I am create enum in Enum folder and access in model. But when i am adding data i am getting this error
syntax error, unexpected identifier "GenderEnum"
Here is my code
GenderEnum.php
<?php
namespace App\Enum;
enum GenderEnum:string
{
case MALE = 'male';
case FEMALE = 'Female';
}
AdminSeeder.php
$data = [
'first_name' => 'Rishab',
'last_name' => 'goyal',
'email' => 'RISHABGOYAL#yopmail.com',
'mobile_number' => '123',
'role' => '1',
'gender' => 'male',
'password' => '123',
'profile_photo' => '',
];
Admin::addEdit($data);
Admin.php (Model)
protected $casts = [
'gender' => GenderEnum::class
];

There's nothing wrong with your code even in the namespace. The problem is your environment setup, maybe you are still running PHP 8.0 or below instead of PHP 8.1
Enums is a new syntax introduced in PHP 8.1, and not
supported in older PHP versions.
Parse error: syntax error, unexpected identifier

Please check 'use App\Enum' namespace is imported properly

I had the same problem before.
My solution is to update your PHP version to 8.1.

Related

Laravel DB Facade BadMethodCallException

I'm trying to implement insertOrIgnore method from the Laravel DB Facade,
here's a link to the docs + explanation snippet:
https://laravel.com/docs/5.8/queries#inserts
The insertOrIgnore method will ignore duplicate record errors while
inserting records into the database:
DB::table('users')->insertOrIgnore([
['id' => 1, 'email' => 'taylor#example.com'],
['id' => 2, 'email' => 'dayle#example.com']
]);
And here's the piece of code that produces the error (it works with regular insert())
if ($datetime->format('H:i') >= '05:50' && $datetime->format('H:i') <= '07:10') {
DB::table('attendanceTable')->insertOrIgnore(['user_id' => $request->loggedUserId, 'day' => $datetime, 'shift_id' => $Shifts->id, 'created_at' => $datetime, 'updated_at' => $datetime]);
Here's the error that Laravel's Telescope produces
Call to undefined method Illuminate\Database\Query\Builder::insertOrIgnore()
Can someone point out what i'm doing wrong, or atleast give me a hint? Thanks in advance!
I had the same error, and it turned out to be because I was on laravel version 5.8.32, and insertOrIgnore was added in version 5.8.33.
Running composer update resolved the issue.

configureListFields issue when I try to update a data with editable option

I am actually trying to add the possibility to update a field in a SonataList.
I followed the documentation of symfony => https://symfony.com/doc/3.x/bundles/SonataAdminBundle/reference/action_list.html
And I did this code
->add('status', 'choice', [
'label' => 'Status',
'class' => 'secret',
'editable' => true,
'choices' => [
$inquiry_service::_statusInWaiting => 'En attente',
$inquiry_service::_statusValidated => 'Validé',
$inquiry_service::_statusRejected => 'Rejeté',
The result is that I can select on my list my 3 choices but when I try to update the data I have this following error =>
Catchable Fatal Error: Method WF\Sonata\AdminBundle\Admin\FieldDescription::__toString() must return a string value
Which options I missed ?
After few hours, I found that the FieldDescription object returned 'true' to '$this->getLabel' and It was a sonata issue due to an outdated version.
To solve it without updated my version, I created a template with a choice form.

Laravel before today rule... how to do

For this rule I am getting an error syntax error, unexpected '.', expecting ')'
public static $rules = array(
'first_name' => 'required|alpha-dash',
'last_name' => ' required|alpha-dash',
'media_release' => 'required|boolean',
'birthday' => 'before:' . date('Y-m-d')
);
I can't figure out why this won't work. I'm running Laravel 4.2.12
try this:
'birthday' => 'date_format:Y-m-d|before:today',
bye
Just for future users:
Laravel 5 using form requests:
'birthday' => 'before:today', // Doesn't accept today date
'birthday' => 'before:tomorrow', // Accept today date
You can't use a function when defining class member variables. You'll have to move that part to your constructor:
<?php
class Foo {
public static $rules = array(
'first_name' => 'required|alpha-dash',
'last_name' => ' required|alpha-dash',
'media_release' => 'required|boolean'
);
public function __construct()
{
self::$rules['birthday'] = 'before:' . date('Y-m-d');
}
EDIT:
The above solution may not work in Laravel. You may have to use a "Custom Validator" instead:
http://laravel.com/docs/4.2/validation#custom-validation-rules
UPDATE:
Looks like Laravel 5 introduced a better solution. See the answer by Fernando, below.
I recommend you to add a before and an after statement for birthdays.
'birthday' => date_format:Y-m-d|before:today|after:1900-01-01
You'll avoid entries like 1111-1-1 because no one alive was born before the year 1900.

PHP - cannot create array [duplicate]

This question already has an answer here:
Parse error: syntax error, unexpected '[' with php 5.3 [duplicate]
(1 answer)
Closed 8 years ago.
I downloaded and installed Ampps and now I'm using PHP version 5.3.28. When I try to create array i.e.
$foo = ['bar'];
or
$foo = [];
or
$data = [
'ts' => time(),
'ip' => $_SERVER['REMOTE_ADDR'],
'user_id' => #$auth->id,
'method' => $_SERVER['REQUEST_METHOD'],
'uri' => $_SERVER['SCRIPT_NAME'],
'data' => json_encode([
'get' => $_GET,
'post' => $post,
]),
];
etc. I always got error "Parse error: syntax error, unexpected '[' in ...". I didn't change anything. Where is problem and how can I fix it?
Using the syntax [] requires PHP 5.5.0 5.4 and higher, earlier versions have to define arrays as:
$array = array( /* data */);
So, if you wish to use the syntax as exampled. then plan an upgrade to 5.5, otherwise use the alternative method to define an array -- Array Documentation
Try something more explicit like this:
$dataTest = array
(
"ts" => time(),
"ip" => 7
);
echo $dataTest["ts"];
echo $dataTest["ip"];

Class function as array value

i want to place an class function as array value, but it shows parsing error:
Linter error message - Expecting `']"
Php error message -
ErrorException [ Parsing Error ]: syntax error, unexpected '['
Example
'name' => [
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
...
As you see, i want the label to be the value returned from the "lang" class "get" function.
Array short syntax [i.e. $a = ['a', 'b']] is available from version 5.4 on.
Be sure to have the right PHP version to use it, otherwise you should stick to old array('a', 'b') syntax.
You have a syntax error on key name line.
Try syntax like this:
$array = array(
'name' => 'val',
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
'array' => array(
...
)
);
And be sure that Lang::get('site.general.name') return a value.
Change [ to array( like this:
'name' => array(
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
[] for defining arrays isn't supported in older versions.

Categories