Search code examples
laravellaravel-5.5laravel-spark

Returning custom variable in Settings - Laravel Spark


I have setup Spark and I have created my custom view in Settings - Students (assume User object is actually a teacher). I have also created migration and model Student.

Now http://spark.app/settings/students returns the page successfully. At this point, I need to return data from backend. I investigated Spark\Http\Controllers\Settings\DashboardController@show - which is the method returning the 'settings' view, however this doesn't return any data to view using ->with('user', $user)

But, as mentioned in Docs, :user="user" :teams="teams" :current-team="currentTeam" already available out of the box.

Where and how does Spark returns these values to /settings? And How do I make my Student object available likewise?


Now, if I want to return my Student object to front-end, I have 2 choices.

1) edit Spark\Http\Controllers\Settings\DashboardController

2) I think Spark\InitialFrontendState is the place where Spark returns these objects user, teams, currentTeam. This approach is something I've seen for the first time to be honest and I didn't really understand how it works.

So how should I achieve in Spark, something as simple as :

return view('spark::settings')->with('student', $student); ?


Solution

  • Add a new route and set up your own Controller & own view

    web.php

    Route::get('/settings/students', 'SettingsStudentController@index');
    

    SettingsStudentController.php

    class SettingsStudentController extends Controller {
        public function __construct() {
            $this->middleware('auth');
        }
        public function index(Request $request) {
                $user          = Auth::user();
                $student = STUDENTCLASS::whatever();
                return view('yourstudentview', ['student' => $student , 'user' => $user]);
        }
    }