0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

コントローラからViewに値を渡す

Posted at

#ビューの作成
ビューはアプリケーションとして動作するHTMLにより構成されており、コントローラ/アプリケーションロジックをプレゼンテーションロジックから分離します。ビューはresources/viewsディレクトリに保存する。

resources/view/greeting.blade.php

<html>
  <body>
     <h1>Hello,{{$name}}</h1>
  </body>
</html>

このビューをresources/views/greeting.blade.phpとして保存しているので以下のようにグローバルviewヘルパ関数を使用し結果を返す。

Route::get(`/`,function(){
  Return view (`greeting`,[`name` => `James]);
});

viewヘルパに渡している最初の引数はresources/viewsディレクトリ中のビューファイル名に対応している。

二つ目の引数はビューで使用するデータの配列。上記の例ではビューにname変数を渡しそれをBlade記法を使用しているビューの中に表示される。

#ビューにデータを渡す

簡単にデータを配列でビューに渡せる。

return view(`greetings`,[`name` => `Victoria`]);

この方法で情報を渡す場合その情報はキー/値ペアの配列。ビューの中で各値へは対抗するキーへアクセスできる。例えば<?php echo $key; ?>のように表示可能。全データをviewヘルパ関数に渡す代わりに``withメソッドでビューに渡すデータを個別に追加することもできる。

return view(`greeting`)->with(`name`,`Victoria`);

#参照
https://readouble.com/laravel/5.7/ja/views.html

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?