Update nulling all values in database

Published 4 months ago by jgravois

I am patching a payload to an update method and I return request()->all() to see what's hitting the server.

The data looks like this:

data: Object
    contact: Object
        company: "blah"
        title: "blah"
        firstname: "blah"
        lastname: "blah"
        email: "blah"

Since it is an object of objects, I am handing it as such

public function update($id)
{
        $project = Project::whereId($id)->first();

    $project->contact->company = request('contact->company');
        $project->contact->firstname = request('contact->firstname');
        $project->contact->lastname = request('contact->lastname');
        $project->contact->title = request('contact->title');
        $project->contact->email = request('contact->email');
        $project->contact->save();

    $updated = Project::whereId($id)->first();
        return $updated;

}

The data patched is not in the database but all fields are null;

Am I not handling the data properly?

Best Answer (As Selected By jgravois)
Snapey

ok, that's not so useful because it's the whole source for the dump with all the formatting. I'm certainly not pouring through that,

try var_dump instead

Snapey
Snapey
4 months ago (648,645 XP)

is your data a php object or json?

i see you are using arrow inside quoted string. that won't work.

jgravois

not really sure

It is an axios patch from a vue component

Snapey
Snapey
4 months ago (648,645 XP)

try dd(request->all()) and see what it shows

jgravois
"<script> Sfdump = window.Sfdump || (function (doc) { var refStyle = doc.createElement('style'), rxEsc = /([.*+?^${}()|\[\]\/\\])/g, idRx = /\bsf-dump-\d+-ref[012]\w+\b/, keyHint = 0 <= navigator.platform.toUpperCase().indexOf('MAC') ? 'Cmd' : 'Ctrl', addEventListener = function (e, n, cb) { e.addEventListener(n, cb, false); }; (doc.documentElement.firstElementChild || doc.documentElement.children[0]).appendChild(refStyle); if (!doc.addEventListener) { addEventListener = function (element, eventName, callback) { element.attachEvent('on' + eventName, function (e) { e.preventDefault = function () {e.returnValue = false;}; e.target = e.srcElement; callback(e); }); }; } function toggle(a, recursive) { var s = a.nextSibling || {}, oldClass = s.className, arrow, newClass; if ('sf-dump-compact' == oldClass) { arrow = '&#9660;'; newClass = 'sf-dump-expanded'; } else if ('sf-dump-expanded' == oldClass) { arrow = '&#9654;'; newClass = 'sf-dump-compact'; } else { return false; } a.lastChild.innerHTML = arrow; s.className = newClass; if (recursive) { try { a = s.querySelectorAll('.'+oldClass); for (s = 0; s < a.length; ++s) { if (a[s].className !== newClass) { a[s].className = newClass; a[s].previousSibling.lastChild.innerHTML = arrow; } } } catch (e) { } } return true; }; return function (root, x) { root = doc.getElementById(root); var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || ' ').replace(rxEsc, '\\$1')+')+', 'm'), options = {"maxDepth":1,"maxStringLength":160,"fileLinkFormat":false}, elt = root.getElementsByTagName('A'), len = elt.length, i = 0, s, h, t = []; while (i < len) t.push(elt[i++]); for (i in x) { options[i] = x[i]; } function a(e, f) { addEventListener(root, e, function (e) { if ('A' == e.target.tagName) { f(e.target, e); } else if ('A' == e.target.parentNode.tagName) { f(e.target.parentNode, e); } else if (e.target.nextElementSibling && 'A' == e.target.nextElementSibling.tagName) { f(e.target.nextElementSibling, e, true); } }); }; function isCtrlKey(e) { return e.ctrlKey || e.metaKey; } addEventListener(root, 'mouseover', function (e) { if ('' != refStyle.innerHTML) { refStyle.innerHTML = ''; } }); a('mouseover', function (a, e, c) { if (c) { e.target.style.cursor = "pointer"; } else if (a = idRx.exec(a.className)) { try { refStyle.innerHTML = 'pre.sf-dump .'+a[0]+'{background-color: #B729D9; color: #FFF !important; border-radius: 2px}'; } catch (e) { } } }); a('click', function (a, e, c) { if (/\bsf-dump-toggle\b/.test(a.className)) { e.preventDefault(); if (!toggle(a, isCtrlKey(e))) { var r = doc.getElementById(a.getAttribute('href').substr(1)), s = r.previousSibling, f = r.parentNode, t = a.parentNode; t.replaceChild(r, a); f.replaceChild(a, s); t.insertBefore(s, r); f = f.firstChild.nodeValue.match(indentRx); t = t.firstChild.nodeValue.match(indentRx); if (f && t && f[0] !== t[0]) { r.innerHTML = r.innerHTML.replace(new RegExp('^'+f[0].replace(rxEsc, '\\$1'), 'mg'), t[0]); } if ('sf-dump-compact' == r.className) { toggle(s, isCtrlKey(e)); } } if (c) { } else if (doc.getSelection) { try { doc.getSelection().removeAllRanges(); } catch (e) { doc.getSelection().empty(); } } else { doc.selection.empty(); } } else if (/\bsf-dump-str-toggle\b/.test(a.className)) { e.preventDefault(); e = a.parentNode.parentNode; e.className = e.className.replace(/sf-dump-str-(expand|collapse)/, a.parentNode.className); } }); elt = root.getElementsByTagName('SAMP'); len = elt.length; i = 0; while (i < len) t.push(elt[i++]); len = t.length; for (i = 0; i < len; ++i) { elt = t[i]; if ('SAMP' == elt.tagName) { elt.className = 'sf-dump-expanded'; a = elt.previousSibling || {}; if ('A' != a.tagName) { a = doc.createElement('A'); a.className = 'sf-dump-ref'; elt.parentNode.insertBefore(a, elt); } else { a.innerHTML += ' '; } a.title = (a.title ? a.title+'\n[' : '[')+keyHint+'+click] Expand all children'; a.innerHTML += '<span>&#9660;</span>'; a.className += ' sf-dump-toggle'; x = 1; if ('sf-dump' != elt.parentNode.className) { x += elt.parentNode.getAttribute('data-depth')/1; } elt.setAttribute('data-depth', x); if (x > options.maxDepth) { toggle(a); } } else if ('sf-dump-ref' == elt.className && (a = elt.getAttribute('href'))) { a = a.substr(1); elt.className += ' '+a; if (/[\[{]$/.test(elt.previousSibling.nodeValue)) { a = a != elt.nextSibling.id && doc.getElementById(a); try { s = a.nextSibling; elt.appendChild(a); s.parentNode.insertBefore(a, s); if (/^[@#]/.test(elt.innerHTML)) { elt.innerHTML += ' <span>&#9654;</span>'; } else { elt.innerHTML = '<span>&#9654;</span>'; elt.className = 'sf-dump-ref'; } elt.className += ' sf-dump-toggle'; } catch (e) { if ('&' == elt.innerHTML.charAt(0)) { elt.innerHTML = '&hellip;'; elt.className = 'sf-dump-ref'; } } } } } if (0 >= options.maxStringLength) { return; } try { elt = root.querySelectorAll('.sf-dump-str'); len = elt.length; i = 0; t = []; while (i < len) t.push(elt[i++]); len = t.length; for (i = 0; i < len; ++i) { elt = t[i]; s = elt.innerText || elt.textC…/span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>962</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>963</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>964</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>965</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>966</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>967</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>968</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>969</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>970</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>971</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>972</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>973</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>974</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>975</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>976</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>977</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>978</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>979</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>980</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>981</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>982</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>983</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>984</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>985</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>986</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>987</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>988</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>989</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>990</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>991</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>992</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>993</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>994</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>995</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>996</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>997</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>998</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>999</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1000</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1001</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1002</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1003</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1004</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1005</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1006</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1007</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1008</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1009</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵    <span class=sf-dump-index>1010</span> => <span class=sf-dump-note>array:18</span> [ &hellip;18]↵  </samp>]↵</samp>]↵</pre><script>Sfdump("sf-dump-1240214518")</script>
Snapey
Snapey
4 months ago (648,645 XP)

ok, that's not so useful because it's the whole source for the dump with all the formatting. I'm certainly not pouring through that,

try var_dump instead

jgravois

THANKS!

var_dump showed me what's wrong.

I did this and it works

public function update($id, Request $request)
{
        $data = (object) $request->json()->all();
        $project = AdcProject::whereId($id)->first();
    
    $project->contact->company = $data->contact['company'];
        $project->contact->firstname = $data->contact['firstname'];
        $project->contact->lastname = $data->contact['lastname'];
        $project->contact->title = $data->contact['title'];
        $project->contact->email = $data->contact['email'];
        $project->contact->save();

    $updated = AdcProject::whereId($id)->first();
    return $updated;
}
Snapey
Snapey
4 months ago (648,645 XP)

good

Please sign in or create an account to participate in this conversation.