dayjournal memo

Total 975 articles!!

Laravel #004 - モデルを作成

Yasunori Kirimoto's avatar

画像



モデルを作成するメモ。


モデル新規作成。


php artisan make:model Sample

画像



内容はとりあえすそのまま。


./app/


Sample.php


<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Sample extends Model
{
    //
}


DBにsamplesテーブルを作成しておく。

画像



コントローラ修正。


./app/Http/Controllers/


SampleAppController.php


<?php

namespace App\Http\Controllers;

use App\Sample;
use Illuminate\Http\Request;

class SampleAppController extends Controller
{

    public function index()
    {
        //全レコード取得
        $items = Sample::all();
        return $items;
    }

}



./routes/


web.php


<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('sample', 'SampleAppController@index');


localhost/sampleで表示確認。

画像



book

Q&A