2

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.

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Dear friend, Thank you very much to ease my pain which was caused by my lack of knowledge. I used to think that using the ->default($value) would enter the value into the database by itself. I Wish you the best of luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.