bobdebower's avatar

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?

0 likes
18 replies
bobdebower's avatar

oh sorry forgot to add it

  public function index(Request $request )
     {
      {
        $data = DB::table('users')
            ->select('id', 'name')
            ->where('db_name', null)
            ->limit(10);
automica's avatar
automica
Best Answer
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
bobdebower's avatar

Thank you! this really helped a lot too! awesome guys!

wingly's avatar

Add a ->get() after the limit

1 like
automica's avatar

@wingly this fixes loading the data but doesn't fix the issue with $new_dbname = $name + $user->id;

1 like
MichalOravec's avatar

@automica This is the result when people don't read the documentation.

By the way $this->name in accessor :)

wingly's avatar

Yep sure but it solves the original error that the OP had didn't check the rest of the code

automica's avatar

@bobdebower the problem you have got though is if the user changes their user name then you would then get a different dbName.

as the users id is unique and unchanging - I would suggest making your db name a hash of this $id.

eg

public function getDatabaseNameAttribute()
{
	return Hash::make($this->id);
}
1 like
bobdebower's avatar

Hey yes, because of the validation problems in the future i decided to only use the ID as dbname because it's always unique. i'am just trying to figure out how to implement it all. Thank you helping, helps a lot!

automica's avatar

ok. I would suggest you review your best answer though..

1 like
bobdebower's avatar

One Last question!

i'am using the accessor in my modle

 public function getDatabaseNameAttribute()
 {
 return Hash::make($this->id);
  }

I'am now only selecting the ID. i feel like i'am still doing something because i keep getting the error Illuminate\Database\QueryException: SQLSTATE[HY000] [1049] Unknown database '1' i changed my db_name to '1' user_1 would be better tho. but do you maybe know why it's getting this error? is it maybe because my DB statement is not working?creating a new DB? THankyou again!

 public function index(Request $request )
  {

         DB::table('users')
            ->select('id')
            ->where('db_name', null)
            ->limit(10)->get();

        // IF NOT EXISTS!!

        $db = DB::statement("CREATE DATABASE IF NOT EXISTS ");


        if ($db !== null) {
            DB::table('users')->whereNull('db_name',)->update([
                'db_name' => DB::raw("CONCAT(id)")
            ]);
        }

    }
}
automica's avatar

perhaps don't bother with the hash for now

 public function getDatabaseNameAttribute()
 {
  return 'user_' . $this->id;
 }

not sure what you are doing here though?

 DB::table('users')
            ->where('db_name', null)
            ->limit(10)->get();

        // IF NOT EXISTS!!

        $db = DB::statement("CREATE DATABASE IF NOT EXISTS ");


        if ($db !== null) {
            DB::table('users')->whereNull('db_name',)->update([
                'db_name' => DB::raw("CONCAT(id)")
            ]);
        }

you have an array of users so you should iterate through them.

public function handle()
{
    $users = DB::table('users')
        ->select('id')
        ->where('db_name', null)
        ->limit(10)->get();

    foreach ($users as $user) {

        $dbName = $user->databaseName;

        $db = DB::statement("CREATE DATABASE IF NOT EXISTS $dbName");
        if ($db !== null) {
            $user->update(['db_name' => $dbName]);
        }
    }
}

I've also added that answer to https://laracasts.com/discuss/channels/laravel/how-to-debug-on-command-line-using-php-artisan-schedulerun

bobdebower's avatar

Thank you so much waw! almost there!

i keep getting this error

ErrorException: Undefined property: stdClass::$databaseName in file

at this line

$dbName = $user->databaseName;

I don't know why it's showing a stdClass with a Viriable.

Thanks again!

MichalOravec's avatar

@bobdebower You have to use User model instead DB facade, if you want to use an accessor.

So instead this

 $users = DB::table('users')
    ->whereNull('db_name')
    ->limit(10)
    ->get();

use

 $users = User::whereNull('db_name')
    ->limit(10)
    ->get();
1 like
bobdebower's avatar

YESSS! Thank you! i have been working on this forever. The final final thing is

$db = DB::statement("CREATE DATABASE IF NOT EXISTS $dbName");

the DB::statement doesn't create a database. i thought it would also create the databases in my phpmyadmin without doing it myself

Please or to participate in this conversation.