I am working on laravel 4.1. I want to create MYSQL function with migration but it doesn't work with php artisan migrate
. even if it shows Migrated: 2017_01_10_140002_create_MYSQL_UNAVAILABLE_PRODS_FUNCTION
in the console. and when i copy/past the same creation code in MYQL client, it works and the function is created. and when i run php artisan migrate:rollback
the function is removed.
Any help would be appreciated.
the migration file :
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateMYSQLUNAVAILABLEPRODSFUNCTION extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$available = Constant::PRODUCT_AVAILABLE_TRUE | Constant::PRODUCT_AVAILABLE_STOCK_TRUE;
$sql = 'DROP FUNCTION IF EXISTS UNAVAILABLE_PRODS;
DELIMITER $$
CREATE FUNCTION UNAVAILABLE_PRODS(CONFIG_ID INT, PROD_TYPE INT)
RETURNS TEXT
LANGUAGE SQL
BEGIN
DECLARE RES TEXT;
SET GROUP_CONCAT_MAX_LEN = 2048;
SET RES = (SELECT GROUP_CONCAT(ID SEPARATOR \'|\') FROM PRODUCT_BASE WHERE `PRODUCT_TYPE` & PROD_TYPE AND `PRODUCT_BASE`.`AVAILABLE` <> ' . $available . ');
RETURN CONCAT(CONFIG_ID, \'=(\', RES, \')\');
END;
$$
DELIMITER ;
';
DB::unprepared($sql);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP FUNCTION IF EXISTS UNAVAILABLE_PRODS;');
}
}
I figured out what the problem was, it's the delimiters. it worked after i removed the lignes DELIMITER $$
, $$
and DELIMITER ;
I think the mistake is here \'|\')
this quotes are outside of your sql(there are not green). So php tries to execute them in some way screenshot of your code