ãLaravelãä¸éãã¼ãã«ã触ã£ã¦ã¿ãã追å ã»åé¤ã»åæã
è¶
åèã«ããããã¥ã¡ã³ããµã¤ã
Eloquent:リレーション 8.x Laravel
ä¾ãã°playerã¨teamã®é¢ä¿ã«ã¤ãã¦
1人ã®playerã¯éå»ç¾å¨å«ãã¦è¤æ°ã®teamã«æå±ãã¦(ãã¾ãã)ãã¾ãã
1ã¤ã®teamã¯éå»ç¾å¨å«ãã¦è¤æ°ã®playerãæå±ãã¦(ãã¾ãã)ãã¾ãã
ã¤ã¾ãplayerã¨teamã¯å¤å¯¾å¤ã®é¢ä¿ã«ãªãã¾ãã
å¤å¯¾å¤ã®ãªã¬ã¼ã·ã§ã³ã表ãã¨ãã¯ãå¤å¯¾å¤ã¨ãªããã¼ãã«ã®ä¸éå°ç¹ã«ä¸éãã¼ãã«ããã¾ãã¦
1ã対ãå¤ã対ã1ã¨ãªããããªãªã¬ã¼ã·ã§ã³ã«ãªãããã«è¨è¨ãã¾ãã
ã§ã¯ä¸è¨ã«æ¸ããplayerã¨teamã®ãªã¬ã¼ã·ã§ã³ãlaravelã«è½ã¨ãè¾¼ã¿ããã¨æãã¾ãã
ãã¤ã°ã¬ã¼ã·ã§ã³ãã¡ã¤ã«
create_players_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('players', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('players'); } };
create_team_table.php
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('teams', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('teams'); } };
ä¸éãã¼ãã« create_player_team_table
ä¸éãã¼ãã«ã®å½åè¦åã¯
Eloquent:リレーション 8.x Laravel
ã«ããã¨
ï¼ã¤ã®é¢é£ããã¢ãã«åãã¢ã«ãã¡ãããé ã«çµå
ã¨ãªã£ã¦ãã¾ãã
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('player_team', function (Blueprint $table) { $table->id(); $table->unsignedBigInteger('player_id'); $table->unsignedBigInteger('team_id'); $table->string('note'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('player_team'); } };
ã¢ãã«ã®å®ç¾©
Player.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Team extends Model { use HasFactory; public function players() { return $this->belongsToMany(Player::class); } }
Team.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Team extends Model { use HasFactory; public function players() { return $this->belongsToMany(Player::class); } }
å®éã«ä½¿ã£ã¦ã¿ã
$player = Player::find(1)->teams; dd($player);
å®è¡çµæ
ä¸éãã¼ãã«ã§ãuser_idãï¼ã¨å¯¾å¿ä»ãteamsãã¼ãã«ã®ã¬ã³ã¼ã(Teamã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹)ãåå¾ãããã¨ãåºæ¥ã¾ããã
hasOneãbelongsToã ã¨å¯¾å¿ãããã¼ãã«ã¯å¤é¨ãã¼ãæã£ã¦ãããã¨ãä¸è¬çã§ãããä»åã¯ä¸éãã¼ãã«ã¨ã®ãªã¬ã¼ã·ã§ã³ãæã£ã¦ãã話ãªã®ã§
playersãã¼ãã«ã¯teamsãã¼ãã«ã®å¤é¨ãã¼ãæã¡ã¾ããããteamsãã¼ãã«ãplayersãã¼ãã«ã®å¤é¨ãã¼ãæã£ã¦ãã¾ããã
ä¸éãã¼ãã«ã®å¤ã«ã¢ã¯ã»ã¹ãã
å
ç¨ã®å®è¡çµæã®Teamã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ãã©ã®ãããªå¤ãæã£ã¦ããã®ã確èªããã¨
teamsãã¼ãã«ã®ã«ã©ã ã«å ãã¦ä¸éãã¼ãã«ã®å¤é¨ãã¼player_idã®å¤ãpivot_player_idãå¤é¨ãã¼team_idã®å¤ãpivot_team_idã¨ããååã§åå¾ã§ãã¦ãããã¨ããããã
ãã®å¤ã«ã¢ã¯ã»ã¹ããã«ã¯ä»¥ä¸ã®ããã«ãããfirstã¯ã³ã¬ã¯ã·ã§ã³ã®æåã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãã¦ããã®ã¤ã³ã¹ã¿ã³ã¹ã«å¯¾ãã¦->pivot->team_id;ã¨ãã¦ä¸éãã¼ãã«ã®å¤ãåå¾ãã¦ããã
$player = Player::find(1)->teams->first() ->pivot->team_id;
ãããä¸è¨ã®å®è¡çµæãè¦ã¦ã¿ãã¨ãä¸éãã¼ãã«ã§è¨å®ããã¯ãã®noteã«ã©ã ããªããnoteã«ã©ã ã®å¤ãè¨å®ããã«ã¯
ã¢ãã«å®ç¾©ã«ã¦
Player.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Player extends Model { use HasFactory; public function teams() { return $this->belongsToMany(Team::class)->withPivot('note'); } }
withPivot(ã«ã©ã å)ã¨ããããããããã¨ã§->pivot->noteã¨ããã¨ãä¸éãã¼ãã«ã®noteã«ã©ã ã«ã¢ã¯ã»ã¹ã§ããããã«ãªãã
Pivotã¯ä¸å¿ã¨ããæå³ã§ãwithä¸å¿(Pivot)ã®ã«ã©ã ãã¨æããã¨æè¦çã«ç解ãããããã
ä¸éãã¼ãã«ã«å¤ã追å ãåé¤ããæ¹æ³
attach()
$player = Player::find(2); $player->teams()->attach(2,['note'=>'ã·ã£ãã¯ã¨å ±ã«åªåï¼']);
player_teamãã¼ãã«ã«ã¬ã³ã¼ãã追å ãã¾ããuser_idã«ã©ã ã«ï¼ãteam_idã«ã©ã ã«ï¼ãnoteã«ã©ã ã«'ã·ã£ãã¯ã¨å ±ã«åªåï¼'ãè¨å®ããã¾ãã
detach()
$player = Player::find(2); $player->teams()->detach(2);
player_teamãã¼ãã«ãããuser_idã«ã©ã ãï¼ãteam_idã«ã©ã ã2ã®ã¬ã³ã¼ããåé¤ããã
ããdetach()ã®ããã«å¼æ°ã«team_idãæå®ããªãã¨ãuser_idã«ã©ã ã2ã®ã¬ã³ã¼ããå
¨ã¦åé¤ããã
sync()
$player = Player::find(2); $player->teams()->sync([1 => ['note' => 'hello'],2 => ['note'=>'hello2']]);
syncã¯ä¸éãã¼ãã«ã®è¿½å ãåé¤ã¨ãã£ãèªèãããæåéãåæãã¨ãã¤ã¡ã¼ã¸ã
ä¸è¨ã³ã¼ãã¯ä¸éãã¼ãã«ã®user_idã«ã©ã ã2ã®ã¬ã³ã¼ãã«ããã¦ãteam_idãï¼ã¨2ã®ãã®ãåæããã¨ããæå³ã
ã¤ã¾ãæ¢åã®ä¸éãã¼ãã«ã«user_idã¨team_idã®çµã¿åããã2,3ã®ãã®ããã£ãå ´åã«åé¤ãããã
ã¾ãä¸è¨ã³ã¼ãã¯user_idã2以å¤ã®ã¬ã³ã¼ãã«ã¯å½±é¿ã¯ãªãã
syncWithPivotValues()
$player = Player::find(2); $player->teams()->syncWithPivotValues([1,2,3],['note'=>'hello']);
$player->teams()->sync([1 => ['note' => 'hello'],2 => ['note'=>'hello'],3=>['note'=>'hello']]);
ã¨åãæå³
syncWithoutDetaching()
$player = Player::find(2); $player->teams()->syncWithoutDetaching([4 =>['note'=>'hello']]);
æ¢ã«ãã¼ãã«ã®player_idã¨team_idã«ã©ã ã®çµã¿åããã¨ãã¦
(2,1) (2,2) (2,3)ããã£ãã¨ãã«ã(2,4)ã¨ãã¦è¿½å ã§ããã
attach()ã¨ç°ãªãç¹ã§ãããattachã®å ´åã¯attach(1)ãï¼åå®è¡ããã¨(,1)(,1)ã®ããã«éè¤ã§ãã¦ãã¾ãã¾ãã
syncç³»ã¯åæãªã®ã§éè¤ã®å¿é
ãããã¾ããã
ãPHPãrequire_onceé¢æ°ã§ã¨ã©ã¼ã«ãªã£ã件
âtatum.phpã¯Celticsãã©ã«ãã®ä¸ã«ããã¾ãã
ä¸è¨ãããªæ§æã®ã¢ããªã±ã¼ã·ã§ã³ããããå®è¡ãã¡ã¤ã«ãindex.phpã¨ããã
ããããã®ã³ã¼ãã®å 容
index.php
<?php require_once('Lakers/lebron.php'); hello(); ?>
lebron.php
<?php require_once('../Celtics/tatum.php');
tatum.php
<?php function hello() { echo 'hello'; } ?>
ä¸è¨ã³ã¼ãã§index.phpãå®è¡ããã¨ã¨ã©ã¼ã¨ãªãã
ã¨ã©ã¼ã¨ãªãåå ã¯require_onceã®ãã¹ã®æå®ã®ä»æ¹ã¨PHPã®ä»æ§ã«ããã
PHPã¯å®è¡ãã¡ã¤ã«(index.php)ã®ãã£ã¬ã¯ããªä½ç½®ãåºæºã¨ãã¦ãrequireãè¡ãã®ã§lebron.phpã®require_onceã®ãã¹ã¯
testing_php/../Celtics/tatum.php ã¨ãªããå½ç¶ãã®ãã£ã¬ã¯ããªã«ã¯tatum.phpãåå¨ããªãã®ã§ã¨ã©ã¼ã¨ãªãã
解決æ¹æ³
__DIR__ ã使ããPHPã§ã¯__DIR__ã¨ãããã¸ã«ã«å®æ°ãç¨æããã¦ãã¾ãã
PHP: マジック定数 - Manual
PHPããã¥ã¡ã³ãã«ããã¨
include ã®ä¸ã§ä½¿ç¨ããã¨ã ã¤ã³ã¯ã«ã¼ãããããã¡ã¤ã«ã®åå¨ãããã£ã¬ã¯ããªãè¿ãã¾ãã
ã¤ã¾ãå®è¡ãã¡ã¤ã«ã®å½±é¿ãåããããã¹ãæå®ãããã¨ãã§ããã
ä¾ãã°lebron.phpã®ä¸ã§__dir__ã¯
/var/www/html/Lakers
ã¨ãªã /ããå§ã¾ã£ã¦ãããã¹ãªã®ã§çµ¶å¯¾ãã¹ã«ãªã£ã¦ãããã¨ãåããã
ãªã®ã§lebron.phpã以ä¸ã®ããã«å¤ããã°ã¨ã©ã¼ãçºçããã«å®è¡ãè¡ããã¨ãã§ããã
<?php require_once(__DIR__.'/../Celtics/tatum.php');
ãJavaScriptãAsyncã¨awaitã試ããæ®´ãæ¸ãã
Async
é¢æ°ã®åã«asyncãä»ãããã¨ã«ãã£ã¦promiseãè¿ãããã«ãªããä¾ãã°return 'hello'ã¨ããã¨
async function hello() { return 1; } console.log(hello());
å®è¡çµæ
return <é promise> ãããå ´åãJavaScript ã¯èªåçã«ãã®å¤ãæ㤠解決ããã promise ã«ã©ãããã¾ãã
Async/awaitãããå¼ç¨
Await
Promiseã®çµæ(fulfilled rejected)ã確å®ããã¾ã§ãå¾
æ©ããã
awaitã¯asyncãä»ãã¦ããé¢æ°ã®ä¸ã§ä½¿ããã¨ãã§ããã
async function hello() { let x = await new Promise((resolve)=>{ setTimeout(() => { resolve('hello'); }, 10000); }); console.log(x); } hello();
å®è¡çµæã«helloã¨è¡¨ç¤ºããããresolveã§fulfilledã®ç¶æ
ã«ãªã£ãPromiseã®PromiseResultã®å¤ãå¤æ°xã«ä»£å
¥ãããã
awaitã§resolveã«ãªãã¾ã§å¦çãæ¢ãã訳ã§ãããawaitãç¡ããããã©ããªãã®ãã
çµè«ã¯è§£æ±º(resolve)ããã®ãå¾
ããã«ãpromiseã¤ã³ã¹ã¿ã³ã¹ã代å
¥ãããã
10ç§å¾
ããã«console.logãå®è¡ãããã
async function hello() { let x = new Promise((resolve)=>{ setTimeout(() => { resolve('hello'); }, 10000); }); console.log(x); } hello();
å®è¡çµæ
â»10ç§çµã¤ã¨resolveãããã®ã§ãéçºè ãã¼ã«ã§ä¸è¨ã«åºåããã¤ã³ã¹ã¿ã³ã¹ã®PromiseStateãè¦ãã¨fulfilledã®ç¶æ ã«ãªã£ã¦ãã¾ãã
ãJavaScriptãéåæå¦ç Promise,thenã«ã¤ãã¦è²ã 試ããæ®´ãæ¸ãã
åã«ä»¥ä¸ã®ãããªè¨äºãæ¸ãããã¨ãããã¾ãããã
【JavaScript】Promiseのthenメソッドの挙動について ちょっとした確認 - たけるのプログラミング
ã¾ãéåæå¦çãå®è£ ããæ©ä¼ããã£ãã®ã§ã復ç¿ãã¦ãè²ã ãªã³ã¼ãã試ãã¦ããããã¨æãã¾ãã
ããããéåæå¦çã«ã¤ãã¦ã¯ããã®è¨äºãè¦ããããè¯è³ªãªè¨äºãããããããã®ã§å²æãã¾ããã
ç°¡åã«èª¬æããã¨
JavaScriptã¯ã·ã³ã°ã«ã¹ã¬ããã§ã®å®è¡ã«ãªãã®ã§ãä¾ãã°DBãããã¼ã¿ãåã£ã¦ãããããªå¦çã§å¤ãã®æéãè¦ããå ´åã
ãã®å¾ã«ç¶ãå¦çã¯ãåã®å¦çãçµããã®ãå¾
ããªãã¦ã¯ããã¾ããã
ãããªåé¡ã解決ããããã«å©ç¨ãããã®ãéåæå¦çã§ãã
fetchã使ã£ã¦jsonãã¼ã¿ãåå¾ãã
const response = fetch("http://localhost/fetchtest") .then(function(response){ console.log('ãã¼ã¿åå¾ãã¾ããï¼'); return response.json(); }).then(function(json){ console.log(json); }); console.log(response); console.log('(1)'); console.log('(2)'); console.log('(3)');
å®è¡çµæ
ã¾ãã©ãããfetchããã¦ããã®ãã¨ããã¨ãã¼ã«ã«ã«ç«ã¡ä¸ããlaravelããã¸ã§ã¯ãã§ããJavaScriptã®è©±é¡ã¨ã¯é¢ä¿ãªãã§ããã
laravelã§ã¯ã¢ãã«ã®ã¤ã³ã¹ã¿ã³ã¹ãè¦ç´ ã«æã¤ã³ã¬ã¯ã·ã§ã³ãreturnããã ãã§jsonå½¢å¼ã§ããã³ãã«è¿ããã¨ãåºæ¥ã¾ãã便å©ã§ããã
ã¾ããã®fetchã¯ï¼ç§ãããããã«ãlaravelå´ã§sleepé¢æ°ã使ã£ã¦å¦çãæ¢ãã¦ãã¾ãããªã®ã§JavaScriptãåããã¦ããããã³ãå´ã§ã¯
ãã¼ã¿ãåå¾ããã®ã«ï¼ç§ãããããã«è¦ãã訳ã§ãããã®ããã«è©¦ãã¦ããè¨äºã¯èªåã調ã¹ãéãã¾ã è¦ã¦ã¾ããã
fetchã¯éåæã§è¡ãããæéã®ãããå¦çãªã®ã§ã¾ã
console.log(response); console.log('(1)'); console.log('(2)'); console.log('(3)');
ä¸è¨ã®ã³ã¼ãã®å®è¡çµæãå ã«è¡¨ç¤ºããã¾ãã
console.log(response);
fetchã¡ã½ããã¯Promiseã¤ã³ã¹ã¿ã³ã¹ãè¿ãã¾ãã
ã¾ãä¸è¨ã®ã³ã¼ãã§Promiseã¤ã³ã¹ã¿ã³ã¹ã®ç¶æ
ãè¦ããã¨ãåºæ¥ã¾ããä¸è¨ã«è¼ããå®è¡çµæã§
PromiseStateã®å¤ãpeddingã¨ãªã£ã¦ãã¾ããããã®pendingã¯å¾
æ©ã®ç¶æ
ã®ãã¨ã§ãè¨ãæããã¨laravelãããã¼ã¿ãåå¾ãã¦ããæä¸ã¨ãããã¨ã«ãªãã¾ãã
ãã®å¾ãã®å¦çãæåããã¨fulfilledã
ãã®å¾ãã®å¦çã失æããã¨rejectedã¨ãªãã¾ãã
ãã®å¾fetchã¡ã½ããæåããã¨PromiseStateãfulfilledã¨ãªãã¾ãã
å人çãªæ¨æ¸¬ã§ããfetchã¡ã½ããã®å
é¨ã«ã¦resolveã¡ã½ãããå®è¡ããPromiseStateãfulfilledã«ãªã£ã¦ããã®ããªã¨æã£ã¦ãã¾ãã
Promiseãfulfilledã®ç¶æ ã«ãªãã¨ãã®å¾ã«thenã¡ã½ãããç¹ãã¦ãéåæå¦çãæåããå¾ã«è¡ãããå¦çãå®ç¾©ãããã¨ãåºæ¥ã¾ãã
thenã¡ã½ããã®å¼æ°ã®ã³ã¼ã«ããã¯é¢æ°ã®å¼æ°ã«ã¯ãPromiseã®PromiseResultã®å¤ãå ¥ã£ã¦ãã¾ãã
ã¾ããã®Promiseã®PromiseResultã¯resolveã¡ã½ããã®å¼æ°ã«æå®ããå¤ãå ¥ãã¾ãã
ä¾
console.log(new Promise(function(resolve){ resolve('JavaScript')}));
å®è¡çµæ
ãªã®ã§ï¼ã¤ç®ã®thenã®ã³ã¼ã«ããã¯é¢æ°ã®å¼æ°ã®responseã«ã¯Responseã¤ã³ã¹ã¿ã³ã¹ãå
¥ã£ã¦ãã¾ããã
ã¤ã¾ãä¸è¨ã®ãã¨ããfetchå
é¨ã§ã¯resolve(Responseã¤ã³ã¹ã¿ã³ã¹)ã®ããã«ãªã£ã¦ããã¨äºæ³ãããã¨ãåºæ¥ã¾ãã
const response = fetch("http://localhost/fetchtest") .then(function(response){ console.log('ãã¼ã¿åå¾ãã¾ããï¼'); return response.json(); }).then(function(json){ console.log(json); });
Responseãªãã¸ã§ã¯ãã®jsonã¡ã½ããã¯Promiseã¤ã³ã¹ã¿ã³ã¹ãè¿ãã¾ãã
ããã§èµ·ããçåã§ãããthenã®ã³ã¼ã«ããã¯é¢æ°ã«ã¦Promiseã¤ã³ã¹ã¿ã³ã¹ãè¿ããããthenã¡ã½ããã¯ã©ã®ãããªå¤ãè¿ãã®ãã
çµè«ããããã¨thenã¯Promiseã¤ã³ã¹ã¿ã³ã¹ãè¿ãã¾ãããã³ã¼ã«ããã¯ã®returnã«ãã£ã¦ãã®Promiseã¤ã³ã¹ã¿ã³ã¹ã®ç¶æ
ã¯å¤ããã¾ãã
ããããèªåãªãã®è§£éãå«ã¿ã¾ãã
mdn web docsã«ããã¨
Promise.prototype.then() - JavaScript | MDN
ãã³ãã©é¢æ°ã
ãã§ã«å±¥è¡ããããããã¹ãè¿ããå ´åã then ã«ãã£ã¦è¿ããããããã¹ã¯ããã®ãããã¹ã®å¤ããã®å¤ã¨ãã¦è¿ãã¾ãã
ã¨ããã¾ããå±¥è¡=æåãå¤=PromiseResultã®ãã¨ããªã¨æãã¾ãã
ã¤ã¾ãreturn response.jsonã§è¿ãããPromiseã¤ã³ã¹ã¿ã³ã¹ã®PromiseResultã®å¤ã
thenã¡ã½ããã§è¿ãããPromiseã¤ã³ã¹ã¿ã³ã¹ã®PromiseResultã®å¤ã«è¨å®ãããã®ã§ã¯ãªããã¨æã£ã¦ãã¾ãã
ããã ã¨PromiseResultã®å¤ã次ã®thenã®ã³ã¼ã«ããã¯é¢æ°ã®å¼æ°ã«è¨å®ãããäºå®ã¨è¾»è¤ãããã¾ãã
thenã®ã³ã¼ã«ããã¯é¢æ°ã®returnãpenddingç¶æ ã®Promiseã¤ã³ã¹ã¿ã³ã¹ã ã£ãå ´å
new Promise(function(resolve){ resolve(100); }).then(function(num){ return new Promise(function(resolve){ setTimeout(() => { resolve(); }, 10000); }); }).then(function(){ console.log('ããã¯å®è¡ããã'); });
å®è¡çµæã§ç´10ç§å¾ã«'ããã¯å®è¡ããã'ã¨è¡¨ç¤ºããã¾ããã
mdnããã¥ã¡ã³ãã«ããã¨
Promise.prototype.then() - JavaScript | MDN
ã³ã¼ã«ããã¯é¢æ°ã
ä»ã®å¾ æ©ç¶æ ã®ãããã¹ãªãã¸ã§ã¯ããè¿ããå ´åã then ã«ãã£ã¦è¿ããããããã¹ã®è§£æ±º/æå¦ã¯ããã³ãã©ã¼ã«ãã£ã¦è¿ããããããã¹ã®è§£æ±º/æå¦çµæã«ä¾åãã¾ããã¾ãã then ã«ãã£ã¦è¿ããããããã¹ã®è§£æ±ºå¤ã¯ããã³ãã©ã¼ã«ãã£ã¦è¿ããããããã¹ã®è§£æ±ºå¤ã¨åãã«ãªãã¾ãã
ç¹ã«ä»¥ä¸ã®é¨åã大åã§ãããããèããã¨json()ããã£ã¡ã®ä¾ã ã£ããããããªããããããã ã
then ã«ãã£ã¦è¿ããããããã¹ã®è§£æ±º/æå¦ã¯ããã³ãã©ã¼ã«ãã£ã¦è¿ããããããã¹ã®è§£æ±º/æå¦çµæã«ä¾å
ããããreturnããå¾ãPromiseã®ã³ã¼ã«ããã¯é¢æ°ã£ã¦å®è¡ããããã§ããã
console.log( new Promise(function(resolve){ resolve(100); }).then(function(num){ return new Promise(function(resolve){ setTimeout(() => { console.log('returnãã10ç§å¾ã«resolveãã¾ã'); resolve(100); }, 10000); }); }));
å®è¡çµæ
ãªã®ã§thenã®ã³ã¼ã«ããã¯é¢æ°ãreturnãã10ç§å¾
thenã§è¿ããPromiseã¤ã³ã¹ã¿ã³ã¹ä¸è¨ã®å®è¡çµæãã
PromiseStateãfulfilledã«å¤ãã£ã¦ãPromiseResultã100ã«å¤ããã¾ãã
ãPHP PHPUnitãPHPUnitã使ã£ã¦ç°¡åãªåä½ãã¹ããè¡ã
åèã«ããè¨äº
PHPUnitでユニットテスト① 導入編 | Points & Lines
1. アサーション — PHPUnit latest Manual
PHPUnitã触ã£ã¦ã¿ãã
PHPUnitã¯åä½ãã¹ã(ã¦ããããã¹ã)ãè¡ãããã®ãã¬ã¼ã ã¯ã¼ã¯ã
å®éã«å°å ¥ããç°¡åãªãã¹ããè¡ã£ã¦ã¿ãã
composerãå°å ¥ããã¦ãããã¨ãåæã¨ãã¦ããã
1.PHPUnitã®ã¤ã³ã¹ãã¼ã«
ã¾ãããã¸ã§ã¯ãã®ãã£ã¬ã¯ããªã§ä»¥ä¸ã®ãããªã³ãã³ããå®è¡ããã
composer require --dev phpunit/phpunit ^latest
以ä¸ã®ã³ã¼ãã使ã£ã¦ããã¼ã¸ã§ã³æ å ±ã表示ããããã¤ã³ã¹ãã¼ã«æåã
vendor/bin/phpunit --version
2.ãã¹ã対象ã®ã¯ã©ã¹ãæ¸ã
<?php namespace app; class Sample { public function plus100($num) { $num += 100; return $num; } }
ãªã¼ããã¼ããæå®ããããã«namespaceãè¨è¿°ãã¦ããããã®ããcomposer.jsonã§ã¯ä»¥ä¸ã®ããã«è¨è¿°ãã¦ããã
{ "require-dev": { "phpunit/phpunit": "^9.5" }, "autoload": { "psr-4": { "app\\": "./" } } }
3.ãã¹ãã³ã¼ããæ¸ã
<?php require_once("vendor/autoload.php"); use PHPUnit\Framework\TestCase; use app\Sample; class SampleTest extends TestCase { public function testplus100_1() { $sample = new Sample(); $result = $sample->plus100(-1); $this->assertGreaterThan(100,$result); } public function testplus100_2() { $sample = new Sample(); $result = $sample->plus100(1); $this->assertGreaterThan(100,$result); } }
ä»å使ã£ãã¢ãµã¼ã·ã§ã³ã¡ã½ããã¯assertGreaterThan()
4.ãã¹ãå®è¡&å®è¡çµæ
vendor/bin/phpunit SampleTest.php
F. 2 / 2 (100%) Time: 00:00.036, Memory: 4.00 MB There was 1 failure: 1) SampleTest::testplus100_1 Failed asserting that 99 is greater than 100.
Fã¯ã¢ãµã¼ã·ã§ã³ã«å¤±æããéã«è¡¨ç¤ºãããã.ã¯ãã¹ããæåããéã«è¡¨ç¤ºãããã
3. コマンドラインのテストランナー — PHPUnit latest Manual
There was 1 failureã¨æ¸ãã¦ããããã«ã1ã¤å¤±æãããã
testplus100_1ã§ã¢ãµã¼ã·ã§ã³ã«å¤±æããããã
ä»å¾ã¯ããå°ãè¤éãªãã¹ããæ¸ãã¦ã¿ããã
ãJavaScriptããããã¿ã¤ãã«ã¤ãã¦ãç¶æ¿ ãããã¿ã¤ããã§ã¼ã³ã
ãã®è¨äºã¯JavaScriptã®çµé¨ãæµ
ã人ãæ¸ãã¦ãã¾ãã
ééã£ã¦ããç¹çãããã¾ãããããææããã ããã¨å¹¸ãã§ãã
åèã«ããè¨äº
【JavaScript】 プロトタイプとは?prototypeプロパティはプロトタイプではない件について
JavaScriptのプロトタイプからオブジェクト指向を学ぶ - Qiita
Object のプロトタイプ - ウェブ開発を学ぶ | MDN
ãããã¿ã¤ãã¯ã¡ã¢ãªã®ç¯ç´ãç¶æ¿æ©è½ãæä¾ãããã®ã§ãã
ä¾ãã°ä»¥ä¸ã®ãããªã³ã¼ã
const str = new String('hello');
Stringãªãã¸ã§ã¯ããã¤ã³ã¹ã¿ã³ã¹åãã¾ãã
strã¯Stringãªãã¸ã§ã¯ããªã®ã§ãæè¦çã«Stringãªãã¸ã§ã¯ãã§ç¨æããã¦ããã¡ã½ããã使ãããã¨ã¯åããã¾ãã
ä¾ãã°ä»¥ä¸ã®ãããªã³ã¼ã
console.log(str.charAt(2));
ããã§éçºè ãã¼ã«ã使ã£ã¦strã®æ§é ãè¦ã¦ã¿ããã¨æãã¾ãã
ããã¨charAtã¡ã½ãããå®ç¾©ããã¦ããªããã¨ãåããã¾ããã§ã¯ãªãcharAtã¡ã½ããã使ãããã¨ãã§ããã®ãã
çµè«ããæ¸ãã¨ãprototypeã®ä»çµã¿ã«ãã£ã¦ä¸è¨ã®ããã«ã¡ã½ããã使ããã¨ãã§ãã¦ãã¾ãã
ä¾ãã°ä»¥ä¸ã®ã³ã¼ã
const a = new b;
aã®[[Prototype]]ã«b.prototypeã®åç §ã渡ãã¦ãã
ã¤ã¾ãä¸è¨ã«æ¸ãã
const str = new String('hello');
ã¯
strã®[[Prototype]]ã«String.prototypeã®åç §ã渡ãã¦ããã¨ãããã¨ã«ãªãã¾ãã
ãªã®ã§å ã»ã©ä½¿ã£ãã¡ã½ããã®charAtã¯String.prototypeãªãã¸ã§ã¯ãã®ããããã£ã¨ãã¦ç»é²ããã¦ããã¯ãã§ãã
証æ ã¨ãã¦ããã¥ã¡ã³ãã«ã¯ä»¥ä¸ã®ããã«å®ç¾©ããã¦ãã¾ãã
String.prototype.charAt
ãJavaScriptãPromiseã®thenã¡ã½ããã®æåã«ã¤ã㦠ã¡ãã£ã¨ãã確èª
ãã®è¨äºã¯JavaScriptçµé¨æµ
ããPromiseåå¼·ããã¦ã®äººãæ¸ãã¦ãã¾ãã
ééãçãããã¾ããããææãã¢ããã¤ã¹çããã ããã¨å¬ããã§ãã
thenã¡ã½ããã¯Promiseãªãã¸ã§ã¯ããè¿ãã
ãªã®ã§ãã§ã¼ã³ãããã¨ãå¯è½ã§ãã
ä¾ãã°ãµã¼ãã¼ããfetchã¡ã½ããã使ã£ã¦jsonå½¢å¼ã®ãã¼ã¿ãåå¾ãã¦ã¿ã
fetch('https://jsonplaceholder.typicode.com/users') .then(function(response){ return response.json(); }) .then(function(json){ console.log(json); });
â
ã¾ããã®ã³ã¼ããconsole.logã§åºåããã¨ä»¥ä¸ã®ããã«ãªãã
fetch('https://jsonplaceholder.typicode.com/users')
Promiseãªãã¸ã§ã¯ããè¿ãã¦ããããã®ããå¾ã«thenã¡ã½ãããç¶ãããã¨ãã§ããã
ã¾ãPromiseResultã®å¤ãResponseãªãã¸ã§ã¯ãã«ãªã£ã¦ããã
â¡
次ã«ãã®ã³ã¼ãã®æ»ãå¤ãconsole.logã§åºåããã¨ä»¥ä¸ã®ããã«ãªãã
.then(function(response){ return response.json(); })
thenã¡ã½ããã®å¼æ°ã«ã³ã¼ã«ããã¯é¢æ°ã使ç¨ãã¦ããããã®ã³ã¼ã«ããã¯é¢æ°ã®å¼æ°ã§ããresponseã«â ã®PromiseResultã®å¤ãæ ¼ç´ããã¦ããã
ä¸è¨ã§ãæ¸ããããã«thenã¡ã½ããã¯Promiseãè¿ãã注ç®ããã¹ãç¹ã¯ã³ã¼ã«ããã¯é¢æ°å
ã§returnããå¤(response.json)ãPromiseResultã®å¤ã¨ãªã£ã¦ãããã¨ã
ã¤ã¾ãâ ââ¡ã®ããã«PromiseResultã®å¤ã å¾ã«ç¶ãthenã¡ã½ããã®ã³ã¼ã«ããã¯é¢æ°ã®å¼æ°ã®å¤ã¨ãã¦å©ç¨ãããã¨ãã§ããã
è£è¶³
ä¾ãã°thenã®ã³ã¼ã«ããã¯é¢æ°ã®æ»ãå¤ã«Promiseãæå®ãããã©ããªãã®ã試ãã¦ã¿ãã
console.log( fetch('https://jsonplaceholder.typicode.com/users') .then(function(response){ //return response.json(); return Promise.resolve(100); }) );