Laravel 5.2使用.blade视图显示表格

时间:2017-05-09 11:36:30

标签: php laravel laravel-5.2 blade laravel-blade

我有我的数据库,已经迁移了所有内容,我的代码可以在需要时在我的表中记录新条目,等等。

我的表格内容为:' id',' name',' lastname',' email',&# 39;电话','地址'。

我想要的是,当您点击提交按钮时,您将被重定向到一个新的视图.blade.php'您可以在其中查看所有DB条目,包括最新的条目。但我不知道如何开发此代码以使其正常工作。

我需要帮助。 谢谢。

3 个答案:

答案 0 :(得分:6)

在控制器功能中,在将新记录存储到表后立即执行此操作。 假设您的模型名称为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>

答案 1 :(得分:0)

// 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', 'YourController@index')->name('showForm');
Route::post('/submitForm', 'YourController@index')->name('submitForm');
Route::get('/showAllusers', 'YourController@index')->name('showAllusers');

?>

答案 2 :(得分:0)

我应编写此代码以显示所有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>