Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create TEMPORARY table in laravel

how to create a TEMPORARY table in laravel, insert a record and retrieve hello, I'm trying to create a temp table in laravel and insert a record and retrieve that record from temp table and then drop the table.

But my temp table is not created

DB::raw(“CREATE TEMPORARY TABLE tbl_temp(temp_id VARCHAR(100),tempcolumn1 VARCHAR(100),tempcolumn2 VARCHAR(100),tempcolumn3 VARCHAR(100)) ;
like image 751
Abbas Ahmad Avatar asked Mar 02 '17 12:03

Abbas Ahmad


2 Answers

Try this

// CREATE TEMPORARY TABLE

$productList = DB::insert( DB::raw( "CREATE TEMPORARY TABLE tempproducts") );

// DELETE TEMPORARY TABLE

$dropTable = DB::unprepared( DB::raw( "DROP TEMPORARY TABLE tempproducts" ) );
like image 87
Manish Avatar answered Sep 26 '22 10:09

Manish


I have recently created a temp table( laravel 8) as such:

public function createLocalStoreProductTable(): \Illuminate\Http\JsonResponse
    {
        $tempTable = env("DB_TEMP_STORE_PRODUCT_TABLE_NAME");
        DB::connection('mysql')->statement(
            "CREATE TABLE " . $tempTable . " (
                    `uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
                    `store_uid` int(10) unsigned NOT NULL)
                     PRIMARY KEY (`uid`),
                     KEY `store_uid` (`store_uid`)
                     ) ENGINE=InnoDB AUTO_INCREMENT=3035849 DEFAULT 
                       CHARSET=utf8;"

 return response()->json(array('success' => true), Response::HTTP_OK);

like image 33
Alison Wasserfall Avatar answered Sep 26 '22 10:09

Alison Wasserfall