9. Lấy dữ liệu của một input (Retriveing an input data)

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

Vẫn giữ nguyên blade view form, tại method post của FormController ta dump lệnh sau:

public function post(Request $request)
{
    dd($request->input('username'));
}

Để có thể lấy dữ liệu của một input nào đó trong request hiện tại, ta chỉ cần sử dụng method input với tham số là tên của input cần lấy trong HTML. Kết quả thu được là:

Ngoài ra, bạn có thể gán giá trị mặc định cho một input nào đó nếu như nó không xác định trong request hiện tại. Chẳng hạn lấy form trên thì mình chẳng có cái input nào tên là remember cả. Nhưng mình muốn kể cả khi không nó không tồn tại thì vẫn có được giá trị là true. Để làm thế, mình chỉ cần thêm tham số thứ hai của method input là giá trị mặc định mà mình muốn gán cho nó.

public function post(Request $request)
{
    dd($request->input('remember', true));
}

Nếu bạn làm việc với các array input, sử dụng ký hiệu . để tham chiếu đến các phần tử của nó. Chẳng hạn giờ mình sẽ thay đổi blade view form như sau:

<form action="/post" method="POST">
    @csrf
    
    <div>
        Name: <input type="text" name="products[][name]">
        Price: <input type="text" name="products[0][price]">
    </div>
    <div>
        Name: <input type="text" name="products[][name]">
        Price: <input type="text" name="products[1][price]">
    </div>

    <br>

    <div>
        <button type="submit">Submit</button>
    </div>
</form>

Đây là form mô phỏng đăng các sản phẩm lên shop online. Như bạn thấy, các thông tin của sản phẩm đều chứa name là products dạng mảng.

Giờ ta thử dump data của input products này bằng cách:

public function store(Request $request)
{
    dd($request->input('products'));
}

Chúng ta sẽ nhận được một mảng dữ liệu trả về của các input sau khi nhập thử dữ liệu:

Nếu bạn muốn lấy thông tin của sản phẩm có index 0 thì bạn sử dụng cú pháp tham chiếu sau:

$request->input('products.0'); // Lấy toàn bộ thông tin sản phẩm có index "0"

$request->input('products.0.name'); // Lấy name của sản phẩ có index "0"

Nếu bạn chỉ muốn lấy name của tất cả sản phẩm trong products thì có thể là như sau:

$request->input('products.*.name');

Ví dụ đã hoàn thành:

C:\xampp\htdocs\blog\routes\web.php

Route::post('/store', 'FormController@store');

C:\xampp\htdocs\blog\resources\views\form.blade.php

<form action="/store" method="POST">
    @csrf
    <div>
      Name: <input type="text" name="products[][name]">
      Price: <input type="text" name="products[0][price]">
    </div>
    <div>
      Name: <input type="text" name="products[][name]">
      Price: <input type="text" name="products[1][price]">
    </div>
    <br>
    <div>
        <button type="submit">Submit</button>
    </div>
</form>

C:\xampp\htdocs\blog\app\Http\Controllers\FormController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class FormController extends Controller {
  public function show() {
	  return view('form');
	}
	public function post(Request $request) {
	  dd($request->input('username'));
	}
	public function store(Request $request) {
	    dd($request->input('products'));
	}
}

Last updated