Bài 12: Truyền tham số từ Controller sang View – Học lập trình Laravel

Trang chủ » Training » Bài 12: Truyền tham số từ Controller sang View – Học lập trình Laravel
28/02/2022 Training 175 viewed
Để truyền dữ liệu ra View (Passing Data to Views) chúng ta có các cách như sau:

1. Dùng compact()

Cú pháp:
compact('tencuabientruyenvao')
Ví dụ:
Route::get('chao/{user}', function ($user) {
    return view('hello-user', compact('user'));
});
// view muon hien thi thi goi bien $user

2. Dùng with()

Cú pháp:
view('tenview')->with('key', 'value');
Ví dụ:
Route::get('chao/{user}', function ($user) {
    return view('hello-user')->with('user', $user);
});

//de hien thi trong view goi bien $user

3. Dùng mảng

Cú pháp:
view('tenview', ['key' => 'value']);
Ví dụ:
Route::get('chao/{user}', function ($user) {
    return view('hello-user', ['user' => $user]);
});
Chia sẻ:
Tags:
TOP HOME