- First of all you should make your code more readable by respecting coding indentation and variables naming
- Second, you can use the Student model to get all the students in DB. No need to use the DB facade
-
There is no need to create 2 controllers
StudViewController
andStudInsertController
to manipulate students data, but you should create an single controller namedStudentController
. 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','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]);
}
}
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