# Laravel
# æºå
# ã»ããã¢ããæé
- Composer ãã€ã³ã¹ããŒã«
C:/Users/Shota/AppData/Roaming/Composer/vendor/bin
ã«ãã¹ãéãcomposer global require "laravel/installer"
# åºæ¬
laravel new ${myProjectName}
php artisan serve # ãã¹ãå°çšã³ãã³ãã§ãããæ¬çªç°å¢ã§ã¯äœ¿ããªã
# æ¬çªç°å¢ã®ã»ããã¢ããïŒXAMPP ã®äŸïŒ
äŸãã°htdocs
ã«my-app
ãšãããããžã§ã¯ããæŸã蟌ãã ãšãããšãhttps://some.com/my-app/public
ãšããã¢ãã¬ã¹ã§ã¢ããªã±ãŒã·ã§ã³ã«ã¢ã¯ã»ã¹ã§ããã
/my-app/public
ã/
ã«ããããããïŒãšã€ãªã¢ã¹ãäœãããïŒå Žåã¯ãæ¬æž P20 ã«èšèŒã®èšå®ã Apache ã® httpd.conf
ã«å
¥ããããšã
# ãã¡ã€ã«ã»ãã©ã«ãæ§æ
# ã«ãŒããã¡ã€ã«
name | description |
---|---|
.env , .env.example | DB ã®èªèšŒæ å ±ãªã©ãåäœã«é¢ããèšå®ãã¡ã€ã« |
artisan | php artisan *** ã®ããã«ããŠäœ¿ã |
composer.json , composer.lock | composer ãã€ãã |
phpunit.xml | ãŠããããã¹ãã«é¢ããèšå®ãã¡ã€ã« |
server.php | ãµãŒãæ¬äœ |
webpack.min.js | webpack ã®èšå®ãã¡ã€ã« |
# ã«ãŒããã©ã«ã
*
ããŒã¯ã¯ãã䜿ããã©ã«ãã
name | description |
---|---|
app | * ã¢ããªã±ãŒã·ã§ã³ã®æ¬äœ |
bootstrap | èµ·åæã®åŠç |
config | èšå® |
database | * DB é¢ä¿ |
public | ãã®ãŸãŸå
¬éãããã¡ã€ã«çŸ€ããã¹ãååŸããããšãã¯asset() ãã«ãã䜿ã |
resources | * ãã³ãã¬ãŒããããã«ããã¹ã JS/CSS ãªã©ã®ãªãœãŒã¹ |
routes | * ã«ãŒãã£ã³ã°æ å ± |
storage | ãã°ãªã©ã®ãã¡ã€ã«ã®ä¿åå Žæ |
tests | ãŠããããã¹ãé¢ä¿ |
vendor | Laravel æ¬äœã®ããã°ã©ã |
# app ãã©ã«ã
name | description |
---|---|
Console | ã³ã³ãœãŒã«ããã°ã©ã ïŒ |
Exceptions | äŸå€åŠç |
Http | Web ã¢ããªã±ãŒã·ã§ã³ã«ã¢ã¯ã»ã¹ãããšãã®åŠçãäžçªè¯ã䜿ã |
Providers | ãµãŒãã¹ãããã€ããã¢ããªéå§æã®åŠçãªã©ãèšè¿°ãã |
User.php | ãŠãŒã¶èªèšŒã«é¢ããããã°ã©ã |
# routes ãã©ã«ã
name | description |
---|---|
api.php | API ã®æ©èœãç¹å®ã®ã¢ãã¬ã¹ã«å²ãåœãŠããæã«äœ¿ã |
channels.php | ãããŒããã£ã¹ããã£ã³ãã«ã®ããã®ã«ãŒãã£ã³ã°ïŒ |
console.php | ã³ã³ãœãŒã«ããã°ã©ã ã®ããã®ã«ãŒãã£ã³ã°ïŒ |
web.php | äžè¬ç㪠Web ããŒãžãšããŠã¢ã¯ã»ã¹ãããšãã®ã«ãŒãã£ã³ã°ãäžçªè¯ã䜿ã |
# ã«ãŒãã£ã³ã°ãšã³ã³ãããŒã©
# ã«ãŒãã£ã³ã°
ã«ãŒãã£ã³ã°ã®èšå®ã¯routes/web.php
ã«èšèŒããã
# çã® HTML ãè¿ã
Route::get('/hello', function() {
return '<h1>Some HTML!</h1>';
});
// GET以å€ã®ã¡ãœããã®å Žå
Route::post();
Route::put();
Route::delete();
# ãã³ãã¬ãŒããæå®ãã
ïŒå®éã¯view()
㯠Response ãªããžã§ã¯ããè¿ãïŒ
// routes/web.php
Route::get('/', function() {
return view('welcome');
});ã// => `resources/views/welcome.blade.php`
Route::get('/hello', function() {
return view('hello.index');
});ã// => `resources/views/hello/index.blade.php`
# ã³ã³ãããŒã©ãæå®ãã
Route::get('/hello', 'HelloController@index');
Route::get('/hello', 'HelloController'); // ã·ã³ã°ã«ã¢ã¯ã·ã§ã³ã³ã³ãããŒã©ã®å Žå
# params ãåãåã
ä»»æã® param ã«ã¯?
ãã€ãããä»»æã®é
ç®ã«ã¯ããã©ã«ãå€ãã»ããããŠãããšè¯ãã
Route::get('/hello/{msg}/{id}', function($msg, $id){ // é ã«æž¡ãããã®ã§ååã¯éã£ãŠãOK
return "<h1>Hello! $msg2 $id</h1>";
});
Route::get('/hello/{msg?}/{id?}', function($msg='a', $id='b'){
return "<h1>Hello! $msg $id</h1>";
});
# ã«ãŒãã®äžèŠ§ã衚瀺ãã
php artisan route:list
# ã³ã³ãããŒã©
# MVC
User <-> Controller <-â¬-> View <-> Templates
â
â-> Model <-> Database
# ã³ã³ãããŒã©ã®äœæ
# 空ã®ã³ã³ãããŒã©ãäœæãã
php artisan make:controller HelloController
# ãªãœãŒã¹ãã«(å
žåçãªã¡ãœããããããããæã£ãŠãã)ãªã³ã³ãããŒã©ãäœæãã
php artisan make:controller HelloController --resource
// app/Http/Controllers/HelloController.php
namespace App\Http\Controllers; // åå空éã®æå®
use Illuminate\Http\Request; // Requestãªããžã§ã¯ãã䜿ããããèšå®
class HelloController extends Controller {}; // ã³ã³ãããŒã©æ¬äœ
# ã¢ã¯ã·ã§ã³ã®è¿œå
- ã¢ã¯ã·ã§ã³ïŒã³ã³ãããŒã©ã«çšæãããåŠçã®ããš
- ååãå€ããããšã§ãè€æ°èšå®ã§ãã
- ã€ã³ã¹ã¿ã³ã¹ã¡ãœãããšããŠå®è£ ãã
- ã¢ã¯ã·ã§ã³ã¯ãHTML ãããã¯ãªã¯ãšã¹ããªããžã§ã¯ãã Return ãã
// controller
class HelloController extends Controller
{
public function index() {
return <<<EOF
<h1>ID: $id</h1>
<h1>Pass: $pass</h1>
EOF;
}
}
ã³ã³ãããŒã©ãã«ãŒãã£ã³ã°èšå®ã«èšèŒããã
// routes/web.php
Route::get('/hello', 'HelloController@index');
# ã·ã³ã°ã«ã¢ã¯ã·ã§ã³ã³ã³ãããŒã©
äžã€ããã¢ã¯ã·ã§ã³ãæããªãã¯ã©ã¹ã®å Žåã__invoke()
ã¡ãœãããå®è£
ããããšã§èšèŒãç°¡ç¥åã§ããã
// controller
class HelloController extends Controller
{
public function __invoke() {
return '<h1>something</h1>';
}
}
ã«ãŒãã£ã³ã°èšå®ã§ã¯ã¡ãœããåãçç¥ããã
// routes/web.php
Route::get('/hello', 'HelloController');
# ãªãœãŒã¹ãã«
- Resourceful = RESTful ã«å ããè¿œå ã»ç·šéã»åé€çšãã©ãŒã çããªãœãŒã¹ãæ±ãããã«å¿ èŠãªãã¹ãŠã®æ©èœãåããŠããããš
- äžèšã®æé ã§ãªãœãŒã¹ãã«ãªã³ã³ãããŒã©ãäœæããäœæããã³ã³ãããŒã©ã
Route::resource
ã«æž¡ããŠããããšã§ãã¢ã¯ã·ã§ã³ãåå¥ã«äœæããããšãªããç°¡åã«ãªãœãŒã¹ãã«ãªã³ã³ãããŒã©ã䜿ãå§ããããšãã§ããã
php artisan make:controller RestappController --resource
Route::resource('/rest', 'RestappController');
http method | route | action | RESTful | Resourceful |
---|---|---|---|---|
GET | /route | index() | * | * |
GET | /route/create | create() | * | |
POST | /route | store() | * | * |
GET | /route/1 | show() | * | * |
GET | /route/1/edit | edit() | * | |
PUT/PATCH | /route/1 | update() | * | * |
DELETE | /route/1 | destroy() | * | * |
# ãã©ãŒã ãã PUT ã DELETE ãéãã«ã¯
- ãªãœãŒã¹ãã«ãªã«ãŒããèšå®ããå Žåã¯ãæŽæ°ãåé€ã®åŠçã
PUT
ãDELETE
ãšãã£ãã¡ãœããã§è¡ãå¿ èŠãããã - ãããããã©ãŒã ããé
ããã®ã¯
POST
ã¡ãœããã ãã§ããã - ãã®ããã
PUT
ãDELETE
ã䜿ãéã¯ã_method
ãšããé ãå±æ§ã«ã¡ãœãããèšè¿°ããŠããå¿ èŠãããã
<form action="/posts/1234/delete" method="POST">
<input type="hidden" name="_method" value="PUT" />
</form>
# ã¢ã¯ã·ã§ã³ã§ params ãåãåã
// routes/web.php
Route::get('/hello/{id?}/{pass?}', 'HelloController@index');
// controller
class HelloController extends Controller
{
public function index($id='defualtId', $pass='defaultPass') { /* do something */}
}
# Request, Response ã®å©çš
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class HelloController extends Controller
{
public function index(Request $request, Response $response) {
$html = <<<EOF
<h1>Request</h1>
<pre>$request</pre>
<h1>Response</h1>
<pre>$response</pre>
EOF;
$response->setContent($html);
return $response;
}
}
äž»ãªã¡ãœãã
$request->url()
ã¯ãšãªãå«ãŸãªã URL ãè¿ã$request->fullUrl()
ã¯ãšãªãå«ã URL ãè¿ã$request->path()
ãã¡ã€ã³ããäžã®ãã¹éšåã ããè¿ã$request->QUERY_NAME
ã¯ãšãªãååŸããïŒã¯ãšãªåã id ãªãã$request->id
ïŒ$response->status()
ã¹ããŒã¿ã¹ã³ãŒããè¿ã$response->content()
ã³ã³ãã³ããååŸ$response->setContent()
ã³ã³ãã³ããèšå®
ãªããLaravel ã§ã¯ãäžèšã®ããŒã¿ãå šãŠãªã¯ãšã¹ããªããžã§ã¯ãã«ã¶ã£ããŸããïŒExpress ã®ããã«ãreq.params ã req.query ãªã©ã¯ãªãããããªã req.propertyName ã®åœ¢ã§è¿œå ãããïŒã
- params
- query string
- form data (input ã® name å±æ§)
- ããã«ãŠã§ã¢ããæž¡ãããå€ïŒ
$request->merge(é å)
ãªã©ïŒ
# ãã¥ãŒãšãã³ãã¬ãŒã
# PHP ãã³ãã¬ãŒãã®å©çš
PHP ãã³ãã¬ãŒãã䜿ãã«ã¯ãview(ãã©ã«ãå.ãã¡ã€ã«å)
ãåŒã¶ããšã§ããã³ãã¬ãŒããã Response ã€ã³ã¹ã¿ã³ã¹ãäœæããã
Route::get('/hello', function(){
// resources/views/hello/index.phpãšãããã³ãã¬ãŒããåºã«ãã
// view()ã¯Responseã€ã³ã¹ã¿ã³ã¹ãçæãã
return view('hello.index');
});
äžèšã¯ãã«ãŒãã£ã³ã°èšå®ã«ãã³ãã¬ãŒããæå®ããŠããã ã«ãŒãã£ã³ã°èšå®ã«ã³ã³ãããŒã©ãæå®ããå Žåã¯ã次ã«ããã«ããã
class HelloController extends Controller
{
public function index() {
return view('hello.index');
}
}
ãã³ãã¬ãŒãã«ããŒã¿ãæž¡ããããšãã¯ãview ã®ç¬¬äºåŒæ°ã« Array ãæž¡ãã
// controller
class HelloController extends Controller
{
public function index() {
// compactçã䜿ã£ãŠãæž¡ãããŒã¿ãäžã€ã¥ã€æå®ããæ¹æ³
$title = 'my title';
$msg = 'my msg';
return view('hello.index', compact('title', 'msg'));
// é
åã䜿ã£ãŠããŸãããšããŒã¿ãæž¡ãæ¹æ³
$data = [
'msg1' => 'msg1ã§ã',
'msg2' => 'msg2ã§ã',
];
return view('hello.index2', $data);
}
}
// template
<h1>{{ $title . $msg }}</h1>
<h1>{{ $msg1 . $msg2 }}</h1>
# Blade ãã³ãã¬ãŒãã®å©çš
blade ãã³ãã¬ãŒããå©çšããã«ã¯ãFILENAME.blade.php
ã®åœ¢åŒã§ãã³ãã¬ãŒããäœæããã
PHP ãã³ãã¬ãŒããš Blade ãã³ãã¬ãŒããäž¡æ¹ååšããå Žå㯠blade ãåªå
ãããã
# Form ãå®è£ ããŠã¿ã
input èŠçŽ ã® name å±æ§ãããã®ãŸãŸ$request
ã®ããããã£ãšããŠåãåºããããã«ãªãã
// template
<form method="POST" action="/hello">
@csrf // å€éšãµã€ãããã®ãã©ãŒã éä¿¡ãé²ããããèªèšŒããŒã¿ãæ¿å
¥
<input type="text" name="myname">
<input type="submit">
</form>
// routing
Route::post('/hello', 'HelloController@post');
// controller
class HelloController extends Controller
{
public function post(Request $request) {
// $request->myname ã«å
¥åããå€ãå
¥ã£ãŠãã
}
}
# Blade ã®æ§æ
# å€ã®è¡šç€º
{{ å€ãªã© }}
{!! å€ãªã© !!} // HTMLãšã¹ã±ãŒãããããªãå Žå
# æ¡ä»¶åå²
@if()
@elseif ()
@else
@endif
@unless()
@else
@endunless
@empty()
@else
@endempty
@isset()
@else
@endisset
# ç¹°ãè¿ã
@for (i=1; i<10; i++)
@endfor
@foreach ($array as $value)
@endforeach
// foreach-else
@forelse ($array as $value)
// é
åãããå Žåã®åŠç
@empty
// é
åã空ã®å Žåã®åŠç
@endforelse
@while ()
@endwhile
@break // ç¹°ãè¿ããçµäº
@continue // 次ã®ã«ãŒããž
ç¹°ãè¿ãã®äžã§ã¯$loop
ãšããç¹æ®ãªãªããžã§ã¯ãã䜿ããã
$loop->index
ã€ã³ããã¯ã¹ïŒ0 ã¹ã¿ãŒãïŒ$loop->iteration
ç¹°ãè¿ãæ°ïŒ1 ã¹ã¿ãŒãïŒ$loop->remaining
æ®ãåæ°(ãã®ã«ãŒããå«ãŸã)$loop->count
ç¹°ãè¿ãã®å é åã®èŠçŽ æ°$loop->first
æåã®ã«ãŒããã©ãã$loop->last
æåŸã®ã«ãŒããã©ãã$loop->depth
ç¹°ãè¿ãã®ãã¹ãæ°(1 ã¹ã¿ãŒã)$loop->parent
ãã¹ãããŠããå Žåã«ã芪ã®ã«ãŒãå€æ°ãè¿ã
@foreach ([1,2,3] as $value)
{{$loop->index}}
@endforeach
// => 0,1,2
# php ãã£ã¬ã¯ãã£ã
@php
ã䜿ããšãblade ãã³ãã¬ãŒãã®äžã« php ãèšèŒã§ããã
åºæ¬çã«ãã³ãã¬ãŒãã®äžã«ããžãã¯ãæžãã®ã¯ããããã©ã¯ãã£ã¹ãªã®ã§ã
ãããŸã§ãã¥ãŒã«é¢ããå©çšã«ãšã©ããããšã
@php
$counter = 0;
@endphp
@while($counter < 3);
{{ $counter }}
@php
$counter += 1;
@endphp
@endwhile
# ã¬ã€ã¢ãŠãã®äœæ
# ããŒã¹ã¬ã€ã¢ãŠããšç¶æ¿ã¬ã€ã¢ãŠã
ããŒã¹åŽã«ãããŠã@yield()
, @section() - @show
ã䜿ã£ãŠå ŽæãçšæããŠããã
ç¶æ¿åŽã§@section()
, @section()-@endsection
ã䜿ãããšã§å
容ãåããŠããæ¹æ³ã
Nuxt.js ã® Layouts ãšåããããªäœ¿ãæ¹ãããã°ãããã ãšæãã
ããŒã¹ã¬ã€ã¢ãŠã
// resourses/views/layouts/helloapp.blade.php (layoutsãšãããã©ã«ãåã¯å€ããŠããã)
<body>
<h1>@yield('title')</h1>
@yield('content')
@section('menubar')
<p>ã¡ãã¥ãŒ</p>
@show
@yield('footer')
</body>
ç¶æ¿ã¬ã€ã¢ãŠã
// resourses/views/hello.blade.php
@extends('layouts.helloapp')
@section('title', 'My Title')
@section('content')
<p>ãããæ¬æã§ãã</p>
@endsection
@section('menubar')
@parent // ããŒã¹ã¬ã€ã¢ãŠãã®äžèº«ãç¶æ¿ãããæã«äœ¿ã
ã¡ãã¥ãŒé
ç®ãªã©
@endsection
@section('footer')
<p>copyright 2018</p>
@endsection
# ã³ã³ããŒãã³ãã®äœæ
- ããããããã¿ãªã©ãããŒãããšã«äœæããæ¹æ³
- ã³ã³ããŒãã³ãã®äœææ¹æ³ã¯éåžžã®ãã³ãã¬ãŒããšå šãåã
// resources/views/components/message.blade.php (componentsãšãããã©ã«ãåã¯å€ããŠããã)
<p>ããã¯ã³ã³ããŒãã³ãã§ã</p>
<p>{{ $msg_title }}</p>
<p>{{ $msg_content }}</p>
# ã³ã³ããŒãã³ãã®å©çšãã® 1 (@component
ïŒ
- å€ã¯
@slot()
ã§æž¡ã - 芪ãã³ãã¬ãŒãã®å€æ°ã¹ã³ãŒãã¯ãã³ã³ããŒãã³ãã«åŒãç¶ãããªãã
// resources/views/hello/index.blade.php
@component('components.message')
@slot('msg_title','ã¿ã€ãã«ãã§')
@slot('msg_content')
ã³ã³ãã³ãã§ã
@endslot
@endcomponent
# ã³ã³ããŒãã³ãã®å©çšãã® 2ïŒ@include
=ãµããã¥ãŒïŒ
- å€ãæž¡ããšã㯠Array ã§æž¡ã
- 芪ãã³ãã¬ãŒãã®å€æ°ã¹ã³ãŒãã¯ãã³ã³ããŒãã³ãã«åŒãç¶ãããã芪ãã³ãã¬ãŒãã§å©çšã§ããå€æ°ã¯ãäœãããã«èªã¿èŸŒãã ã³ã³ããŒãã³ãå ã§å©çšã§ããããŸãã«ãããã«ãã³ãã¬ãŒããç¶ã足ãããããªæåããããšããããšã
@include('components.message',[
'msg_title' => 'ã¿ã€ãã«ã§ã',
'msg_content' => 'æ¬æã§ã'
])
# ã³ã³ããŒãã³ãã®å©çšãã® 3 (@each
=ã³ã¬ã¯ã·ã§ã³ãã¥ãŒ)
- ç¹°ãè¿ãããŒã¿ãã³ã³ããŒãã³ãã§è¡šç€ºããæ¹æ³
@each(èªã¿èŸŒãã³ã³ããŒãã³ã, æž¡ãå€æ°, ã³ã³ããŒãã³ãå ã«ãããããå€æ°å)
// ãã³ãã¬ãŒãåŽã$dataã¯çŸå®ã®ã¢ããªã§ã¯ã³ã³ãããŒã©çããåãåãããšã«ãªã
@php
$data = [
['name' =>'john', 'mail' => 'john@test.com'],
['name' => 'jack', 'mail' => 'jack@test.com']
]
@endphp
@each('components.item', $data, 'item')
// ã³ã³ããŒãã³ãåŽãresources/views/components/item.blade.php
<li>{{ $item['name'] }} - {{ $item['mail'] }}</li>
# ãã¥ãŒã³ã³ããŒã¶ãŒ
ã³ã³ãããŒã©ããããã¥ãŒã«é¢ããããžãã¯ãåé¢ããããã«äœ¿ãã ç¹å®ã®ãã¥ãŒã衚瀺ããéã«å¿ èŠãšãªãåŠçãå®è¡ããçµæãªã©ã®æ å ±ããã³ãã¬ãŒãã«æž¡ã圹å²ãæã€ã
Client <-> Controller <-Rendering <-- View Template
^
â----- View Composer
# ãµãŒãã¹ãããã€ãã®äœæãšç»é²
- ãã¥ãŒã³ã³ããŒã¶ã®ã»ããã¢ããã¯ããµãŒãã¹ãããã€ããšããä»çµã¿ã䜿ã£ãŠè¡ãã
- ãµãŒãã¹ãããã€ãã䜿ããšãã¢ããªã±ãŒã·ã§ã³éå§æã«å¿ èŠãªåŠçãè¡ãããšãã§ããã
- ãŸãã¯ç©ºã®ãµãŒãã¹ãããã€ããç»é²ããã
php artisan make:provider HelloServiceProvider
// app/Providers/HelloServiceProvider.php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class HelloServiceProvider extends ServiceProvider
{
public function boot(){
// ã¢ããªã±ãŒã·ã§ã³ã®èµ·åæã«è¡ãããåŠç
}
}
// config/app.php
'providers' => [
// äžèšã®è¡ãè¿œå ãã
App\Providers\HelloServiceProvider::class,
],
# ç¡åé¢æ°ã䜿çšããŠã»ããã¢ãã
// app/Providers/HelloServiceProvider.php
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class HelloServiceProvider extends ServiceProvider
{
public function boot()
{
View::composer('hello.index', function($view){
$view->with('value_from_composer', 'this message is from view composer!');
});
}
}
// resources/views/hello/index.blade.php
{{ $value_from_composer }} // => 'this message is from view composer!'
- ãµãŒãã¹ãããã€ãå
ã§
View::composer()
ã䜿ã£ãŠããã¥ãŒãšå¯Ÿå¿ããåŠçïŒç¡åé¢æ° or ã¯ã©ã¹ïŒãèšèŒããããšã§ã»ããã¢ããããã - äžèšã®äŸã§ã¯ã
resources/views/hello/index.blade.php
ãåŒã°ããéã«ãç¡åé¢æ°å ã«èšèŒããåŠçãè¡ãããã«èšå®ããŠããã $view
㯠View ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãããã䜿ã£ãŠãã¥ãŒãæäœãããäžèšäŸã§ã¯ã$view->with()
ã䜿ã£ãŠãã¥ãŒã«å€æ°ãè¿œå ããŠããã
# ã¯ã©ã¹ã䜿çšããŠã»ããã¢ãã
å
è¿°ã®ç¡åé¢æ°ã®éšåãã¯ã©ã¹ãšããŠå®è£
ããæ¹æ³ã
ãã¥ãŒã³ã³ããŒã¶ã¯ã©ã¹ã¯ãäœãç¶æ¿ããŠããªããã ã®ã¯ã©ã¹ã§ããã
compose
ãšããã¡ãœãããå®è£
ããŠããããã° OKã
// app/Http/Composers/HelloComposer.php Composerãã©ã«ãã®ååã¯å€ããŠãOK
namespace App\Http\Composers; // ãããå¿ãããšä»ã®ãã¡ã€ã«ããåç
§ã§ããªã
use Illuminate\View\View;
class HelloComposer
{
public function compose(View $view)
{
$view->with('value_from_composer', 'this message is from view composer!');
}
}
ã¯ã©ã¹ãäœæãããããµãŒãã¹ãããã€ãã«ããããã¥ãŒãžã®åŠçã®å²åœããã¯ã©ã¹ã䜿ã£ããã®ã«æžãæããã
// app/Providers/HelloServiceProvider.php
class HelloServiceProvider extends ServiceProvider
{
// ã¢ããªã±ãŒã·ã§ã³ã®èµ·åæã«è¡ãããåŠç
public function boot()
{
View::composer('hello.index', 'App\Http\Composers\HelloComposer');
}
}
# ãªã¯ãšã¹ãã»ã¬ã¹ãã³ã¹ã®è£å®
# ããã«ãŠã§ã¢
- ã³ã³ãããŒã©ã®åãåŸã«å²ã蟌ã¿ãåŠçãè¡ã
- ããã«ãŠã§ã¢ã¯ãã«ãŒãã£ã³ã°æ å ±ãèšèŒããéã«æå®ã§ãã
# é圢ã®äœæ
php artisan make:middleware HelloMiddleware
// app/Http/Middleware/HelloMiddleware.php
namespace App\Http\Middleware;
use Closure; // ç¡åã¯ã©ã¹ãè¡šãã¯ã©ã¹ïŒ
class HelloMiddleware
{
public function handle($request, Closure $next)
{
// $requestã䜿ã£ãŠããªã¯ãšã¹ãã«å²ã蟌ãåŠçãããã«æžã
$response = $next($request);
// $responseã䜿ã£ãŠãã¬ã¹ãã³ã¹ã«å²ã蟌ãåŠçãããã«æžã
return $response;
}
}
// ã«ãŒãã£ã³ã°èšå®
Route::get('/hello', 'HelloController@index')
->middleware(HelloMiddleware::class)
->middleware(SomeOtherMiddleware::class);
- ãŠãŒã¶ããã®ãªã¯ãšã¹ãããããšãæåã®ããã«ãŠã§ã¢ã®
handle()
ãå®è¡ãããã handle()
ã®äžã®$next
ã¯äžèšã®ãããããåŒã¶ããªããäžèšã¯ã©ã¡ããã¬ã¹ãã³ã¹ãªããžã§ã¯ããè¿ãã- 次ã®ããã«ãŠã§ã¢ã®
handle()
ã¡ãœãã - 次ã®ããã«ãŠã§ã¢ãååšããªãå Žåã¯ã³ã³ãããŒã©ã®ã¢ã¯ã·ã§ã³
- 次ã®ããã«ãŠã§ã¢ã®
$next
ã次ã ã«åŒã°ããŠããã®ã§ãååž°çã«åŠçãå®è¡ãããŠãã- æåã®ããã«ãŠã§ã¢ã®ãªã¯ãšã¹ãã«é¢ããåŠç
- n çªç®ã®ããã«ãŠã§ã¢ã®ãªã¯ãšã¹ãã«é¢ããåŠç
- ã³ã³ãããŒã©ã®ã¢ã¯ã·ã§ã³
- n çªç®ã®ããã«ãŠã§ã¢ã®ã¬ã¹ãã³ã¹ã«é¢ããåŠç
- 1 çªç®ã®ããã«ãŠã§ã¢ã®ã¬ã¹ãã³ã¹ã«é¢ããåŠç
# ãªã¯ãšã¹ããä¿®æ£ãã
ãªã¯ãšã¹ããªããžã§ã¯ããæäœããããšã§ããªã¯ãšã¹ãå
容ã«æãå ããããšãã§ãããäŸãã°äžèšã§ã¯ã
$request->merge(é
å)
ã䜿ãããšã§ããªã¯ãšã¹ããªããžã§ã¯ãã«ããããã£ãè¿œå ããããã«ãŠã§ã¢ããã³ã³ãããŒã©ã«ããŒã¿ãæž¡ããŠããã
// ããã«ãŠã§ã¢
class HelloMiddleware
{
public function handle($request, Closure $next)
{
$additionalData = [
['name'=>'taro', 'mail'=>'taro@dot.com'],
['name'=>'hanako', 'mail'=>'hanako@dot.com'],
];
$request->merge(['additionalData' => $additionalData]);
return $next($request);
}
}
// ã³ã³ãããŒã©
class HelloController extends Controller
{
public function index(Request $request) {
return view('hello.index', ['mydata'=>$request->additionalData]);
}
}
# ã¬ã¹ãã³ã¹ãä¿®æ£ãã
ã¬ã¹ãã³ã¹ãªããžã§ã¯ããæäœããããšã§ãã³ã³ãããŒã©ã®äœæããã¬ã¹ãã³ã¹å 容ã«æãå ããããšãã§ããã
class HelloMiddleware
{
public function handle($request, Closure $next)
{
$response = $next($request);
$content = $response->content();
$content = '<h1>some addition to the response</h1>'.$content;
$response->setContent($content);
return $response;
}
}
# ã°ããŒãã«ããã«ãŠã§ã¢
ããã«ãŠã§ã¢ããåå¥ã®ã³ã³ãããŒã©ã§ã¯ãªããã¢ããªã±ãŒã·ã§ã³å šäœã«é©çšããæ¹æ³
// app/Http/Kernel.php
protected $middleware = [
\App\Http\Middleware\HelloMiddleware::class,
];
# ããã«ãŠã§ã¢ã°ã«ãŒã
è€æ°ã®ããã«ãŠã§ã¢ããŸãšããŠæ±ãæ¹æ³
// app/Http/Kernel.php
protected $middlewareGroups = [
'myMiddlewares' => [
\App\Http\Middleware\HelloMiddleware1::class,
\App\Http\Middleware\HelloMiddleware2::class,
],
];
// routes/web.php
Route::get('/hello', 'HelloController@index')
->middleware('myMiddlewares');
# ããªããŒã·ã§ã³
ããªããŒã·ã§ã³ã®èšå®æ¹æ³ã®åã«ãããªããŒã·ã§ã³é¢ä¿ã§å ±éããäºé ãèšèŒããŠããã
# ãšã©ãŒã®è¡šç€º
$error
ããªããŒã·ã§ã³ã«å€±æããæã«ããšã©ãŒãæ ŒçŽããããªããžã§ã¯ã$error->all()
ãã¹ãŠã®ãšã©ãŒãé åã«ããŠåãåã
// Template
@if (count($errors) > 0)
@foreach($errors->all() as $error)
<p>{{ $error }}</p>
@endforeach
@endif
# ç¹å®é ç®ã®ãšã©ãŒã衚瀺
$error->has(é ç®å)
ç¹å®é ç®ã«ãšã©ãŒããããã©ããã確èªãã$error->first(é ç®å)
ç¹å®é ç®ã®æåã®ãšã©ãŒãæååã§ååŸ$error->get(é ç®å)
ç¹å®é ç®ã®ãã¹ãŠã®ãšã©ãŒãé åã§ååŸ
@if($errors->has('email'))
// æåã®ãšã©ãŒã ããååŸ
<p>{{ $errors->first('email')}}</p>
// åã¯é
åã§ååŸ
@foreach($errors->get('email') as $error)
<p>{{ $error }}</p>
@endforeach
@endif
# å ¥åå€ã®ä¿æ
ããªããŒã·ã§ã³åŸãå€ãä¿æããŠããã«ã¯ãold(é
ç®å)
ã value å±æ§ã«èšå®ããã
ãªããããªããŒã·ã§ã³ãæåããå Žåã«ã¯old()
ã«ã¯äœãå€ã¯å
¥ããªãã
// Template
name:<input type="text" name="name" value="{{old('name')}}">
mail:<input type="text" name="mail" value="{{old('mail')}}">
age:<input type="text" name="age" value="{{old('age')}}">
# ããªããŒã·ã§ã³ã«ãŒã«
å ¬åŒããã¥ã¡ã³ã (opens new window)ãåç §
# ããªããŒã·ã§ã³(ã³ã³ãããŒã©ã® validate ã¡ãœããã䜿ãæ¹æ³)
# åºæ¬çãªã»ããã¢ãã
// Template
<p>{{ $msg }}</p>
<form action="/hello" method="POST">
name:<input type="text" name="name">
mail:<input type="text" name="mail">
age:<input type="text" name="age">
<input type="submit" value="send">
</form>
// Controller
class HelloController extends Controller
{
public function index(Request $request) {
return view('hello.index', ['msg'=>'ãã©ãŒã ãå
¥åïŒ']);
}
public function post(Request $request) {
$validate_rule = [
'name' => 'required',
'mail' => 'email',
'age' => 'numeric|between:0,150',
];
$this->validate($request, $validate_rule);
return view('hello.index', ['msg'=>'æ£ããå
¥åãããŠããŸã']);
}
}
- POST ã«å¯Ÿå¿ããã¢ã¯ã·ã§ã³(
post()
)å ã§ã$this->validate(ãªã¯ãšã¹ã,ã«ãŒã«)
ãåŒã¶ããšã§ããªããŒã·ã§ã³ãè¡ãã - ããªããŒã·ã§ã³ã«å€±æããå Žåã¯èªåçã« GET ã«å¯Ÿå¿ããã¢ã¯ã·ã§ã³(
index()
)ãåŒã°ããã
# ããªããŒã·ã§ã³(FormRequest ã䜿ãæ¹æ³)
åè¿°ã®æ¹æ³ã¯ã³ã³ãããŒã©ãçŽæ¥ããªããŒã·ã§ã³æ©èœãåŒã³åºããŠãããããŸãã¹ããŒãã§ãªããLaravel ã«ã¯ãFormRequest ãšãã Request ã¯ã©ã¹ãç¶æ¿ããã¯ã©ã¹ããããããã䜿ãããšã«ãããã³ã³ãããŒã©ã®å段ã§èªåçã«ããªããŒã·ã§ã³ãå®è¡ããããšãã§ããã
php artisan make:request HelloRequest
// app/Http/Requests/HelloRequest.php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class HelloRequest extends FormRequest
{
// ãã®FormRequestãå©çšã§ãããã¹ãéå®ããŠãã
public function authorize()
{
if($this->path() == 'hello') return true;
return false;
}
public function rules()
{
return [
'name' => 'required',
'mail' => 'email',
'age' => 'numeric|between:0,150',
];
}
}
// Controller
class HelloController extends Controller
{
public function index(Request $request) {
return view('hello.index', ['msg'=>'ãã©ãŒã ãå
¥åïŒ']);
}
// Requestã§ã¯ãªãHelloRequestã«ãã
public function post(HelloRequest $request) {
return view('hello.index', ['msg'=>'æ£ããå
¥åãããŠããŸã']);
}
}
# ã¡ãã»ãŒãžã®ã«ã¹ã¿ãã€ãº
FormRequest ã¯ã©ã¹ã®messages()
ã¡ãœããããªãŒããŒã©ã€ãããããšã§ããšã©ãŒã¡ãã»ãŒãžãã«ã¹ã¿ãã€ãºã§ããã
class HelloRequest extends FormRequest
{
public function messages()
{
return [
'name.required' => 'ååãå
¥åããŠãã ãã',
'mail.email' => 'ã¡ãŒã«ã¢ãã¬ã¹ã®åœ¢åŒãæ£ãããããŸãã',
'age.numeric' => '幎霢ã¯æŽæ°ã§å
¥åããŠãã ãã',
'age.between' => '幎霢ãæ£ãããããŸãã',
];
}
}
# ããªããŒã·ã§ã³(ããªããŒã¿ãªããžã§ã¯ããäœæããæ¹æ³)
äžèšã®ããšãè¡ãããå Žåã¯ãããªããŒã¿ãªããžã§ã¯ããäœæããã
- ãšã©ãŒæã« GET ããŒãžã«ãªãã€ã¬ã¯ããããå¥ã®åŠçãè¡ããã
- ãã©ãŒã ã®å€ä»¥å€ã§ããªããŒã·ã§ã³ãããå Žå
// Controller
use Validator;
class HelloController extends Controller
{
public function post(Request $request) { // FormRequestã§ã¯ãªãã®ã§æ³šæ
$rules = [
'name' => 'required',
'mail' => 'email',
'age' => 'numeric|between:0,150',
];
$messages = [
'name.required' => 'ååãå
¥åããŠãã ãã',
'mail.email' => 'ã¡ãŒã«ã¢ãã¬ã¹ã®åœ¢åŒãæ£ãããããŸãã',
'age.numeric' => '幎霢ã¯æŽæ°ã§å
¥åããŠãã ãã',
'age.between' => '幎霢ãæ£ãããããŸãã',
];
// POSTããŒã¿ãå«ãå
šãŠã®ããŒã¿ïŒ$request->all() = é£æ³é
åïŒãæž¡ããŠãã
$validator = Validator::make($request->all(), $rules, $messages);
if($validator->fails()) {
return redirect('/hello')
->withErrors($validator)
->withInput();
}
return view('hello.index', ['msg'=>'æ£ããå
¥åãããŠããŸã']);
}
}
Validator::make(ãã§ãã¯ãããå€ã®é å, ã«ãŒã«ã®é å[, ã¡ãã»ãŒãžã®é å])
validator ãäœæãã$validator->fails()
ããªããŒã·ã§ã³ã倱æãã©ãã$validator->passes()
ããªããŒã·ã§ã³ãæåãã©ããredirect(ãªãã€ã¬ã¯ãå ã®ãã¹)
ãªãã€ã¬ã¯ããã->withErrors($validator)
ãšã©ãŒïŒ$errors
ïŒãšãšãã«ãªãã€ã¬ã¯ã->withInput()
å ¥åå 容ïŒold()
ïŒãšãšãã«ãªãã€ã¬ã¯ã->with(ã»ãã·ã§ã³å€ååãã»ãã·ã§ã³å€)
session å€ãšãšãã«ãªãã€ã¬ã¯ã
# ã¯ãšãªã«ããªããŒã¿ãé©çšãã
ããªããŒã¿ãªããžã§ã¯ããå¿çšããã°ãã¯ãšãªãæ€èšŒããããšãå¯èœã
class HelloController extends Controller
{
public function index(Request $request) {
$validator = Validator::make($request->query(), [ // queryïŒé£æ³é
åïŒãæž¡ããŠãã
'id' => 'required',
'pass' => 'required',
]);
/* do something */
}
}
# ããªããŒã¿ã«åçã«ã«ãŒã«ãè¿œå ãã
ãã©ãŒã ãžã®å ¥åå 容ãªã©ã«ãã£ãŠåçã«ã«ãŒã«ãå€æŽãããå Žåã¯ãäžèšã䜿ãã ç¡åé¢æ°ã®è¿å€ãfalse ã®å Žåã«é©çšãããã®ã§æ³šæã
$validator->sometimes(é
ç®å, ã«ãŒã«, é©çšãããªãfalseãè¿ãç¡åé¢æ°)
class HelloController extends Controller
{
public function post(Request $request) {
$rules = [
'name' => 'required',
'mail' => 'email',
'age' => 'numeric|between:0,150',
];
$validator = Validator::make($request->all(), $rules);
$validator->sometimes('age', 'min:0', function($input){
return !is_int($input->age);
});
$validator->sometimes('age', 'max:150', function($input){
return !is_int($input->age);
});
/* do something */
}
}
# ããªããŒã·ã§ã³(Validator ã¯ã©ã¹èªäœãäžæžãããæ¹æ³)
ãªãªãžãã«ã®æ€èšŒã«ãŒã«ãäœããããšãã«äœ¿ãæ¹æ³ã
ïŒïŒãªãªãžãã«ã®ããªããŒã¿ãŒã¯ã©ã¹ãäœãããã®äžã«æ€èšŒã«ãŒã«ãäœã
// app/Http/Validators/HelloValidator.php (Validatorsã®ãã©ã«ãåã¯äœã§ãOK)
namespace App\Http\Validators;
use Illuminate\Validation\Validator;
class HelloValidator extends Validator
{
// validate*** ã® *** ã®éšåãã«ãŒã«åã«ãªãïŒãã®å Žå'hello'ïŒ
public function validateHello($attribute, $value, $parameters)
{
return $value % 2 === 0;
}
}
ïŒïŒãµãŒãã¹ãããã€ãã䜿ã£ãŠã¢ããªèµ·åæã«ããªããŒã¿ãäžæžããã
// ãµãŒãã¹ãããã€ã
use Illuminate\Validation\Validator;
use App\Http\Validators\HelloValidator;
class HelloServiceProvider extends ServiceProvider
{
// ã¢ããªã±ãŒã·ã§ã³ã®èµ·åæã«è¡ãããåŠç
public function boot()
{
$validator = $this->app['validator'];
$validator->resolver(function(...$args){
return new HelloValidator(...$args);
});
}
}
ïŒïŒæ€èšŒã«ãŒã«ãèšå®ãã
$rules = [
'name' => 'required',
'mail' => 'email',
'age' => 'numeric|between:0,150|hello',
];
# ãªãªãžãã«ã®æ€èšŒã«ãŒã«
ãªãªãžãã«ã®æ€èšŒã«ãŒã«ãäœãããããValidator ã¯ã©ã¹èªäœãäžæžãããã»ã©ã§ã¯ãªããšããå Žåã¯ã
Validator::extend(ã«ãŒã«å, ç¡åé¢æ°)
ã䜿ãããæ軜ã
// ãµãŒãã¹ãããã€ã
use Validator;
class HelloServiceProvider extends ServiceProvider
{
public function boot()
{
Validator::extend('hello', function($attribute, $value, $parameters, $validator){
return $value % 2 == 0;
});
}
}
# ãã®ä»
# CSRF 察çãç¡å¹ã«ãã
Kernel.php
=>$middlewareGroups
=>web
ã®äžããäžèšã®è¡ãåé€
\App\Http\Middleware\VerifyCsrfToken::class,
# CSRF 察çãäžéšã®ã«ãŒãã§ç¡å¹ã«ãã
app\Http\Middleware\VerifyCsrfToken.php
ã«äžèšãè¿œå ããã
protected $except = [
'hello', // somedomain.com/helloãã§ç¡å¹å
'hello/*', // somedomain.com/hello/ããã®ãã¹ä»¥äžã®ãã¹ãŠã®ããŒãžã§ç¡å¹å
];
# ã¯ãããŒã®èªã¿æžã
$request->hasCookie(ã¯ãããŒå)
æå®ããã¯ãããŒã®æç¡ãååŸãã$request->cookie(ã¯ãããŒå)
æå®ããã¯ãããŒãååŸãã$response->cookie(ã¯ãããŒå, å€, ä¿åæé)
æå®ããã¯ãããŒãã»ãããã
class HelloController extends Controller
{
// ã¯ãããŒã®ååŸ
public function index(Request $request) {
if($request->hasCookie('my-cookie-name'))
{
$msg = $request->cookie('my-cookie-name');
}
/* do something */
}
// ã¯ãããŒã®ä¿å
public function post(HelloRequest $request) {
// äžåºŠãResponseã€ã³ã¹ã¿ã³ã¹ãçæããå¿
èŠããã
$response = new Response(view('hello.index'));
$response->cookie('my-cookie-name', $request->someValue, 100);
return $response;
}
}
# ãªãã€ã¬ã¯ã
redirect(ãã¹)
=> RedirectResponse ãšãããªããžã§ã¯ããè¿ãredirect()
=> Redirector ãšãããªããžã§ã¯ããè¿ã
RedirectResponse ã®äœ¿ãæ¹
->withInput()
ãã©ãŒã ã®å€ãä»äžãããŸãŸãªãã€ã¬ã¯ã->withErrors(<MessageProvider>)
ãšã©ãŒã¡ãã»ãŒãžãä»äžããŠãªãã€ã¬ã¯ã->withCookie(cookieã®é å)
Cookie ãä»äžããŠãªãã€ã¬ã¯ãïŒ
Redirector ã®äœ¿ãæ¹
->route(ã«ãŒãå, æž¡ãããŒã¿ã®é å)
ã«ãŒãåã®éšåã¯/hello
ãªã©->action(ã¢ã¯ã·ã§ã³, æž¡ãããŒã¿ã®é å)
ã¢ã¯ã·ã§ã³ã®éšåã¯'SomeController@index'
ãªã©->view(ãã¥ãŒå)
ãã¥ãŒãæå®ããŠãªãã€ã¬ã¯ã->json(ããã¹ã)
JSON ããŒã¿ãè¿ã->download(ãã¹)
ãã¡ã€ã«ãããŠã³ããŒã->file(ãã¹)
ãã¡ã€ã«ã衚瀺
# ããŒã¿ããŒã¹
Laravel ã®ããŒã¿ããŒã¹æäœã«ã¯ããã€ãã®æ¹æ³ãããã
- DB ã¯ã©ã¹ã䜿ãïŒSQL çŽæã¡ïŒ
- DB ã¯ã©ã¹ã䜿ãïŒã¯ãšãªãã«ãïŒ
- Eloquent(Object-Relational Mapping)
# æºå
- sqlite ã®ã€ã³ã¹ããŒã«ïŒsystem32 ãã©ã«ãã«ããŠã³ããŒããã dll ãã¶ã¡ããïŒ
- DB browser for sqlite ã®ã€ã³ã¹ããŒã«
# DB ãšã®æ¥ç¶
æ¥ç¶ã®èšå®ã¯config/database.php
ã§è¡ããããããããèšå®é
ç®ã®å€ãã¯env()
ãã«ãã䜿ã£ãŠç°å¢å€æ°ããèªã¿èŸŒãŸããç°å¢å€æ°ããªãå Žåã ãããã©ã«ãå€ïŒenv()
ã®ç¬¬äºåŒæ°ïŒãæå®ãã圢åŒãªã£ãŠããããã®ãããå®éã®èšå®ã¯.env
ãã¡ã€ã«åã¯ç°å¢å€æ°ã«ãããŠè¡ãã»ããããã
äŸãã°ãsqlite ã®å Žåã®èšå®ã¯äžèšã®éã
DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
DB_DATABASE="C:\Users\SomeUser\Desktop\my-first-laravel\database\database.sqlite"
# DB_USERNAME=homestead
# DB_PASSWORD=secret
# DB ã¯ã©ã¹
# select
// ã³ã³ãããŒã©
use Illuminate\Support\Facades\DB;
class HelloController extends Controller
{
public function index(Request $request) {
$items = DB::select('select * from people');
// or ãã©ã¡ãŒã¿çµåã䜿ãå Žå
$params = ['id' => 1234];
$items = DB::select('select * from people where id = :id', $params);
}
}
# insert
/hello/add
ã« GET ã§ã¢ã¯ã»ã¹ããå Žåã¯ããã©ãŒã ã衚瀺/hello/add
ã« POST ã§ã¢ã¯ã»ã¹ããå Žåã¯ãDB ã«ããŒã¿ãè¿œå ããŠãªãã€ã¬ã¯ã
// ã«ãŒãã£ã³ã°
Route::get('/hello/add', 'HelloController@showAddForm');
Route::post('/hello/add', 'HelloController@addNewData');
// ã³ã³ãããŒã©
class HelloController extends Controller
{
public function showAddForm(Request $request) {
return view('hello.add');
}
public function addNewData(Request $request) {
$param = [
'name' => $request->name,
'mail' => $request->mail,
'age' => $request->age,
];
DB::insert('insert into people (name, mail, age) values (:name, :mail, :age)', $param);
return redirect('/hello');
}
}
<!-- ãã³ãã¬ãŒã -->
<form action="/hello/edit" method="post">
@csrf
<div>name:<input type="text" name="name" /></div>
<div>mail:<input type="text" name="mail" /></div>
<div>age:<input type="text" name="age" /></div>
<input type="submit" value="send" />
</form>
# update
- /hello/edit ã« GET ã§ã¢ã¯ã»ã¹ããå Žåã¯ãããŒã¿ãååŸããã®ã¡ããã©ãŒã ã衚瀺
- /hello/update ã« POST ã§ã¢ã¯ã»ã¹ããå Žåã¯ãããŒã¿ãæŽæ°ããŠãªãã€ã¬ã¯ã
// ã«ãŒãã£ã³ã°
Route::get('/hello/edit', 'HelloController@showEditForm');
Route::post('/hello/update', 'HelloController@updateData');
old('mail', $form->mail)
ãšããããšã§ãããªããŒã·ã§ã³å€±ææã«å€ãä¿æããŠããããšãã§ããã第äºåŒæ°ã¯ããã©ã«ãå€ã§ãããåå衚瀺ã®éã«äœ¿çšãããã
// ã³ã³ãããŒã©
class HelloController extends Controller
{
public function showEditForm(Request $request) {
$param = ['id' => $request->id];
$item = DB::select('select * from people where id = :id', $param);
return view('hello.edit', ['form' => $item[0]]);
}
public function updateData(Request $request) {
$param = [
'id' => $request->id,
'name' => $request->name,
'mail' => $request->mail,
'age' => $request->age,
];
DB::update('update people set name = :name, mail = :mail, age = :age where id = :id', $param);
return redirect('/hello');
}
}
<!-- ãã³ãã¬ãŒã -->
<form action="/hello/edit" method="post">
@csrf
<!-- IDãä¿æããŠããå¿
èŠãã -->
<input type="hidden" name="id" value="{{$form->id}}" />
<div>
name:<input
type="text"
name="name"
value="{{ old('name', $form->name) }}"
/>
</div>
<div>
mail:<input
type="text"
name="mail"
value="{{ old('mail', $form->mail) }}"
/>
</div>
<div>
age:<input type="text" name="age" value="{{ old('age', $form->age) }}" />
</div>
<input type="submit" value="send" />
</form>
# delete
- /hello/delete ã« GET ã§ã¢ã¯ã»ã¹ããå Žåã¯ãããŒã¿ãååŸããã®ã¡ããã©ãŒã ã衚瀺
- /hello/delete ã« POST ã§ã¢ã¯ã»ã¹ããå Žåã¯ãããŒã¿ãåé€ããŠãªãã€ã¬ã¯ã
// ã«ãŒãã£ã³ã°
Route::get('/hello/delete', 'HelloController@showDeleteForm');
Route::post('/hello/delete', 'HelloController@removeData');
// ã³ã³ãããŒã©
class HelloController extends Controller
{
public function showDeleteForm(Request $request) {
$param = ['id' => $request->id];
$item = DB::select('select * from people where id = :id', $param);
return view('hello.delete', ['form' => $item[0]]);
}
public function removeData(Request $request) {
$param = [
'id' => $request->id,
];
DB::delete('delete from people where id = :id', $param);
return redirect('/hello');
}
}
<!-- ãã³ãã¬ãŒã -->
<form action="/hello/edit" method="post">
@csrf
<!-- IDãä¿æããŠããå¿
èŠãã -->
<input type="hidden" name="id" value="{{$form->id}}" />
<div>name: {{$form->name}}</div>
<div>mail: {{$form->mail}}</div>
<div>age: {{$form->age}}</div>
<input type="submit" value="send" />
</form>
# ã¯ãšãªãã«ã
DB::table()
ã¯ãIlluminate\Database\Query\Builder
ã¯ã©ã¹ãè¿ãã
ããã䜿ãããšã§ããŒãã«ã®æäœãè¡ããã
# select
// ã³ã³ãããŒã©
// è€æ°ä»¶ãååŸ
$items=DB::table('people')->get();
$items=DB::table('people')->get(['id', 'name']); // ã«ã©ã ãæå®ããå Žå
// æåã®1件ãååŸ
$items=DB::table('people')->first();
// æåã®3件ãååŸ
$items=DB::table('people')->take(3);
// æ¡ä»¶ãæå®ããŠååŸ
$items=DB::table('people')->where('id', $request->id)->get();
$items=DB::table('people')->where('id', $request->id)->first();
$items=DB::table('people')->where('id', '<=', $request->id)->get();
$items=DB::table('people')->where('name', 'like', '%John%')->get();
// æ¡ä»¶æå®ïŒANDïŒ
$items=DB::table('people')->where()->where()->get();
// æ¡ä»¶æå®ïŒorïŒ
$items=DB::table('people')->where()->orWhere()->get();
// æ¡ä»¶æå®(æ¡ä»¶ãé
åã§æå®)
$items=DB::table('people')->whereRaw('age >= ? and age <= ?', [10, 20])->get();
// 䞊ã¹æ¿ã
$items=DB::table('people')->orderBy('name', 'desc')->get();
// ããŒãžããŒã·ã§ã³ãªã©
$items=DB::table('people')->offset($page * 10)->limit(10)->get();
# insert
$param = [
'name' => $request->name,
'mail' => $request->mail,
'age' => $request->age,
];
DB::table('people')->insert($param);
# update
$param = [
'id' => $request->id,
'name' => $request->name,
'mail' => $request->mail,
'age' => $request->age,
];
$item = DB::table('people')->where('id', $request->id)->update($param);
# delete
DB::table('people')->where('id', $request->id)->delete();
# ãã€ã°ã¬ãŒã·ã§ã³
ãã€ã°ã¬ãŒã·ã§ã³ïŒ DB ã®ããŒãžã§ã³ç®¡çæ©èœã®ããšãé圢ã«åºã¥ã㊠DB ãäœæããããåé€ãããããã
䜿çšã§ãã Column ã«ã€ããŠã¯å ¬åŒããã¥ã¡ã³ã (opens new window)ãåç §
# Eloquentã®ã¢ãã«äœææã«ããã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ãäžç·ã«äœæããæ¹æ³
php artisan make:model Person -m
# ãã€ã°ã¬ãŒã·ã§ã³ãåäœã§äœæããæ¹æ³
php artisan make:migration create_people_table # people ãããŒãã«åã«ãªã
// database/migration/****_create_people_table.php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePeopleTable extends Migration
{
// ããŒãã«çæã®åŠç
public function up()
{
Schema::create('people', function (Blueprint $table) {
$table->increments('id'); // primary key
$table->string('name');
$table->string('mail');
$table->integer('age');
$table->timestamps(); // created_at, updated_at
});
}
// ããŒãã«åé€ã®åŠç
public function down()
{
Schema::dropIfExists('people');
}
}
touch database/database.sqlite # 空ã®DBãã¡ã€ã«ãäœæ
php artisan migrate
php artisan migrate:fresh # ãã¹ãŠã®ããŒãã«ãããããããŠåäœæ
# ããšããã«ã©ã ãè¿œå ãã
class AddUserIdToPosts extends Migration
{
public function up() {
Schema::table('posts', function (Blueprint $table) {
// åãè¿œå ãã
$table->integer('user_id');
});
}
public function down() {
Schema::table('posts', function (Blueprint $table) {
// ããŒã«ããã¯æã«ã¯åãåé€ãã
$table->dropColumn('user_id');
});
}
}
# ã·ãŒãã£ã³ã°
ã·ãŒãã£ã³ã°ïŒã·ãŒãïŒæåããçšæããŠããã¬ã³ãŒãïŒãäœæããæ©èœ
php artisan make:seeder PeopleTableSeeder
// database/seeds/PeopleTableSeeder
use Illuminate\Support\Facades\DB;
class PeopleTableSeeder extends Seeder
{
public function run()
{
$param = [
'name' => 'taro',
'mail' => 'taro@taro.com',
'age' => 18,
];
DB::table('people')->insert($param);
/* å¿
èŠã«å¿ããŠæŽã«ããŒã¿ãè¿œå ãã */
}
}
ã·ãŒãã£ã³ã°ã§å®è¡ããããã¡ã€ã«ã¯database/seeds/DatabaseSeeder.php
ãªã®ã§ãããã«äœæããã·ãŒããŒãè¿œèšããŠããã
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(PeopleTableSeeder::class);
}
}
php artisan db:seed
# Eloquent ORM
ORM ïŒ DB ã®ããŒã¿ããã¯ã©ã¹ããªããžã§ã¯ãã®åœ¢åŒã§æ±ããããã«ããããã«ãPHP ãš DB ãæ©æž¡ãããä»çµã¿
# ã»ããã¢ãã
php artisan make:controller PersonController # è€æ°åœ¢ã«ããããš
php artisan make:model Person # åæ°åœ¢ã«ããããš
# or
php artisan make:model Person -m # -mãã€ãããšãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ãçæããã
// ã«ãŒãã£ã³ã°
Route::get('/person', 'PersonController@index');
# ã¢ãã«ã¯ã©ã¹
ã¢ãã«ãš DB ãçŽä»ããããã®åçš®èšå®ããã¢ãã«ã䟿å©ã«æ±ãããã®ã¯ã©ã¹ã®æ¡åŒµãããã§è¡ãã
ORM ãš DB ã¯ã©ã¹ãšã®çžéç¹ã¯ãããŒã¿ãåãªãé åã§ã¯ãªãPerson ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ããç¹ã§ããããã®ãããã¯ã©ã¹ãæ¡åŒµããã°ã€ã³ã¹ã¿ã³ã¹ã®æ¯ãèããæ¡åŒµããããšãã§ããã
// app/Person.php
class Person extends Model
{
// ããŒãã«åãæåèšå®ããå Žå(default: ã¢ãã«åã®è€æ°åœ¢)
protected $table = 'people';
// primary keyãæåèšå®ããå Žå(default: id)
public $primaryKey = 'id';
// Auto Increment ãªé
ç®ãªã©ãå€ããµãŒãåŽã§
// èšå®ããããããã£ã¯ããã§æ瀺ããŠããããš
protected $guarded = ['id'];
// ã¢ãã«ã¯ã©ã¹ã®ã¹ã¿ãã£ãã¯ããããã£ã«ããªããŒã·ã§ã³ã«ãŒã«ã
// æã£ãŠãããšåŸã
䟿å©
public static $rules = [
'name' => 'required',
'mail' => 'email',
'age' => 'integer|min:0|max:150',
];
// ã¯ã©ã¹ã®æ¡åŒµ
public function getData() {
return $this->id.':'.$this->message.'('.$this->url.')';
}
}
# ããŒã¿ã®ååŸ
# all()
$items = Person::all();
å
šä»¶ååŸã§ãããPerson::all()
ã¯ãIlluminate\Database\Eloquent\Collection
ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãè¿ãããã®ã€ã³ã¹ã¿ã³ã¹ã¯é
åãšåãããã«æ±ãããšãã§ããã
# find()
$item = Person::find($request->id);
id ãã£ãŒã«ããæå®ã®å€ã§ããããŒã¿ã 1 件ã ãååŸããã
ãªãããã id ãã£ãŒã«ãã®ååãid
ã§ãªãå Žåã¯ãã¢ãã«ã¯ã©ã¹ã«$primaryKey
ãšããããããã£ãçšæããããã«ãã£ãŒã«ãåãèšå®ããããšã
# where()
$items = Person::where('name', 'John')->get();
$items = Person::where('name', 'John')->first();
where()
ã¯Illuminate\Database\Eloquent\Builder
ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãè¿ããDB ã¯ã©ã¹ã®ã¯ãšãªãã«ããšã¯ç°ãªããã®ã®ãæ©èœã¯ã»ãšãã©åãã
# ãã®ä»
ãã®ä»ã®ã¡ãœããã¯åºæ¬çã«ã¯ãšãªãã«ããšåãã
# ã¹ã³ãŒã
- ã¹ã³ãŒãïŒäºãèšå®ããŠãããæ€çŽ¢æ¡ä»¶ã®é圢ãçµã¿åãããŠäœ¿ãããšãã§ããã
- ã°ããŒãã«ã¹ã³ãŒããšããŒã«ã«ã¹ã³ãŒãããã
# ããŒã«ã«ã¹ã³ãŒã
scope****
ãšããååã§å®çŸ©ããã
// ã¢ãã«
class Person extends Model
{
public function scopeNameEqual($query, $str) {
return $query->where('name', $str);
}
public function scopeAgeGreaterThan($query, $age) {
return $query->where('age', '>', $age);
}
public function scopeAgeLessThan($query, $age) {
return $query->where('age', '<', $age);
}
}
- 䜿ããšãã¯
scope
ã¯ã€ããªãã - è€æ°ã®ã¹ã³ãŒããçµã¿åãããå Žåã¯ããã§ãŒã³ããŠäœ¿ãã
// ã³ã³ãããŒã©
$item = Person::nameEqual('taro')
->ageGreaterThan(10)
->ageLessThan(20)
->where('mail', 'taro@taro.com')
->first();
# ã°ããŒãã«ã¹ã³ãŒã(ç¡åé¢æ°ã§æå®)
ç¹ã«æå®ããªããŠãã¹ã³ãŒããé©çšãããå Žåã¯ãã°ããŒãã«ã¹ã³ãŒãã䜿ãã
ã°ããŒãã«ã¹ã³ãŒãã¯ãã¢ãã«ã¯ã©ã¹ã®boot()
ãšããéçã¡ãœããå
éšã§å®çŸ©ããã
// ã¢ãã«
use Illuminate\Database\Eloquent\Builder;
class Person extends Model
{
protected static function boot(){
parent::boot();
static::addGlobalScope('age', function (Builder $builder){
$builder->where('age', '>', 17);
});
}
}
# ã°ããŒãã«ã¹ã³ãŒã(ã¹ã³ãŒãã¯ã©ã¹ã§æå®)
- åé ãã¹ã³ãŒãã¯ã©ã¹ã§è¡ãæ¹æ³ã
- Scope ã¯ã©ã¹ã¯ã
apply()
ãã¡ã³ã¯ã·ã§ã³ããã€Scope
ã€ã³ã¿ãŒãã§ãŒã¹ãå®è£ ããã
// app/Scopes/ScopePerson.php (Scopesã®ãã©ã«ãåã¯ãªãã§ãOK)
namespace App\Scopes;
use Illuminate\Database\Eloquent\Scope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
class ScopePerson implements Scope{
public function apply(Builder $builder, Model $model) {
$builder->where('age', '>', 10);
}
}
// ã¢ãã«
class Person extends Model
{
protected static function boot(){
parent::boot();
static::addGlobalScope(new ScopePerson());
}
}
# ããŒã¿ã®è¿œå ã»æŽæ°ã»åé€
以äžãCreate, Update, Delete ã®ããæ¹ãèšèŒããªãããã³ãã¬ãŒãã®ã³ãŒãã¯DB ã¯ã©ã¹ã䜿ã£ãã³ãŒããšã»ãŒåããªããèšèŒçç¥ã
# è¿œå
// ã«ãŒãã£ã³ã°
Route::get('/person/add', 'PersonController@add');
Route::post('/person/add', 'PersonController@create');
class PersonController extends Controller
{
public function add(Request $request) {
return view('person.add');
}
public function create(Request $request) {
// ããªããŒãããã倱æãããGETã®ã«ãŒãã£ã³ã°ã«ãªãã€ã¬ã¯ããããã
$this->validate($request, Person::$rules);
// ã€ã³ã¹ã¿ã³ã¹ãäœæ
$person = new Person();
// ãæ¹æ³1ããªã¯ãšã¹ãã«ã¶ãäžãã£ãŠããå€ãã€ã³ã¹ã¿ã³ã¹ã«ã»ããããDBã«ä¿å
$postData = $request->all();
$person->fill($postData)->save();
// ãæ¹æ³2ããªã¯ãšã¹ãã®ããŒã¿ãã€ã³ã¹ã¿ã³ã¹ã«äžã€ãã€ã»ããããŠãDBã«ä¿å
$person->name = $request->name;
$person->mail = $request->mail;
$person->age = $request->age;
$person->save();
return redirect('/hello');
}
}
# æŽæ°
// ã«ãŒãã£ã³ã°
Route::get('/person/edit', 'PersonController@edit');
Route::post('/person/edit', 'PersonController@update');
// ã³ã³ãããŒã©
class PersonController extends Controller
{
public function edit(Request $request) {
$person = Person::find($request->id);
return view('person.edit', ['form'=> $person]);
}
public function update(Request $request) {
// ããªããŒãããã倱æãããGETã®ã«ãŒãã£ã³ã°ã«ãªãã€ã¬ã¯ããããã
$this->validate($request, Person::$rules);
// ã€ã³ã¹ã¿ã³ã¹ãäœæ
$person = Person::find($request->id);
// ãªã¯ãšã¹ãã«ã¶ãäžãã£ãŠããå€ãã€ã³ã¹ã¿ã³ã¹ã«ã»ããããDBã«ä¿å
$postData = $request->all();
$person->fill($postData)->save();
return redirect('/person');
}
}
# åé€
// ã«ãŒãã£ã³ã°
Route::get('/person/delete', 'PersonController@delete');
Route::post('/person/delete', 'PersonController@remove');
// ã³ã³ãããŒã©
class PersonController extends Controller
{
public function delete(Request $request) {
$person = Person::find($request->id);
return view('person.delete', ['form'=> $person]);
}
public function remove(Request $request) {
$person = Person::find($request->id)->delete();
return redirect('/person');
}
}
# ãªã¬ãŒã·ã§ã³
- äž»ããŒãã«ïŒããŒãæäŸããããŒãã«ã顧客ãã¹ã¿ãªã©ãïŒ
- åŸããŒãã«ïŒããŒãå©çšããããŒãã«ã顧客蚪åå±¥æŽãªã©ãïŒ
# hasOne
ããŒãã«ã 1:1 ã§çµã³ã€ãé¢ä¿ã
äŸïŒperson ãš board ã 1:ïŒã§çµã³ã€ãå Žå
class Person extends Model
{
// åæ°åœ¢
public function board() {
return $this->hasOne('App\Board');
}
}
class Board extends Model
{
protected $guarded = ['id'];
public static $rules = [
'person_id' => 'required', // ãããŒãã«å_idããšããããŒã§èªåçã«çµã³ä»ãããã
'title' => 'required',
'message' => 'required',
];
}
// personã€ã³ã¹ã¿ã³ã¹ããã¯ã`board()`ã§ã¯ãªã`board`ã®ããã«ããããã£ã§ã¢ã¯ã»ã¹ã§ããã
// hasOneã®å Žåã¯ããªããžã§ã¯ããè¿ãã
echo $person->board->message;
# hasMany
ããŒãã«ã 1:N ã§çµã³ã€ãé¢ä¿ã
äŸïŒperson ãš board ã 1:N ã§çµã³ã€ãå Žå
class Person extends Model
{
// è€æ°åœ¢
public function boards() {
return $this->hasMany('App\Board');
}
}
class Board extends Model
{
protected $guarded = ['id'];
public static $rules = [
'person_id' => 'required', // ãããŒãã«å_idããšããããŒã§èªåçã«çµã³ä»ãããã
'title' => 'required',
'message' => 'required',
];
}
// personã€ã³ã¹ã¿ã³ã¹ããã¯ã`board()`ã§ã¯ãªã`board`ã®ããã«ããããã£ã§ã¢ã¯ã»ã¹ã§ããã
// hasManyã®å Žåã¯ããªããžã§ã¯ãã§ã¯ãªãã€ãã©ãã«ãè¿ãã
@foreach ($person->boards as $board)
echo $board->message;
@endforeach
# belongsTo
åŸããŒãã«ããäž»ããŒãã«ãååŸããæã«äœ¿ããhasOne ã hasMany ãšã¯éã®æ¹åã
äŸïŒboard ã person ã«æå±ããå Žå
class Board extends Model
{
public static $rules = [
'person_id' => 'required', // ãã®ããŒãåºã«ã«ãã¯ã¢ãããã
'title' => 'required',
'message' => 'required',
];
public function person() {
return $this->belongsTo('App\Person');
}
public function getData() {
// $this->personã§äž»ããŒãã«ã«ã¢ã¯ã»ã¹ã§ãã
return $this->id.': '.$this->title.'('.$this->person->name.')';
}
}
# ãªã¬ãŒã·ã§ã³ã®æç¡ã§æ€çŽ¢ãã
äŸãã°ãperson ãš board ã 1:N ã§é¢é£ããå ŽåãboardïŒãªã¬ãŒã·ã§ã³ïŒãæ〠person ãšããã§ãªã person ãçãŸããããããæ€çŽ¢ããããã®äŸ¿å©ãªã¡ãœãããšããŠãhas()
ãšdoesntHave()
ãããã
Person::has('boards')->get(); // => iterable
Person::doesntHave('boards')->get(); // => iterable
# with ã«ãã Eager ããŒãã£ã³ã°
åè¿°ã®äŸã¯ãå®ã¯ããŸãå¹ççã§ãªããããšãã°ãPerson::all()
ãšããæããPerson ãååŸããã®åŸ Person1 件ããšã«ãé¢é£ä»ãããã Board ãååŸããšããåäœã«ãªã£ãŠãããïŒN+1 åé¡ïŒ
ãããããPerson ãååŸããã®åŸãé¢é£ãã Board ã 1 åã§ååŸããšããæ¹æ³ã«ããã«ã¯ãall()
ã®æ¿ããã«with()
ã䜿ãã
Person::with('boards')->get();
# Tinker
ã¢ãã«ã䜿ã£ãŠ DB ãæäœã§ãããREPL ã®ãããªãã®ã
php artisan tinker
App\Post::count()
App\Post::all()
App\Post::where('title','Post1')->get()
App\Post::find($id)->delete()
$post = new App\Post()
$post->title = 'Post1'
$post->body = 'Post1 body'
$post->save()
# ããã³ããšã³ã
# ã»ããã¢ãã
äžèšã³ãã³ããå®è¡ããããšã§ãresouces/js|css
ã®ãã¡ã€ã«ãã³ã³ãã€ã«ãããpublic/js|css
ã«é
眮ãããã
yarn
yarn dev # 1åããã®ã³ã³ãã€ã«
yarn watch # ãã¡ã€ã«ã®å€æŽãç£èŠããŠã³ã³ãã€ã«
# CSS
- Laravel ã®ãããžã§ã¯ãã«ã¯ã
resources/sass
ã®äžã«æšæºã§ CSS ãçšæãããŠããã - ãã® CSS 㯠Bootstrap ãå å ããŠããã
- Pagination ã®ããŒããªã©ã¯ãã® CSS ãã¡ã€ã«ãåæã«ã§ã¹ã¿ã€ãªã³ã°ãããŠããã
<head>
<link rel="stylesheet" href="{{ asset('css/app.css') }}" />
</head>
# ãã®ä»
# RESTful API
# ã»ããã¢ãã
ãŸãã--api
ãªãã·ã§ã³ã«ãããRESTful ãªã³ã³ãããŒã©ãäœæãããïŒéã«ãResourceful ãªã³ã³ãããŒã©ãäœããšãã¯--resource
ãªãã·ã§ã³ã䜿ãïŒ
REST API ã§ã¯ create()ã edit()ã¢ã¯ã·ã§ã³ã¯å¿ èŠãªãã®ã§äœæãããªãã
php artisan make:controller RestappContoller --api
äœæããã³ã³ãããŒã©ãRoute::apiResources()
ã«æž¡ããRoute::resources()
ãšç°ãªããcreate()ã edit()ã®ããã®ã«ãŒãã¯å¿
èŠãªãã®ã§äœæãããªãã
Route::apiResources('/rest', 'RestappController');
# ããŒã¿ã®ååŸ
- laravel ã§ã¯ãã¢ã¯ã·ã§ã³å ã§é å | Eloquent ã® Collection | ã¢ãã«ã®ã€ã³ã¹ã¿ã³ã¹ã Return ãããšèªåçã« JSON ã«å€æããŠã¯ã©ã€ã¢ã³ãã«è¿ããŠãããã
- ãã£ãŠãåã« DB ãæ€çŽ¢ã㊠Return ããŠããã° OK
class RestappController extends Controller
{
public function index()
{
return Restdata::all();
}
public function show($id)
{
return Restdata::find($id);
}
}
# ããŒã¿ã®è¿œå
class RestappController extends Controller
{
public function create()
{
return view('rest.create');
}
public function store(Request $request)
{
$restdata = new Restdata();
$postData = $request->all();
$restdata->fill($postData)->save();
return redirect('/rest');
}
}
# ããŒã¿ã®æŽæ°ã»åé€
çç¥
# ã»ãã·ã§ã³
- ã¯ã©ã€ã¢ã³ãåŽã«ã»ãã·ã§ã³ ID ãããµãŒãåŽã« ID ã«çŽã¥ãããŒã¿ãä¿åããããšã§ããŠãŒã¶ãèå¥ããæ¹æ³
- ãµãŒãåŽã®ä¿åææ³ã«ã¯ããã¡ã€ã«å©çšïŒããã©ã«ãã
storage/framework/sessions
ïŒãã¡ã¢ãªå©çšãããŒã¿ããŒã¹å©çšãªã©ããã€ãã®æ¹æ³ãããã
// ä¿å
$request->session()->put('msg', $msg);
// ååŸ
$request->session()->get('msg');
// ã»ãã·ã§ã³æ
å ±ãšãšãã«ãªãã€ã¬ã¯ã
redirect('/home')->with('somekey', 'somevalue');
# ä¿åå ã DB ã«ãã
ã»ãã·ã§ã³ã®èšå®ã¯config/session.php
ã§è¡ããããå€ãã®èšå®ã¯ env ãã«ãã«ããç°å¢å€æ°ããèªã¿èŸŒãŸããããã.env
ãç·šéããã
// .env
SESSION_DRIVER=database
session çšã®ããŒãã«ãäœæãããæäœæ¥ã§ã¯ãªãããã€ã°ã¬ãŒã·ã§ã³ã䜿çšããã
php artisan session:table # ãã€ã°ã¬ãŒã·ã§ã³ãã¡ã€ã«ã®äœæ
php artisan migrate # ãã€ã°ã¬ãŒã·ã§ã³ã®å®è¡
# ãªãã€ã¬ã¯ãæã«ã»ãã·ã§ã³ã䜿ã£ãŠãšã©ãŒã¡ãã»ãŒãžã衚瀺ãã
äžèšã®ãããªãã¡ãã»ãŒãžè¡šç€ºçšã³ã³ããŒãã³ããäœæããŠããã¹ã¿ãŒã¬ã€ã¢ãŠãã« include ããŠããã
// 'success'ãšããã»ãã·ã§ã³å€ããã£ãå Žå
@if (session('success'))
<div class="alert alert-success">
{{ session('success')}}
</div>
@endif
// 'error'ãšããã»ãã·ã§ã³å€ããã£ãå Žå
@if (session('error'))
<div class="alert alert-danger">
{{ session('error')}}
</div>
@endif
// ã€ãã§ã«ãããªããŒã·ã§ã³å€±ææã®ãšã©ãŒã衚瀺
@if (count($errors) > 0)
@foreach ($errors->all() as $error)
<div class="alert alert-danger">{{ $error }}</div>
@endforeach
@endif
ã³ã³ãããŒã©ã§ãªãã€ã¬ã¯ãããéã«ã»ãã·ã§ã³å€ãã»ããããã
return redirect('/posts')
->with('success', 'Post Created');
# ããŒãžããŒã·ã§ã³
all()
ãwith()
ã®æ¿ããã«ãsimplePaginate()
ã䜿ãããšã§ç°¡åã«ããŒãžããŒã·ã§ã³ãå®è£
ã§ããã
//ã³ã³ãããŒã©
// DBã¯ã©ã¹ã®å Žå
$items=DB::table('people')->simplePaginate(5);
$items=DB::table('people')->orderBy('age')->simplePaginate(5);
// ã¢ãã«ã®å Žå
$items=Person::simplePaginate(4);
$items=Person::orderBy('age')->simplePaginate(4);
return view('hello.index', ['items'=>$items]);
// ãã³ãã¬ãŒã
// ååŸããŒãžãžã®ãªã³ã¯ãèªåçæãã
{{ $items->links() }}
// ååŸããŒãžãžã®ãªã³ã¯ã«å¯ŸããŠè¿œå ã®ã¯ãšãªãä»äžãããå Žåã¯ãappends()ãæã
{{ $items->appends(['sort'=>$sort])->links() }} // => /some?sort=***&page=2
# ããŒãžçªå·ã®ãªã³ã¯ã衚瀺ãã
simplePaginate()
ã®æ¿ããã«paginate()
ã䜿ããšãèªåçæããããªã³ã¯ã«ãããŒãžçªå·ãå«ãŸããæ§ã«ãªãã
# ã«ã¹ã¿ã ã¬ã€ã¢ãŠã
links()
ã«åŒæ°ãæå®ããããšã§ãããŒãžããŒã·ã§ã³ã®ã¬ã€ã¢ãŠããã«ã¹ã¿ã ã§ããã- äžèšã³ãã³ãã§é圢ãçæã§ããã®ã§ãé©å®å©çšãã
php artisan vendor:publish --tag=laravel-pagination
# ãŠãŒã¶èªèšŒ
# ã»ããã¢ãã
äžèšã³ãã³ãã§é¢é£ãããã¡ã€ã«ãèªåçæããããçæããããã¡ã€ã«ãããŒã¹ã«ç§»æ€ãè¡ããšãæ軜ã§ããã
php artisan make:authã# é¢é£ããRoute, View, Controllerãçæãã
php artisan migrate # èªèšŒã«äœ¿ãããŒãã«ãäœæ
Auth::user()
ãŠãŒã¶æ
å ±ã®ååŸ
// ã³ã³ãããŒã©
use Illuminate\Support\Facades\Auth;
class HelloController extends Controllerã{
$user = Auth::user();
return view('some.view', ['user' => $user]);
}
Auth::check()
ãŠãŒã¶ããã°ã€ã³ããŠãããã©ãã
// ãã³ãã¬ãŒã
@if (Auth::check())
<p>{{$user->name}}</p>
<p>{{$user->email}}</p>
@else
<p>ãã°ã€ã³ããŠããŸãã</p>
<a href="/login">ãã°ã€ã³</a>|
<a href="/register">ç»é²</a>
@endif
# æ¢è£œã®ã«ãŒãã£ã³ã°
/login
ãã°ã€ã³/register
ãµã€ã³ã¢ãã/home
ã¢ã«ãŠã³ã管çç»é¢
# ãã°ã€ã³ã®åŒ·å¶
auth
ãšããããã«ãŠã§ã¢ã䜿ãããšã§ãç¹å®ã®ã«ãŒããé²èŠ§ããéã«ãã°ã€ã³ãç¯æ£ã§ããããã°ã€ã³ããŠããªãå Žåã¯/login
ã«ãªãã€ã¬ã¯ããããã
Route::get('/hello', 'HelloController@index')
->middleware('auth');
# ãã°ã€ã³ã®ããã¥ã¢ã«å®è£
if(Auth::attempt(['email'=>$mail, 'password'=>$pass])){
/* on success */
} else {
/* on fail */
}
# ãŠããããã¹ã
ãã¹ãã¯TestCase
ã¯ã©ã¹ãç¶æ¿ããã¯ã©ã¹ã«å¯Ÿããtest****()
ãšããã¡ãœãããå®è£
ããããšã§è¡ãã
# èšå®ãã¡ã€ã«
ãã¹ãã«é¢ããèšå®ã¯phpunit.xml
ã§è¡ãã
<!-- ãã¹ãçšã®ããŒã¿ããŒã¹ãæå® -->
<env name="DB_DATABASE" value="database\database_test.sqlite"/>
# ãããŒã¬ã³ãŒãã®çšæ
ãã¹ãçšã®ãããŒããŒã¿ãäœæããã«ã¯ãdatabase/factories
ã®äžã«ãã Model Factories ã䜿ãã
// database/factories/ModelFactory.php
$factory->define(App\Person::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->safeEmail,
'age' => $faker->random_int(1,99),
];
});
# ãã¹ããã¡ã€ã«ã®äœæãšå®è¡
php artisan make:test HelloTest
// tests/Feature/HelloTest.php
class HelloTest extends TestCase
{
public function testExample()
{
$this->assertTrue(true);
$arr = [];
$this->assertEmpty($arr);
$msg = 'Hello';
$this->assertEquals($msg, 'Hello');
$number = random_int(0, 100);
$this->assertLessThan(100, $number);
}
}
vendor/bin/phpunit
# ã«ãŒãã£ã³ã°ãèªèšŒããã¹ããã
class HelloTest extends TestCase
{
use DatabaseMigrations;
public function testExample()
{
// ã«ãŒãã£ã³ã°ã®ãã¹ã
$response = $this->get('/');
$response->assertStatus(200);
$response = $this->get('/hello');
$response->assertStatus(302);
$response = $this->get('/no_routes');
$response->assertStatus(404);
// èªèšŒã®ãã¹ã
$user = factory(User::class)->create();
$response = $this->actingAs($user)->get('/hello');
$response->assertStatus(200);
}
}
factory(ã¯ã©ã¹å)->create()
ãã¡ã¯ããªã®èšå®ãåºã«ã€ã³ã¹ã¿ã³ã¹ãçæã㊠DB ã«ä¿åãã
# ããŒã¿ããŒã¹ã®ãã¹ã
class HelloTest extends TestCase
{
// ãã¹ãéå§åã«ãã€ã°ã¬ãŒã·ã§ã³ãå®è¡ããçµäºåŸã«ãŸã£ããã«æ»ã
use DatabaseMigrations;
public function testExample()
{
$dummyPerson = [
'name' => 'XXX',
'mail' => 'YYY@ZZZ.COM',
'age' => 123,
];
// ãã¡ã¯ããªã®èšå®ãäžéšäžæžãããŠã€ã³ã¹ã¿ã³ã¹ãçæãDBã«ä¿å
factory(Person::class)->create($dummyPerson);
// æå®ããæ°ã®ã€ã³ã¹ã¿ã³ã¹ãDBã«ä¿å
factory(Person::class, 10)->create();
$this->assertDatabaseHas('people', $dummyPerson);
}
}
# ç°å¢å€æ°ãã³ã³ãã£ã°ã®ååŸ
ã³ã³ãããŒã©ããã³ãã¬ãŒãå ã§ãç°å¢å€æ°çãååŸããæ¹æ³
// ç°å¢å€æ°ã®ååŸ
env('APP_NAME');
// ã³ã³ãã£ã°ã®ååŸ
config('app.name');
# WYSIWYG
WYSIWYG ãšãã£ã¿ãå®è£ ããã«ã¯ãckeditor (opens new window)ãšãç¡å®³åã®ããã® Purifier (opens new window)ãçµã¿åãããã
chkeditor ã®èšå®
<textarea name="sometextarea"></textarea>
<script src="https://cdn.ckeditor.com/4.11.1/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace('sometextarea');
</script>
Purifier ã®èšå®
composer require mews/purifier
php artisan vendor:publish --provider="Mews\Purifier\PurifierServiceProvider"
ã³ã³ãããŒã©
use Mews\Purifier\Facades\Purifier;
// .....
Purifier::clean($request->someInputValue);