Koorosh Sadeghi Koorosh Sadeghi - 1 month ago 17
MySQL Question

Laravel query builder returns empty result set

I am trying so hard to understand the reason why my code in

Laravel
Controller and the
SQL
statement I have used return empty result set, while as you will view a screenshot of my
PhpMyAdmin
page, there is a table created and it contains the data just as I have requested the codes in the
HomeController.php
. Here are the codes and pictures of
SQL
and
PhpMyAdmin
page:


HomeController.php :


$user_id = Auth::user()->id;
$user_name = Auth::user()->name;
$table_name = $user_name . '_' . $user_id;
$return_value = 'The user\'s Table is now present : ' . $table_name;

if (Schema::hasTable($table_name))
{
$langs = DB::table($table_name)->get();
return view('home', ['rvalue' => $langs]);
}else{
Schema::connection('mysql')->create($table_name, function ($table){
$value = 'necro';
$table->increments('id');
$table->string('en')->default($value);
$table->string('fr')->default($value);
$table->string('sp')->default($value);
});

$langs = DB::table($table_name)->get();
return view('home', ['rvalue' => $langs]);

}





MySQL commanding to check the database :

enter image description here




PhpMyAdmin :

enter image description here

Would you please help me understand what is wrong in my codes that are placed in
HomeController.php
? I have created the database using phpmyadmin with Utf8_General_ci coding.

Answer

It's returning an empty set because the table actually is empty. The phpadmin screenshot shows the structure for the table, but not it's content.

 Schema::connection('mysql')->create($table_name, function ($table){
            $value = 'necro';
            $table->increments('id');
            $table->string('en')->default($value);
            $table->string('fr')->default($value);
            $table->string('sp')->default($value);
        });

        $langs = DB::table($table_name)->get();
        return view('home', ['rvalue' => $langs]);

In this piece of code, you only create a table, but never add any data to it.