Usage of Stored procedures/function in laravel
Stored procedure/function as like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
DROP FUNCTION `number_of_employees` CREATE DEFINER=`mydatabase`@`localhost` FUNCTION `number_of_employees`(branch_id_input INT) RETURNS int(11) BEGIN DECLARE number_of_employees_count INT; select count(*) INTO number_of_employees_count from tbl_employee join branch on branch.emp_id=tbl_employee.branch_id where branch_id= branch_id_input; RETURN(number_of_employees_count); END |
Above stored fuction will execute and it will be saved in the database.We can see this in the phpmyadmin how to use the above function in the laravel eloquent model as like below,
1 2 3 4 5 6 |
<?php public static function listEmployees($branchid,$empid) { $employee->addSelect(DB::raw("number_of_employees($empid,$branch_id) as total_employees")); } ?> |