本例用到的数据库:

    1. //本例用到的表
    2. create table tb_user(
    3. id int primary key auto_increment,
    4. userName varchar(30),
    5. passWord varchar(30),
    6. tel varchar(30),
    7. email varchar(30),
    8. addres varchar(30)
    9. );
    10. //自行添加的数据
    11. insert into tb_user(userName,passWord,tel,email,addres) values('123456789','helloWord','12345678912','test@qq.com','上海');
    12. insert into tb_user(userName,passWord,tel,email,addres) values('987654321','helloWord','11122233344','test@qq.com','北京');

    开始之前我们需要前往 .env 配置好链接的数据库
    VKGXT(C2G$1BR)VWP(]}NWQ.png

    Controller层(控制层)
    创建Controller:php artisan make:Controller User\UserController —resource
    (ps:这里文创建的Controller 是在后面 加上 —resource的)

    1. <?php
    2. //我们需要先导入Model层模型
    3. use App\Models\UserModel;
    4. public function index()
    5. {
    6. //
    7. $dataModel = new UserModel();
    8. $dataTable = $dataModel->getAll();
    9. return view("User/index")->with('data',$dataTable);
    10. }

    View层(视图层)
    新建一个 index 视图页面

    1. <!doctype html>
    2. <html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    3. <head>
    4. </head>
    5. <body>
    6. <div align="center">
    7. <table>
    8. <tr>
    9. <td>id</td>
    10. <td>userName</td>
    11. <td>passWord</td>
    12. <td>tel</td>
    13. <td>email</td>
    14. <td>addres</td>
    15. </tr>
    16. @foreach ($data as $content)
    17. <tr>
    18. <td>{{$content->id}}</td>
    19. <td>{{$content->userName}}</td>
    20. <td>{{$content->passWord}}</td>
    21. <td>{{$content->tel}}</td>
    22. <td>{{$content->email}}</td>
    23. <td>{{$content->addres}}</td>
    24. </tr>
    25. @endforeach
    26. </table>
    27. </div>
    28. </body>
    29. </html>

    Routes层(路由层)

    1. <?php
    2. //配置Rotes
    3. Route::resource('user','User\UserController');

    Model层(模型层)

    1. //Model层代码
    2. <?php
    3. namespace App\Models;
    4. use Illuminate\Database\Eloquent\Model;
    5. class UserModel extends Model
    6. {
    7. protected $table = 'tb_user';//获取的表
    8. protected $primaryKey = 'id';//对应自增的id
    9. protected $fillable = ['userName','passWord','tel','email','addres']; //对应列名
    10. public $timestamps = false;//数据库中没有设置时间戳,所以将他的值设为false
    11. public function getAll(){
    12. return self::all();
    13. }
    14. }

    最终效果
    image.png