Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Laravel でデータの受け渡し

More than 1 year has passed since last update.

次のページを参考にしました。
データの受け渡し

完成すると次のような表示がされます。
laravel_jun0502.png

1) プロジェクトの用意

laravel new sample_project
cd sample_project
php artisan make:controller HelloController

新規に作成しないで、こちらで作成したプログラムを改造することもできます。
Laravel で HelloWorld

2) app/Http/Controllers/HelloController.php の編集

app/Http/Controllers/HelloController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HelloController extends Controller
{
    //
  public function index()
  {
    // 配列の初期化
    $data = array();

    // データ格納
    $data['name'] = '鈴木';
    $data['message'] = 'こんにちは';

    // 現在日時
    date_default_timezone_set('Asia/Tokyo');
    $data['today'] = date('Y年m月d日 H:i:s');

    return view('hello', $data);
  }
}

3) resources/views/hello.blade.php の作成

resources/views/hello.blade.php
<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>My First Page</title>
</head>
<body>
  <p>{{$name}}さん</p>
  <p>{{$message}}</p>
  <p>現在は {{$today}} です</p>
Jun/05/2018<p />
</body>
</html>

4) routes/web.php の編集

最後に一行を加えます。

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('/', function () {
    return view('welcome');
});

Route::get('hello', 'HelloController@index');

5) サーバーを動かします。

php artisan serve --host 0.0.0.0

ブラウザーで、 http://localhost:8000/hello にアクセスします。

冒頭の画面が表示されます。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away