【问题标题】:i just want to view my records from my database using laravel我只想使用 laravel 从我的数据库中查看我的记录
【发布时间】:2019-02-11 10:00:06
【问题描述】:

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

控制器

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');

【问题讨论】:

  • 在您的问题中添加代码而不是图像并显示错误
  • 如果您可以将代码添加到问题中,它会更有帮助,并且还可以添加您遇到的错误
  • 显示dd($users) 提供了什么
  • 在控制器中的数据库查询后添加 dd($users) 并向我们展示结果并遵循我的第一条评论
  • 你可以把你的查询改成这个$users = DB::table('users')-&gt;select(DB::raw('*'))-&gt;get();

标签: php laravel


【解决方案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]);
}

【讨论】:

  • array:3 [▼ 0 => {#172 ▶} 1 => {#174 ▶} 2 => {#175 ▶} ] 它给了我这个
  • “dd($users);”是什么意思
  • dd like var_dump
【解决方案2】:
  1. 首先,您应该通过尊重编码缩进和变量命名来使您的代码更具可读性
  2. 其次,您可以使用Student模型来获取DB中的所有学生。无需使用 DB 门面
  3. 无需创建 2 个控制器 StudViewControllerStudInsertController 来操作学生数据,但您应该创建一个名为 StudentController 的控制器。在此控制器中,您可以有多个操作:indexcreateupdate 等。路由将是:

    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 视图中,您可以像以前一样访问学生:

@foreach ($students as $student)
    <tr>
        <td>{{ $student->id }}</td>
        <td>{{ $student->name }}</td>
    </tr>
@endforeach

【讨论】:

  • 集合 {#173 ▼ #items: array:3 [▼ 0 => {#174 ▼ +"ID": 1 +"Name": "hassan" } 1 => {#176 ▼ +“ID”:2 +“姓名”:“艾哈迈德”} 2 => {#177 ▼ +“ID”:3 +“姓名”:“7oms”}] }
  • 我不想看到这个
  • 那是因为你在某处使用了dd 函数......你应该删除那个函数调用
【解决方案3】:

请试试这个代码

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');
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-07
    • 1970-01-01
    • 2017-10-05
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多