dongxing7530
2017-05-09 11:36
浏览 594
已采纳

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

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'.

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.

I need help with that, please. Thank you.

图片转代码服务由CSDN问答提供 功能建议

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

我的表格内容为:'id','name','lastname','email','phone','address'。 \ n

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

我需要帮助。 谢谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douci1677 2017-05-09 11:51
    已采纳

    In your controller function do this right after you store a new record to table. assuming your model name is User

    fetch all user records from User model,

    $users =  App\User::all();
    

    pass this users variable to view(assuming your view.blade.php is in resources/views folder)

    return View::make('view', compact('users'));
    

    and then in your view.blade.php you can do something like this to display all user records.

    <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>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dougou5852 2017-05-09 11:54

    //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');
    
    ?>
    
    评论
    解决 无用
    打赏 举报
  • duanbing8817 2017-05-09 12:12

    I should write this code for display all users table data :

    //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>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题