我有我的數據庫,已經遷移了所有內容,我的代碼可以在需要時在我的表中記錄新條目,依此類推。Laravel 5.2使用.blade視圖顯示錶
我有一個表,其中包括'id','name','lastname','email','phone','address'。
我想要的是,當你點擊提交按鈕時,你將被重定向到一個新的'view.blade.php',你可以在其中看到所有數據庫的條目,包括最近的條目。但我不知道如何開發這個代碼來使它工作。
我需要幫助。 謝謝。
我有我的數據庫,已經遷移了所有內容,我的代碼可以在需要時在我的表中記錄新條目,依此類推。Laravel 5.2使用.blade視圖顯示錶
我有一個表,其中包括'id','name','lastname','email','phone','address'。
我想要的是,當你點擊提交按鈕時,你將被重定向到一個新的'view.blade.php',你可以在其中看到所有數據庫的條目,包括最近的條目。但我不知道如何開發這個代碼來使它工作。
我需要幫助。 謝謝。
在您的控制器功能中,在將新記錄存儲到表格後執行此操作。 假設您的型號名稱是User
從用戶模型中獲取所有用戶的記錄,
$users = App\User::all();
通過這個用戶變量查看(假設你的view.blade.php
是resources/views
文件夾)
return View::make('view', compact('users'));
然後在你的view.blade.php你可以做這樣的事情來顯示所有的用戶記錄。
<table>
<thead>
<tr>
<th> id</th>
<th> name</th>
<th> last name </th>
<th> email </th>
<th> phone</th>
<th> adddress </th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td> {{$user->id}} </td>
<td> {{$user->name}} </td>
<td> {{$user->last_name}} </td>
<td> {{$user->email}} </td>
<td> {{$user->phone}} </td>
<td> {{$user->address}} </td>
</tr>
@endforeach
</tbody>
</table>
這麼簡單。有用。 非常感謝。 = D –
在開始外泄之前,您需要經常檢查用戶數是否大於0! –
是的,Mihir。由於我的表已經有了一些信息(我正在一個單獨的程序中查看它,DBeaver),我忽略了它,但這是一個很好的輸入。謝謝。 –
//form.blade.php
<form method="post" action="{{ route('submitForm') }}">
{!! csrf_field() !!}
<input type="text" name="name"/>
<input type="text" name="last_name"/>
<input type="text" name="email"/>
<input type="text" name="phone"/>
<input type="text" name="address"/>
</form>
<?php
//app/Http/controllers/YourController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
//user is your eloquent model
class YourController extends Controller
{
//show user form
public function showForm()
{
return view('form');
}
//post url for submit form
public function submitForm(Request $request){
$this->validate($request, [
'name' => 'required|min:2|max:30',
'lastname' => 'required|min:2|max:30',
'email' => 'required',
'phone' => 'required',
'address' => 'required'
]);
$user = new User();
$user->name = $request->name;
$user->lastname = $request->lastname;
$user->email = $request->email;
$user->phone = $request->phone;
$user->address = $request->address;
try{
$user->save();
return redirect()->route('showAllusers')->with('success', "User was successfully created..!");
}
catch(Exception $e){
return redirect()->back()->with('error', "Could not save the user!");
}
return redirect()->back()->with('error', "Error Occured, please try again!");
}
// after form submit, redirect to this page where all users will be showcased
public function showAllusers()
{
$users = User::all();
return view('allusers', compact('users'));
}
}
?>
//allusers.blade.php
@if($users->count > 0)
<table>
<thead>
<tr>
<th> id</th>
<th> name</th>
<th> last name </th>
<th> email </th>
<th> phone</th>
<th> adddress </th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td> {{$user->id}} </td>
<td> {{$user->name}} </td>
<td> {{$user->last_name}} </td>
<td> {{$user->email}} </td>
<td> {{$user->phone}} </td>
<td> {{$user->address}} </td>
</tr>
@endforeach
</tbody>
</table>
@else
<p> No users found..</p>
@endif
//routes.php
<?php
Route::get('/showForm', '[email protected]')->name('showForm');
Route::post('/submitForm', '[email protected]')->name('submitForm');
Route::get('/showAllusers', '[email protected]')->name('showAllusers');
?>
它工作!謝謝! –
我應該寫此代碼用於顯示所有users
表數據:
//Function for store users data
public fucntion store(Request $request){
$insertData = DB::table('users')->insert(array(
'name'=>$request->name,
'lastname'=>$request->lastname,
'email'=>$request->email,
'phone'=>$request->phone,
'address'=>$request->address
));
return Redirect::to('/view')
}
//fucntion for display users data
public fucntion view(){
$rsltUsers = User::get();
return view('view', compact('rsltUsers'));
}
view.blade.php
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
</tr>
</thead>
<tbody>
@foreach($rsltUsers as $rsltUser)
<td>{!! $rsltUser->name !!}</td>
<td>{!! $rsltUser->lastname !!}</td>
<td>{!! $rsltUser->email !!}</td>
<td>{!! $rsltUser->phone !!}</td>
<td>{!! $rsltUser->address !!}</td>
@endforeach
</tbody>
</table>
當你點擊提交按鈕
它也有效,非常感謝。 –
@ G.Felicio太好了,很高興幫助。如果這是您的答案,請將其標記爲已接受的答案 –
你把它發送到控制器,節電細節分貝,然後再返回view.blade.php? –
觀看本教程:它是黃金。 https://laracasts.com/series/laravel-5-fundamentals – Amarnasan
是的,Sapnesh。 我點擊提交,發送數據到我的數據庫,然後我重定向到view.blade.php。 但是這個刀片是空白的,裏面沒有任何東西。我想在這裏展示我的桌子。 –