ãLaravelãModelãããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãããï¼
PHPãã¼ã¸ã§ã³ï¼7.1.14
Laravelãã¼ã¸ã§ã³ï¼5.7.9
ä»åã¯MVCã®ã¢ãã«ã«ã¤ãã¦ã§ãï¼
MVCã®Cã§ããControllerã®è¨äºã¯こちら
MVCã®Vã§ããViewã®è¨äºã¯こちら
ååã¾ã§ã§DBã®ãã¼ãã«ä½æï¼Migrationï¼ã¨ãã¼ã¿ç»é²ï¼Seedingï¼ãè¡ãã¾ããï¼
ã¾ãArticleã®Controllerã¨Viewãä½æãã¾ããï¼
ã¨ããããã§ä»åã¯ãã¢ãã«ãããã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãããã¨æãã¾ãï¼
ã¢ãã«ãä½æãã
ã¾ãã¯artisanã³ãã³ãã§Articleã¨ããååã®ã¢ãã«ãä½æãã¾ãï¼
$ php artisan make:model Models/Article Model created successfully.
ãã¡ãã®ã³ãã³ãã§ã¢ãã«ãä½æãããã¨ãã§ãã¾ãã
Modelsã®é¨åã¯ãã£ã¬ã¯ããªã表ãã¦ãã¾ãã
Laravelã¯ããã©ã«ãã§ã¯ã¢ãã«ç¨ã®ãã£ã¬ã¯ããªã¯ç¨æãã¦ããã¾ããã
ï¼ã¢ãã«ã®å®ç¾©ãã¦ã¼ã¶ã¼ã«ãã£ã¦å¤§ããéãããä½æç®æã¯ã¦ã¼ã¶ã¼ã«å§ãã¦ãããï¼
ãã¦ããã§ã¯Articleã¢ãã«ã確èªãã¦ã¿ã¾ãããï¼
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Article extends Model { // }
app>Models>Article.php
Modelã¯ã©ã¹ãç¶æ¿ããArticleã¨ããååã®ã¯ã©ã¹ã®ã¿ã§ãã·ã³ãã«ã§ãã
ã¢ãã«åã¯ãã¼ã¿ãã¼ã¹ãã¼ãã«ã®åæ°å½¢ã«ããã¨ã
Laravelãèªåçã«å¤å®ãã¦DBã¨Modelãç¹ãã¾ãã
ï¼"articles"ãã¼ãã«ãªã"Article"ã¢ãã«ï¼
ãããã¯"$table"å¤æ°ã«ãã¼ã¿ãã¼ã¹ãã¼ãã«åãæå®ããã¨ã
Modelã¨DBãç¹ãããã¨ãã§ãã¾ãã
Articleã¯ã©ã¹ãç¶æ¿ãã¦ããModelã¯ã©ã¹ã®å¤æ°ã
ãªã¼ãã¼ã©ã¤ãããå½¢ã¨ãªãã¾ãã
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Article extends Model { protected $table = 'special_articles'; }
ä¸ã®ããã«ããã¨ã
DBå
ã«ãã'special_articles'ãã¼ãã«ã¨Articleã¢ãã«ãç¹ããã¾ãã
以ä¸ã§Modelãå©ç¨ãã¦ãã¼ã¿ãã¼ã¹ã«ã¢ã¯ã»ã¹ãããã¨ãã§ãã¾ãã
ArticleControllerãç·¨é
ç¶ãã¦ä»¥åä½æããEventControllerãç·¨éãã¾ãï¼
<?php namespace App\Http\Controllers; // ***** éå§ ***** use App\Models\Article; // ***** çµäº ***** use Illuminate\Http\Request; class ArticleController extends Controller { function show() { // ***** éå§ ***** $articles = Article::all(); return view('article.show', ['articles' => $articles]); // ***** çµäº ***** } }
app>Http>Controllers>ArticleController.php
ã¾ãã¯use宣è¨ã«å
ã»ã©ä½æããArticleã¢ãã«ã®ãã¹ãè¨è¿°ãã¾ãã
ãããå¿ããã¨Controllerå
ã§Articleã¢ãã«ã使ããã¨ãã§ãã¾ããã
ç¶ãã¦showã¡ã½ããã§ãã
Articleã¢ãã«ã®éçã¡ã½ããã¨ãã¦ãallã¡ã½ããã使ã£ã¦ãã¾ãã
allã¡ã½ããã¯articlesãã¼ãã«ã«ç»é²ããã¦ããå
¨ã¬ã³ã¼ããåå¾ãã¾ãã
Article::allã¡ã½ããã¯Laravelã®Eloquentã¨ããæ©è½ã®ä¸ã«ç¨æããã¦ãã¾ãã
Eloquentã®ãããã§ãããã¯ã°ã©ã¦ã³ãããã«ãããã¨ãªã
éçã¡ã½ããã®ããã«ä½¿ããã¨ãåºæ¥ã¾ãã
Eloquentã«ã¤ãã¦ã¯å¥è¨äºã§ãç´¹ä»ããäºå®ã§ãã
æå¾ã«viewã¡ã½ããã§ãã
第ä¸å¼æ°ã«'article.show'ã¨æ¸ããã¦ããã®ã¯ãã£ã¬ã¯ããªæ§æã表ãã¦ãã¾ãã
resources>view以ä¸ã«articleãã£ã¬ã¯ããªãä½æãã
ãã®ä¸ã«show.blade.phpãã¡ã¤ã«ãä½æãã¾ãããã
第ï¼å¼æ°ã«ã¯åå¾ããarticlesãã¼ã¿ãé åã«æ¸¡ãã¾ãã
Viewãç·¨é
ç¶ãã¦Viewãç·¨éãã¾ãï¼
<!doctype html> <html> <head> <title>Article/Show</title> </head> <body> <h1>Article/Show</h1> @foreach($articles as $a) <p>{{$a->title}} : {{$a->body}}</p> @endforeach </body> </html>
resources>views>article>show.blade.php
渡ãããarticlesé
åãã¼ã¿ã@foreachã§åãåºãã¦ãã¾ãã
Laravelã®bladeã¯ãã®ããã«foreachæãªã©phpã®æ©è½ãç°¡æ½ã«è¨è¿°ãããã¨ãã§ãã¾ãã
routeãç·¨é
æå¾ã«routeã§ããweb.phpãã¡ã¤ã«ãç·¨éãã¦çµäºã§ãï¼
<?php Route::get('/', function () { return view('welcome'); }); // ***** éå§ ***** Route::get('article/show', 'ArticleController@show'); // ***** çµäº *****
routes>web.php
Migrationã¨Seedingã«ãã£ã¦MySQLã«ç»é²ãããã¼ã¿ãModelã§åãåºãã
Viewã§è¡¨ç¤ºãããã¨ã«æåãã¾ããï¼
æ¬æ¥ã¯ããã¾ã§ã§ãããããã¨ããããã¾ããã