0
0

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 3 years have passed since last update.

Laravel6|コントローラーからViewへの値の渡し方

0
Last updated at Posted at 2021-12-09

配列としてviewに渡す

配列としてセット

controller.php
$user=Auth::user();
$data=['user_id'=>$user->id, 'user_name'=>$user->name];

return view('create',$data);
view.php
{{ $user_id }}//1234
{{ $user_name }}//hogehoge

compactでviewに渡す

compact — 変数名とその値から配列を作成する

$flowers = compact('sakura', 'ume', 'himawari');

下記で書いたものと同じ意味になる

$flowers = array(
  'sakura' => $sakura,
  'ume' => $ume,
  'himawari' => $himawari,
);

なので

controller.php
$user=Auth::user();

return view('create',compact('user'));
view.php
{{ $user['id'] }}//1234
{{ $user['name']}}//hogehoge

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?