I have a department that has many streams. How can I manage all the streams from a specific department?
For now I have a route for the departments:
Route::resource('/manage/department', 'DepartmentController');
The index controller for the department
public function index()
{
$departments = Department::all();
return view('admin.department.index', compact('departments'));
}
The index file looks like this:
@if($departments)
<table class="table">
<thead>
<tr>
<th>Dept Code</th>
<th>Name</th>
</tr>
</thead>
<tbody>
@foreach($departments as $department)
<tr>
<td>{{ $department->dept_code }}</td>
<td>{{ $department->name }}</td>
<td><a class="btn-primary btn" href="#">Streams</a></td>
<td><a class="btn btn-primary" href="{{route('department.edit', $department->id)}}">Edit</a></td>
<td>
{!! Form::open(['method' => 'DELETE', 'action'=>['DepartmentController@destroy', $department->id]]) !!}
{!! Form::submit('Delete', ['class'=>'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
@endif
Now, when I click under on the streams button, I want to be able to view all the streams of that particular department as well as add a new stream.
How can I achieve that?
How would my new route be in case I have to add a new one, how do I do in the controller?
Hope you someone can guide me on this.
Thanks in advance
It looks like you want to manage Streams related to a department and by managing i think you mean the CRUD operations , a clean way to do that is to define a sub-resource related to the department manager :
Routes
Route::group( [ 'prefix' => '/manage/department/{department_id}'], function ( Router $router ) {
$router->resource( 'streams', 'StreamsController',['as'=>'department'] ); // here 'as' acts as a prefix for streams resource named routes
} );
Now you got a sub resource controller with a department_id as a required parameter in every method ( you should add it ) like this :
StreamsController
public function index($department_id){
// here you list the streams of a certain department smth like
$streams = Stream::where('department_id',$department_id)->get();
return view('admin.stream.index', compact('streams'));
}
public function create($department_id){
// here you add your create view
}
public function store(Request $request , $department_id){
// your post request
}
Finally in your index file you can call the index method like this :
Index
<td><a class="btn-primary btn" href="{{route('department.streams.index',[$department->id])}}">Streams</a></td>
The answer is actually simpler than what I imagined.
I added this route to my index:
<td><a class="btn-primary btn" href="{{route('course.show', $department->id)}}">Courses</a></td>
Then in the show method of the streams controller I did this:
public function show($id)
{
$department = Department::findOrFail($id);
$courses = $department->courses;
return view('admin.department.show', compact('courses'));
}
No extra routes have to be added because when I created the model it already came with the resources.