1

I'm having an issue "new for me" since i was able to store the data in database but now i can't. I'm passing some fields through ajax and it was working good but now it isn't.

Here's my controller code:

public function update_os(Request $request)
{
    $this->validate($request, [
      'os' => 'required|max:50'
    ]);
    $user = User::find(Auth::id());
    $user->fill([
      'os' => $request->os
    ]);
    return response()->json($user);
}

Now this is my ajax code:

$(document).on('submit', '#AjaxFormOs', function (event) {
    event.preventDefault();
    var os = $('#AjaxFormOs').serializeArray();
    $.ajax({
        method: 'post',
        url: userUrl + 'updateos',
        data: os,
        _token: token,
        dataType: 'json'
    })
    .done(function (operative) {
        toastr.info("Your new OS is: " + operative.os + "", 'You have been updated your OS info.', {timeOut: 5000});
        $('#os-cancel').addClass('hide');
        $('#os').addClass('hide');
        $('#user-os').removeClass('hide');
        $('.user-info-content').find('#user-os').html(operative.os);
        $('#os-edit').removeClass('hide');
    });
});

This is the route:

Route::post('/{username}/updateos', 'Account\ProfileController@update_os');

This is the form:

<form id="AjaxFormOs">
    {{ csrf_field() }}

    <ul class="list-inline p-r-10 user-info-details-form">
        <li><strong>OS</strong></li>
        <li class="m-l-20 p-b-10" id="user-os">{{ $user->os }}</li>
        <li>
            <input id="os" type="text" class="form-control hide user-input-field" name="os" value="{{ $user->os }}" autofocus>
            <button type="submit" class="hide"></button>
        </li>
        <li class="pull-right"><a href="#" id="os-edit">Edit</a></li>
        <li class="pull-right"><a class="hide" href="#" id="os-cancel">Cancel</a></li>
    </ul>
</form>

This is the response "from google chrome developer tools"

Request URL: http://centralgeek.test/9/updateos
Status Code: 200 OK

{id: 9, name: "José", last_name: "Martínez", username: "josem", 
gender: "Hombre", os: "Ubuntu",…}
about: null
background: "def-background.jpg"
city: "Curicó"
country: "Venezuela"
created_at: "2018-10-09 00:32:45"
deleted_at: null
email: "[email protected]"
gender: "Hombre"
id: 9
job_title: "¿A qué te dedicas?."
last_login_at: "2018-10-09 00:32:45"
last_login_ip: "127.0.0.1"
last_name: "Martínez"
name: "José"
os: "Ubuntu"
phone_model: null
profilepic: "default.png"
twitter: null
twitter_link: null
updated_at: "2018-10-09 01:21:41"
user_agent: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
username: "josem"

As you can see there the os data i've passed is "ubuntu" but once i refresh de window that info disappear.. Is not storing it on database, but it was working before. So what's happening?.

Thanks in advance.

2
  • 1
    How about you do some debugging and figure out if the problem is Javascript or PHP? Then you could cut the amount of code in your question in half. Commented Oct 9, 2018 at 1:50
  • you could also try Auth::user() instead of User::find(Auth::id()) Commented Oct 9, 2018 at 2:25

1 Answer 1

2

You have only filled the user instance with new data.

$user->fill([
  'os' => $request->os
]);

But has not saved to the database.

$user->save();

Alternative

An alternative to both this fill() and save() function combination,
You can use update.

$user->update([
    'os' => $request->os
]);
Sign up to request clarification or add additional context in comments.

1 Comment

Actually i notice that 10 minutes ago, thank you for your answer, finally works again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.