i just want to view my records from my database using laravel

  1. First of all you should make your code more readable by respecting coding indentation and variables naming
  2. Second, you can use the Student model to get all the students in DB. No need to use the DB facade
  3. There is no need to create 2 controllers StudViewController and StudInsertController to manipulate students data, but you should create an single controller named StudentController. Inside this controller you can have multiple actions: index, create, update, etc. The routes would be:

    Route::get("https://stackoverflow.com/", function () {
        return view('welcome');
    });
    
    // ----------- insert student --------------
    Route::get('create','[email protected]');
    Route::post('store','[email protected]');
    
    // ----------- list students --------------
    Route::get('list','[email protected]');
    

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

In stud_view view you access the students like you already did:

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

Browse More Popular Posts

Leave a Comment