我只是想为学生建立一个数据库,但是当我请求查看记录页面时,它无法正常工作,给我一个错误,为什么它不起作用,我只想从数据库中查看我的记录>
控制器
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');
答案 0 :(得分:2)
无需创建两个控制器['cer', 'creetor', 'bell', 'cestretion']
和StudViewController
来处理学生数据,但是您应该创建一个名为StudInsertController
的控制器。在此控制器内,您可以执行多个操作:StudentController
,index
,create
等。路由为:
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]);
}