10. Lấy dữ liệu input từ chuỗi truy vấn (Retriveing input data from query string)

https://viblo.asia/p/tap-16-request-laravel-aWj534Y8K6m

Trong khi method input dùng để lấy dữ liệu từ input trong request hiện tại (bao gồm cả query string) thì method query chỉ lấy giá trị từ query string.

Các bạn thay đổi blade view form như sau:

<form action="/post?id=1" method="POST">
    @csrf
    
    <div>
        <button type="submit">Submit</button>
    </div>
</form>

Các bạn thấy mình đã truyền query string id=1 cho action. Để lấy query id, tại method post của FormController ta thực hiện cú pháp sau:

public function store(Request $request)
{
    dd($request->query('id'));
}

Và đây là kết quả:

Last updated