2
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 1 year has passed since last update.

VIEWからControllerに値を渡す方法~備忘録~

Posted at

viewからcontrollerに値を渡す方法

①formを利用する場合

input typeをhiddenにすることによって隠しデータとしてフォーム送信

[VIEW]~~.blade.php

<form action="{{route('store')}}" method="POST">
    <div class="form-group">
       <input type="hidden" name="member_id" class="form-control" id="member_id" value="{{$member_id}}">
    </div>
<button type="submit"></button>
</form>
[Route]web.php

Route::post('/store', [HomeController::class, 'store'])->name('store');
[Controller]Homecontroller.php

public function store(Request $request)
{
  $posts = $request->all();
  $member_id = $posts['member_id'];
}

②URLを利用する場合

1.ルートパラメータの利用

member_idの数字が書いたリンクを踏むことで、その値をcontrollerに渡す

[VIEW]~~.blade.php

<a href=/{{$member_id}}>{{$member_id}}</a>
[Route]web.php

Route::get{'/{member_id}', 'UserController@index');
[Controller]Homecontroller.php

public function index($member_id)
{
    $data = $member_id;
    ~~~~
}

2.クエリパラメーターの利用

member_idの数字が書いたリンクを踏むことで、その値をcontrollerに渡す

[VIEW] ~~.blade.php

<a href="/?id={{$member_id}}"> {{$member_id}}</a>
[Route]web.php

Route::get('/', [HomeController::class, 'index'])->name('log');
[Controller]Homecontroller.php

 public function index(Request $request)
{
  $member_id=$request->id;
}

複数渡したい場合は下のように'&'でつなげる。

[VIEW] ~~.blade.php
<a href="/?id={{$member_id}}&name=1234"> {{$member_id}}</a>

3.ルートパラメーターとクエリパラメーターの併用

もっと便利なやり方があれば教えてください> <

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