SlideShare a Scribd company logo
1 of 22
MySQL triggers in Laravel
(continuare la proceduri stocate)
Vom adauga trigger-ul before insert: BITrigger care va converti la
majuscule toate inregistrarile, inainte de a fi inserate:
php artisan make:migration create_BITrigger_trigger
class CreateBITrigger extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::unprepared('CREATE TRIGGER BITrigger BEFORE INSERT ON flowers FOR EACH ROW
BEGIN
SET NEW.nume=UPPER(NEW.nume);
END');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP TRIGGER IF EXISTS BITrigger');
}
}
Daca dorim sa executam fisierele de migrare separat vom crea care un
director pt fiecare procedura(sau trigger). De ex, cream directorul
BITrigger in /database/migrations si mutam in acesta fisierul de migrari
generat pt procedura insert.
Apoi dam comanda:
php artisan migrate –path=/database/migrations/BITrigger
Astfel se va executa numai un fisier de migrare !!!
Pt a executa toate migrarile vom scrie:
php artisan migrate:refresh
php artisan make:migration create_AUTrigger_trigger
class CreateAUTrigger extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::unprepared('CREATE TRIGGER AUTrigger AFTER UPDATE ON flowers FOR EACH ROW
BEGIN
INSERT INTO flowers_updated(nume, status) VALUES(NEW.nume,"UPDATED");
END');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP TRIGGER IF EXISTS AUTrigger');
}
}
• Creati directorul AUTrigger in /database/migrations si mutati in
acesta fisierul de migrari care genereaza trigger-ul AUTrigger.
• Pentru a executa migrarea screti:
php artisan migrate --path=/database/migrations/AUTrigger
Al treilea trigger-- AfterDelete
php artisan make:migration create_AfterDelete_trigger
class CreateAUTrigger extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::unprepared(‘CREATE TRIGGER after_Delete AFTER DELETE ON flowers FOR EACH ROW
BEGIN
INSERT INTO flower_update(nume, status, EDTIME) VALUES(OLD.nume,'DELETED', NOW());
END;');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP TRIGGER IF EXISTS after_Delete ');
}
}
• Creati directorul ADelete in /database/migrations si mutati in acesta
fisierul de migrari care genereaza trigger-ul AfterDelete.
• Pentru a executa migrarea screti:
php artisan migrate --path=/database/migrations/ADelete
php artisan make:migration create_flower_update_table --create=flower_update
Schema::create('flower_update', function(Blueprint $table)
{
$table->increments('id');
$table->string('nume');
$table->string(‘status');
$table->timestamp(‘EDTIME');
$table->timestamps();
});
• Trigger-ul AuTrigger va fi declansat UpdateFlowers, pe care o vom genera astfel:
php artisan make:migration create_UpdateFlowers_procedure
class CreateUpdateFlowersProcedure extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::unprepared("CREATE PROCEDURE UpdateFlowers(IN var_nume varchar(255))
BEGIN
UPDATE flowers SET pret='10' where nume=var_nume;
END");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP PROCEDURE IF EXISTS UpdateFlowers');
}
}
• Vom afisa datele din flowers_updated cu procedura GetUpdated().
php artisan make:migration create_GetUpdated_procedure
class CreateGetUpdatedProcedure extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::unprepared('CREATE PROCEDURE GetUpdated()
BEGIN
SELECT * FROM flowers_updated;
END');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP PROCEDURE IF EXISTS GetUpdated');
}
}
In controller, vom apela procedura astfel:
DB::select('CALL UpdateFlowers("toporasi")');
$flowers3=DB::select('CALL GetUpdated()');
• Trigger-ul AfterDelete va fi declansat de procedura DeleteFlowers(), pe care o vom genera astfel:
php artisan make:migration create_DeleteFlowers_procedure
class CreateDeleteFlowersProcedure extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::unprepared("CREATE PROCEDURE DeleteFlowers(IN var_nume varchar(255))
BEGIN
DELETE FROM flowers where nume=var_nume;
END");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP PROCEDURE IF EXISTS DeleteFlowers');
}
}
php artisan migrate:refresh
Astfel s-au creat toate procedurile si toti triggerii!!!
• In controller, adaugati apelul procedurii:
DB::select('CALL DeleteFlowers("ghiocei")');
FlowersController::index
<?php namespace AppHttpControllers;
use AppFlower;
use DB;
class FlowersController extends Controller {
public function index(){
DB::insert("CALL InsertFlowers('lalele', 'mici','rosii','100')");
DB::insert("CALL InsertFlowers('trandafiri', 'mari','rosii','200')");
DB::insert("CALL InsertFlowers('toporasi', 'mici','albi','150')");
DB::insert("CALL InsertFlowers('ghiocei', 'mici','albi','10')");
$flowers1=DB::select('CALL GetFlowers()');
DB::select('CALL UpdateFlowers("toporasi")');
DB::select('CALL DeleteFlowers("ghiocei")');
$flowers2=DB::select('CALL GetFlower("trandafiri")');
$flowers3=DB::select('CALL GetUpdated()');
return view('pages.flowers')->with([
'title'=>'Flowers data',
'flowers1'=> $flowers1,
'flowers2'=>$flowers2,
'flowers3'=>$flowers3]);
}
}
?>
Modelul app/Flower.php
<?php namespace App;
use IlluminateDatabaseEloquentModel;
use DB;
class Flower extends Model {
protected $table = 'flowers';
//public $timestamps=false;
}
?>
Routes
<?php
Route::get('/', 'FlowersController@index');
?>
/pages/flowers.blade.php
@extends('pages.master')
@section('content')
<h1>Flowers data</h1>
<table>
<tr>
<th>Nume</th>
<th>Culoare</th>
<th>Marime</th>
<th>Pret</th>
</tr>
@foreach($flowers1 as $flower)
<tr>
<td><?php echo $flower->nume;?></td>
<td><?php echo $flower->culoare;?></td>
<td><?php echo $flower->marime;?></td>
<td><?php echo $flower->pret;?></td> </tr>
@endforeach
</table><br/><br/>
<h1>BEGIN SELECT * FROM flowers WHERE nume="trandafiri"</h1>
<table>
<tr>
<th>Nume</th>
<th>Culoare</th>
<th>Marime</th>
<th>Pret</th>
</tr>
@foreach($flowers2 as $flower)
<tr>
<td><?php echo $flower->nume;?></td>
<td><?php echo $flower->culoare;?></td>
<td><?php echo $flower->marime;?></td>
<td><?php echo $flower->pret;?></td> </tr>
@endforeach
</table>
<br/><br/>
<h1>Updated/Deleted data</h1>
<table>
<tr>
<th>Nume</th>
<th>Status</th>
</tr>
@foreach($flowers3 as $flower)
<tr>
<td><?php echo $flower->nume;?></td>
<td><?php echo $flower->status;?></td>
</tr>
@endforeach
</table>
@endsection
/pages/master.blade.php
<!DOCTYPE html>
<html>
<head><title> <?php echo $title;?> </title></head>
<body bgcolor='lightblue'>
@yield('content')
@yield('footer')
</body>
</html>
17.my sql triggers in laravel

More Related Content

What's hot

TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingBethmi Gunasekara
 
Springboot introduction
Springboot introductionSpringboot introduction
Springboot introductionSagar Verma
 
Meetup angular http client
Meetup angular http clientMeetup angular http client
Meetup angular http clientGaurav Madaan
 
Automated testing using Selenium & NUnit
Automated testing using Selenium & NUnitAutomated testing using Selenium & NUnit
Automated testing using Selenium & NUnitAlfred Jett Grandeza
 
Advanced java programming-contents
Advanced java programming-contentsAdvanced java programming-contents
Advanced java programming-contentsSelf-Employed
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework tola99
 
React state
React  stateReact  state
React stateDucat
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming WebStackAcademy
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++ sandeep54552
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)slire
 
AOT(Ahead Of Time)
AOT(Ahead Of Time)AOT(Ahead Of Time)
AOT(Ahead Of Time)Questpond
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - OperatorsWebStackAcademy
 
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...Edureka!
 
Spring Framework - Spring Security
Spring Framework - Spring SecuritySpring Framework - Spring Security
Spring Framework - Spring SecurityDzmitry Naskou
 

What's hot (20)

TestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit TestingTestNG - The Next Generation of Unit Testing
TestNG - The Next Generation of Unit Testing
 
Springboot introduction
Springboot introductionSpringboot introduction
Springboot introduction
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Meetup angular http client
Meetup angular http clientMeetup angular http client
Meetup angular http client
 
Automated testing using Selenium & NUnit
Automated testing using Selenium & NUnitAutomated testing using Selenium & NUnit
Automated testing using Selenium & NUnit
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Angularjs PPT
Angularjs PPTAngularjs PPT
Angularjs PPT
 
Advanced java programming-contents
Advanced java programming-contentsAdvanced java programming-contents
Advanced java programming-contents
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
React state
React  stateReact  state
React state
 
Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming  Angular - Chapter 2 - TypeScript Programming
Angular - Chapter 2 - TypeScript Programming
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
AOT(Ahead Of Time)
AOT(Ahead Of Time)AOT(Ahead Of Time)
AOT(Ahead Of Time)
 
JavaScript - Chapter 5 - Operators
 JavaScript - Chapter 5 - Operators JavaScript - Chapter 5 - Operators
JavaScript - Chapter 5 - Operators
 
Anjular js
Anjular jsAnjular js
Anjular js
 
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring Framework - Spring Security
Spring Framework - Spring SecuritySpring Framework - Spring Security
Spring Framework - Spring Security
 

Similar to 17.my sql triggers in laravel

«Работа с базами данных с использованием Sequel»
«Работа с базами данных с использованием Sequel»«Работа с базами данных с использованием Sequel»
«Работа с базами данных с использованием Sequel»Olga Lavrentieva
 
Build your own_map_by_yourself
Build your own_map_by_yourselfBuild your own_map_by_yourself
Build your own_map_by_yourselfMarc Huang
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hopeMarcus Ramberg
 
Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentationBrian Hogg
 
Vitta Minicurso Laravel - Hackathon League of Legends
Vitta Minicurso Laravel - Hackathon League of LegendsVitta Minicurso Laravel - Hackathon League of Legends
Vitta Minicurso Laravel - Hackathon League of LegendsFilipe Forattini
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJSSandi Barr
 
Oracle trigger
Oracle triggerOracle trigger
Oracle triggernasrul28
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For BeginnersJonathan Wage
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015Fernando Daciuk
 
Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)
Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)
Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)Sarp Erdag
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gearsdion
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...DroidConTLV
 
Talkaboutlithium
TalkaboutlithiumTalkaboutlithium
Talkaboutlithiumnoppoman722
 

Similar to 17.my sql triggers in laravel (20)

«Работа с базами данных с использованием Sequel»
«Работа с базами данных с использованием Sequel»«Работа с базами данных с использованием Sequel»
«Работа с базами данных с использованием Sequel»
 
Advanced redux
Advanced reduxAdvanced redux
Advanced redux
 
To-Do App With Flutter: Step By Step Guide
To-Do App With Flutter: Step By Step GuideTo-Do App With Flutter: Step By Step Guide
To-Do App With Flutter: Step By Step Guide
 
Play!ng with scala
Play!ng with scalaPlay!ng with scala
Play!ng with scala
 
Build your own_map_by_yourself
Build your own_map_by_yourselfBuild your own_map_by_yourself
Build your own_map_by_yourself
 
Mojolicious - A new hope
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
 
Django quickstart
Django quickstartDjango quickstart
Django quickstart
 
Introduction to backbone presentation
Introduction to backbone presentationIntroduction to backbone presentation
Introduction to backbone presentation
 
Vitta Minicurso Laravel - Hackathon League of Legends
Vitta Minicurso Laravel - Hackathon League of LegendsVitta Minicurso Laravel - Hackathon League of Legends
Vitta Minicurso Laravel - Hackathon League of Legends
 
Angular and The Case for RxJS
Angular and The Case for RxJSAngular and The Case for RxJS
Angular and The Case for RxJS
 
Oracle trigger
Oracle triggerOracle trigger
Oracle trigger
 
Doctrine For Beginners
Doctrine For BeginnersDoctrine For Beginners
Doctrine For Beginners
 
ABCD firebase
ABCD firebaseABCD firebase
ABCD firebase
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
 
Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)
Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)
Hızlı Cocoa Geliştirme (Develop your next cocoa app faster!)
 
Add loop shortcode
Add loop shortcodeAdd loop shortcode
Add loop shortcode
 
Future of Web Apps: Google Gears
Future of Web Apps: Google GearsFuture of Web Apps: Google Gears
Future of Web Apps: Google Gears
 
Migrating data to drupal 8
Migrating data to drupal 8Migrating data to drupal 8
Migrating data to drupal 8
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
Talkaboutlithium
TalkaboutlithiumTalkaboutlithium
Talkaboutlithium
 

More from Razvan Raducanu, PhD (20)

12. edit record
12. edit record12. edit record
12. edit record
 
11. delete record
11. delete record11. delete record
11. delete record
 
10. view one record
10. view one record10. view one record
10. view one record
 
9. add new record
9. add new record9. add new record
9. add new record
 
8. vederea inregistrarilor
8. vederea inregistrarilor8. vederea inregistrarilor
8. vederea inregistrarilor
 
7. copy1
7. copy17. copy1
7. copy1
 
6. hello popescu 2
6. hello popescu 26. hello popescu 2
6. hello popescu 2
 
5. hello popescu
5. hello popescu5. hello popescu
5. hello popescu
 
4. forme in zend framework 3
4. forme in zend framework 34. forme in zend framework 3
4. forme in zend framework 3
 
3. trimiterea datelor la vederi
3. trimiterea datelor la vederi3. trimiterea datelor la vederi
3. trimiterea datelor la vederi
 
2.routing in zend framework 3
2.routing in zend framework 32.routing in zend framework 3
2.routing in zend framework 3
 
1. zend framework intro
1. zend framework intro1. zend framework intro
1. zend framework intro
 
18. images in symfony 4
18. images in symfony 418. images in symfony 4
18. images in symfony 4
 
17. delete data
17. delete data17. delete data
17. delete data
 
16. edit data
16. edit data16. edit data
16. edit data
 
15. view single data
15. view single data15. view single data
15. view single data
 
14. add data in symfony4
14. add data in symfony4 14. add data in symfony4
14. add data in symfony4
 
13. view data
13. view data13. view data
13. view data
 
12.doctrine view data
12.doctrine view data12.doctrine view data
12.doctrine view data
 
11. move in Symfony 4
11. move in Symfony 411. move in Symfony 4
11. move in Symfony 4
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 

Recently uploaded (20)

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 

17.my sql triggers in laravel

  • 1. MySQL triggers in Laravel (continuare la proceduri stocate)
  • 2. Vom adauga trigger-ul before insert: BITrigger care va converti la majuscule toate inregistrarile, inainte de a fi inserate:
  • 3. php artisan make:migration create_BITrigger_trigger class CreateBITrigger extends Migration { /** * Run the migrations. * * @return void */ public function up() { DB::unprepared('CREATE TRIGGER BITrigger BEFORE INSERT ON flowers FOR EACH ROW BEGIN SET NEW.nume=UPPER(NEW.nume); END'); } /** * Reverse the migrations. * * @return void */ public function down() { DB::unprepared('DROP TRIGGER IF EXISTS BITrigger'); } }
  • 4. Daca dorim sa executam fisierele de migrare separat vom crea care un director pt fiecare procedura(sau trigger). De ex, cream directorul BITrigger in /database/migrations si mutam in acesta fisierul de migrari generat pt procedura insert. Apoi dam comanda: php artisan migrate –path=/database/migrations/BITrigger Astfel se va executa numai un fisier de migrare !!! Pt a executa toate migrarile vom scrie: php artisan migrate:refresh
  • 5. php artisan make:migration create_AUTrigger_trigger class CreateAUTrigger extends Migration { /** * Run the migrations. * * @return void */ public function up() { DB::unprepared('CREATE TRIGGER AUTrigger AFTER UPDATE ON flowers FOR EACH ROW BEGIN INSERT INTO flowers_updated(nume, status) VALUES(NEW.nume,"UPDATED"); END'); } /** * Reverse the migrations. * * @return void */ public function down() { DB::unprepared('DROP TRIGGER IF EXISTS AUTrigger'); } }
  • 6. • Creati directorul AUTrigger in /database/migrations si mutati in acesta fisierul de migrari care genereaza trigger-ul AUTrigger. • Pentru a executa migrarea screti: php artisan migrate --path=/database/migrations/AUTrigger
  • 7. Al treilea trigger-- AfterDelete
  • 8. php artisan make:migration create_AfterDelete_trigger class CreateAUTrigger extends Migration { /** * Run the migrations. * * @return void */ public function up() { DB::unprepared(‘CREATE TRIGGER after_Delete AFTER DELETE ON flowers FOR EACH ROW BEGIN INSERT INTO flower_update(nume, status, EDTIME) VALUES(OLD.nume,'DELETED', NOW()); END;'); } /** * Reverse the migrations. * * @return void */ public function down() { DB::unprepared('DROP TRIGGER IF EXISTS after_Delete '); } }
  • 9. • Creati directorul ADelete in /database/migrations si mutati in acesta fisierul de migrari care genereaza trigger-ul AfterDelete. • Pentru a executa migrarea screti: php artisan migrate --path=/database/migrations/ADelete
  • 10. php artisan make:migration create_flower_update_table --create=flower_update Schema::create('flower_update', function(Blueprint $table) { $table->increments('id'); $table->string('nume'); $table->string(‘status'); $table->timestamp(‘EDTIME'); $table->timestamps(); });
  • 11. • Trigger-ul AuTrigger va fi declansat UpdateFlowers, pe care o vom genera astfel: php artisan make:migration create_UpdateFlowers_procedure class CreateUpdateFlowersProcedure extends Migration { /** * Run the migrations. * * @return void */ public function up() { DB::unprepared("CREATE PROCEDURE UpdateFlowers(IN var_nume varchar(255)) BEGIN UPDATE flowers SET pret='10' where nume=var_nume; END"); } /** * Reverse the migrations. * * @return void */ public function down() { DB::unprepared('DROP PROCEDURE IF EXISTS UpdateFlowers'); } }
  • 12. • Vom afisa datele din flowers_updated cu procedura GetUpdated(). php artisan make:migration create_GetUpdated_procedure class CreateGetUpdatedProcedure extends Migration { /** * Run the migrations. * * @return void */ public function up() { DB::unprepared('CREATE PROCEDURE GetUpdated() BEGIN SELECT * FROM flowers_updated; END'); } /** * Reverse the migrations. * * @return void */ public function down() { DB::unprepared('DROP PROCEDURE IF EXISTS GetUpdated'); } }
  • 13. In controller, vom apela procedura astfel: DB::select('CALL UpdateFlowers("toporasi")'); $flowers3=DB::select('CALL GetUpdated()');
  • 14. • Trigger-ul AfterDelete va fi declansat de procedura DeleteFlowers(), pe care o vom genera astfel: php artisan make:migration create_DeleteFlowers_procedure class CreateDeleteFlowersProcedure extends Migration { /** * Run the migrations. * * @return void */ public function up() { DB::unprepared("CREATE PROCEDURE DeleteFlowers(IN var_nume varchar(255)) BEGIN DELETE FROM flowers where nume=var_nume; END"); } /** * Reverse the migrations. * * @return void */ public function down() { DB::unprepared('DROP PROCEDURE IF EXISTS DeleteFlowers'); } }
  • 15. php artisan migrate:refresh Astfel s-au creat toate procedurile si toti triggerii!!! • In controller, adaugati apelul procedurii: DB::select('CALL DeleteFlowers("ghiocei")');
  • 16. FlowersController::index <?php namespace AppHttpControllers; use AppFlower; use DB; class FlowersController extends Controller { public function index(){ DB::insert("CALL InsertFlowers('lalele', 'mici','rosii','100')"); DB::insert("CALL InsertFlowers('trandafiri', 'mari','rosii','200')"); DB::insert("CALL InsertFlowers('toporasi', 'mici','albi','150')"); DB::insert("CALL InsertFlowers('ghiocei', 'mici','albi','10')"); $flowers1=DB::select('CALL GetFlowers()'); DB::select('CALL UpdateFlowers("toporasi")'); DB::select('CALL DeleteFlowers("ghiocei")'); $flowers2=DB::select('CALL GetFlower("trandafiri")'); $flowers3=DB::select('CALL GetUpdated()'); return view('pages.flowers')->with([ 'title'=>'Flowers data', 'flowers1'=> $flowers1, 'flowers2'=>$flowers2, 'flowers3'=>$flowers3]); } } ?>
  • 17. Modelul app/Flower.php <?php namespace App; use IlluminateDatabaseEloquentModel; use DB; class Flower extends Model { protected $table = 'flowers'; //public $timestamps=false; } ?>
  • 19. /pages/flowers.blade.php @extends('pages.master') @section('content') <h1>Flowers data</h1> <table> <tr> <th>Nume</th> <th>Culoare</th> <th>Marime</th> <th>Pret</th> </tr> @foreach($flowers1 as $flower) <tr> <td><?php echo $flower->nume;?></td> <td><?php echo $flower->culoare;?></td> <td><?php echo $flower->marime;?></td> <td><?php echo $flower->pret;?></td> </tr> @endforeach </table><br/><br/>
  • 20. <h1>BEGIN SELECT * FROM flowers WHERE nume="trandafiri"</h1> <table> <tr> <th>Nume</th> <th>Culoare</th> <th>Marime</th> <th>Pret</th> </tr> @foreach($flowers2 as $flower) <tr> <td><?php echo $flower->nume;?></td> <td><?php echo $flower->culoare;?></td> <td><?php echo $flower->marime;?></td> <td><?php echo $flower->pret;?></td> </tr> @endforeach </table> <br/><br/> <h1>Updated/Deleted data</h1> <table> <tr> <th>Nume</th> <th>Status</th> </tr> @foreach($flowers3 as $flower) <tr> <td><?php echo $flower->nume;?></td> <td><?php echo $flower->status;?></td> </tr> @endforeach </table> @endsection
  • 21. /pages/master.blade.php <!DOCTYPE html> <html> <head><title> <?php echo $title;?> </title></head> <body bgcolor='lightblue'> @yield('content') @yield('footer') </body> </html>