What is $data?
Jun 11, 2021
18
Level 2
Undefined property: Illuminate\Database\MySqlConnection::$name
Undefined property: Illuminate\Database\MySqlConnection::$name
foreach ($data as $user) {
$name = preg_replace("/[^a-zA-Z0-9]/", "", $user->name);
$name = substr($name, 0, 8); //
$new_dbname = $name + $user->id;
}
Does anyone know how to fix this error?
Level 54
@bobdebower if you are trying to join the 2 variables together as strings you need to do
$new_dbname = $name . $user->id;
+ will try and add them together. I think you are getting mixed up with how you would do it in js.
but what line is your error for:
Undefined property: Illuminate\Database\MySqlConnection::$name
also, if you @foreach through your users you will only get one $new_dbname
if you want to create a value for $db_name for each user, use an accessor on your User Model
eg
public function getDatabaseNameAttribute()
{
$name = preg_replace("/[^a-zA-Z0-9]/", "", $this->name);
$name = substr($name, 0, 8); //
return $name . $this->id;
}
and then you can access it as $user->databaseName
see https://laravel.com/docs/8.x/eloquent-mutators#defining-an-accessor
1 like
Please or to participate in this conversation.