我只想使用laravel从数据库中查看记录

时间:2018-09-06 07:45:07

标签: php laravel

我只是想为学生建立一个数据库,但是当我请求查看记录页面时,它无法正常工作,给我一个错误,为什么它不起作用,我只想从数据库中查看我的记录

控制器

namespace App\Http\Controllers;
 use Illuminate\Http\Request;
 use DB;
 use App\Http\Requests;
 use App\Http\Controllers\Controller;
 class StudViewController extends Controller {
 public function index(){
 /*$users = DB::select('select * from tudent');*/
 $users = DB::table('student')->select(DB::raw('*'))->get();
 return view('stud_view',['users'=>$users]);
  }
  }

这是视图

      <html>

       <head>
       <title>View Student Records</title>
      </head>

        <body>
      <table border = 1>
     <tr>
        <td>ID</td>
        <td>Name</td>
     </tr>
      @foreach ($users as $user)
     <tr>
        <td>{{ $user->id }}</td>
        <td>{{ $user->name }}</td>
       </tr>
       @endforeach
      </table>

     </body>
     </html>

这是路线

    Route::get('/', function () {
    return view('welcome');
     });

     // ----------- insert student --------------
       Route::get('insert','StudInsertController@insertform');
       Route::post('create','StudInsertController@insert');

    // ----------- view Record --------------
       Route::get('view-records','StudViewController@index');

3 个答案:

答案 0 :(得分:2)

  1. 首先,您应该通过尊重代码缩进和变量命名来使代码更具可读性
  2. 第二,您可以使用Student模型获取DB中的所有学生。无需使用数据库外观
  3. 无需创建两个控制器['cer', 'creetor', 'bell', 'cestretion']StudViewController来处理学生数据,但是您应该创建一个名为StudInsertController的控制器。在此控制器内,您可以执行多个操作:StudentControllerindexcreate等。路由为:

    update

Route::get('/', function () {
    return view('welcome');
});

// ----------- insert student --------------
Route::get('create','StudentController@create');
Route::post('store','StudentController@store');

// ----------- list students --------------
Route::get('list','StudentController@index');

namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Student; class StudViewController extends Controller { public function index() { $students = Student::all(); return view('stud_view', ['students' => $students]); } } 视图中,您可以像访问过的学生一样访问学生:

stud_view

答案 1 :(得分:2)

请尝试使用此代码

namespace App\Http\Controllers;
use App\User;
use Auth;
use DB;
use Session;
use Illuminate\Http\Request;

class UsersController extends Controller
{
   public function index(){
        if(Session::has('adminSession')){
          //$users = User::paginate(2);
       //  $users=User::Where('admin','=','')->get();
          $users = User::where(['admin' => null])->get();        
        //echo "<pre>";print_r($users);die;

         return view('admin.users.users',compact('users'));
        }else{
          return redirect('/admin')->with('flash_message_error','Please login to access');
        }

答案 2 :(得分:1)

尝试一下

public function index(){
 /*$users = DB::select('select * from tudent');*/
 $users = DB::table('student')->select(DB::raw('*'))->get();
 dd($users);
 return view('stud_view',['users'=>$users]);
}