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

【PHP】Laravel 学習メモ/ ControllerからViewへ値を渡す方法

Last updated at Posted at 2019-11-03

#ControllerからViewへ値を渡す方法

//SampleController.php

<?php
public function index(){
  $data["name"] = "Tom";
  $data["age"] = "10";
  return view('sample.index', $data);
}
?>

または、

//SampleController.php

<?php
public function index(){
  return view('sample.index', ['name' => 'Tom']);
}
?>

###compact関数を使用

//SampleController.php
<?php
public function index(){
  $name = "Tom";
  $age  = "10";
  return view('sample.index', compact('name', 'age'));
}
?>

###withメソッドを使用

/SampleController.php

<?php
public function index(){
  return view('sample.index')->with('name', 'Tom');
}
?>

※学習中の為、間違い等ありましたらご指摘お願いいたします。

1
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
1
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?