Laravel 5.2 使用 .blade 视图显示表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43868766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2 Showing a table using a .blade view
提问by G.Felicio
I have my DB, already migrated everything, my code can record new entries in my table when required, and so on.
我有我的数据库,已经迁移了所有内容,我的代码可以在需要时在我的表中记录新条目,等等。
I have a table with the contents of: 'id', 'name', 'lastname', 'email', 'phone', 'address'.
我有一个表格,内容为:“id”、“name”、“lastname”、“email”、“phone”、“address”。
What I want is, when you hit the submit button, you'll be redirected to a new 'view.blade.php' where you can see all of the DB's entries, including the most recent. BUT I don't know how to develop this code to make it work.
我想要的是,当您点击提交按钮时,您将被重定向到一个新的“view.blade.php”,在那里您可以看到所有数据库的条目,包括最新的条目。但我不知道如何开发此代码以使其工作。
I need help with that, please. Thank you.
我需要帮助,请。谢谢你。
回答by Sapnesh Naik
In your controller function do this right after you store a new record to table.
assuming your model name is User
在您的控制器函数中,在您将新记录存储到表后立即执行此操作。假设您的模型名称是User
fetch all user records from User model,
从 User 模型中获取所有用户记录,
$users = App\User::all();
pass this users variable to view(assuming your view.blade.php
is in resources/views
folder)
传递这个用户变量来查看(假设你view.blade.php
在resources/views
文件夹中)
return View::make('view', compact('users'));
and then in your view.blade.phpyou can do something like this to display all user records.
然后在你的 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>
回答by Mihir Bhende
//form.blade.php
//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
//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
<?php
Route::get('/showForm', 'YourController@index')->name('showForm');
Route::post('/submitForm', 'YourController@index')->name('submitForm');
Route::get('/showAllusers', 'YourController@index')->name('showAllusers');
?>
回答by AddWeb Solution Pvt Ltd
I should write this code for display all users
table data :
我应该编写此代码来显示所有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
视图.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>