Ad

Laravel 5 User Defined Settings Only Needs To Be Called Once

- 1 answer

For my personal website (online administration) I have an appointments page, with certain settings, which is just for me, so I don't need it to be secure.

For example, I can change my view (to show all appointments or to sort them by label, or status). I can also exclude a status to make sure it's not being shown. Everything works, no problem there.

My issue is this. I have a simple field in my user database called "view". When I go to my appointments page, I check the value of my "view" field and if it is "status" for example, in my controller I set "$view = status", to return my "status" view. This works, with the following simple check:

$getUserView = \Auth::user()->view;

    if($getUserView){
        $view = $getUserView;
    }

In my view itself I have a dropdown to change the view. Now, when I go to my view, it shows the "status view" just fine. But when I want to change the view to "default" or "label" using my dropdown, it should change the view to what I selected. So basically what I want to achieve is, when I go to my appointments page for the first time, it should show the view that I have set in my database, but only that one time. I could set it in a session maybe for that but I am just not sure how to accomplish this. Any pointers would be helpful!

Edit:

Still struggling with this, because I am using GET for everything, also the dropdown. Example, when I change the value in the dropdown, a javascript simply calls the URL again, but with the status that was selected in the dropdown. So, for example, my default URL is the following:

http://example.com/appointments/status/default

Now, I select "completed" in the dropdown, the following URL is called:

http://example.com/appointments/status/completed

In my appointments controller I put the following:

$status = session()->get("status", \Auth::user()->status);

In my routes I have the following:

Route::get('appointments/status/{status}', array('as' => 'appointments', 'uses' => 'Appointments\[email protected]'));

Maybe I should change "{status}" in the route to "$status" and use the put method to set the "$status"? Not sure what the best method would be.

Ad

Answer

When using the get method on the Session, the second argument is intended for a default value, which will be returned if the session key is not found.

You could do something like this:

$user = \Auth::user();
$view = session()->get("appointments_view", $user->view);

That will get the view set in the session, and if that is not set, it'll return $user->view. Now, when the users picks another view in the dropdown, just do:

session()->put("appointments_view", $dropDownValue);
Ad
source: stackoverflow.com
Ad