Skip to content

Commit 83f65a4

Browse files
Added Interaction with migrations section
1 parent 317335c commit 83f65a4

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

docs/snippets/with_operation.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
return new class extends Migration {
8+
public function __invoke()
9+
{
10+
// some
11+
}
12+
13+
public function withOperation(): string
14+
{
15+
return 'foo/2022_10_14_000002_test2';
16+
}
17+
};

docs/topics/running-operations.topic

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,47 @@
220220
<a href="https://laravel.com/docs/queues">queuing system</a> must work in your application.
221221
</note>
222222
</chapter>
223+
224+
<chapter title="Interaction with migrations" id="interaction_with_migrations">
225+
<p>
226+
Operations can also be invoked when Laravel migrations are completed (<code>php artisan migrate</code>).
227+
The Laravel event system is used for this purpose.
228+
</p>
229+
230+
<p>
231+
To do this, add a <code>withOperation</code> public method to your migration file that
232+
will return the name of the file or folder to call.
233+
For example:
234+
</p>
235+
236+
<code-block lang="php" src="with_operation.php" include-lines="5-" />
237+
238+
<p>
239+
Now, once the migration is done, Laravel will send a <code>MigrationEnded</code> event, catching which the
240+
<code>%artisan% %command_run%</code> console command will be called passing this parameter.
241+
</p>
242+
243+
<p>
244+
The same thing will happen if you invoke the following console command:
245+
</p>
246+
247+
<code-block lang="bash">
248+
%artisan% %command_run% --path="foo/2022_10_14_000002_test2"
249+
</code-block>
250+
251+
<p>
252+
This method works with all three migration methods: <code>up</code>, <code>down</code> and
253+
<code>__invoke</code>.
254+
</p>
255+
256+
<p>
257+
When the <code>%artisan% migrate</code> console command is called,
258+
the operation will call the <code>up</code> or <code>__invoke</code> method.
259+
</p>
260+
261+
<p>
262+
When the <code>%artisan% migrate:rollback</code> console command is called,
263+
the operation will call the <code>down</code> method if it exists in the operation file.
264+
</p>
265+
</chapter>
223266
</topic>

0 commit comments

Comments
 (0)