Skip to content

Instantly share code, notes, and snippets.

@lotsofbytes
Last active October 6, 2018 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotsofbytes/e1a58e0921ce7ddb1beb4ffab57779b3 to your computer and use it in GitHub Desktop.
Save lotsofbytes/e1a58e0921ce7ddb1beb4ffab57779b3 to your computer and use it in GitHub Desktop.
[バリデーション:exists] #laravel #L55 #validation

バリデーション:exists

入力値が、指定のDBテーブルのコラムに値が存在することを判定。

laravel 5.5

gistの画面

以下の実行で、ダウンロードできます

$ git clone git@gist.github.com:e1a58e0921ce7ddb1beb4ffab57779b3.git tests/Unit/Validations/Exists
<?php
namespace Tests\Unit\Validations\Exists;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Validator;
use App\User;
class ExistsTest extends TestCase
{
use RefreshDatabase;
/**
* @test
* @dataProvider provider_exists
*/
public function exists($input, $expected)
{
// \DB::enableQueryLog();
// DBに1つだけレコードを作成
$user = factory(User::class)->create([
'email' => 'test@example.com'
]);
$v = Validator::make(
$input,
['email' => 'required|exists:users,email']
);
$this->assertEquals($expected, $v->passes());
// print_r(\DB::getQueryLog());
}
public function provider_exists()
{
return [
[['email' => 'test@example.com'], true],
[['email' => 'test2@example.com'], false],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment