Yash Lotan
Yash Lotan

Reputation: 377

Laravel and blade error still not solved

I just wanted to show the image by

<img src="{!!DB::table('users')->where('username',$game->initiator)->first()->photo_path!!}" alt="" />

but i get a error

Trying to get property of non-object 

I have tried all of the methods like removing brackets, quotes and using Html::image but no cure was found.

But when I change my code to

{!!dd(DB::table('users')->where('username',$game->initiator)->first()->photo_path)!!}

or

{{dd(DB::table('users')->where('username',$game->initiator)->first()->photo_path)}}

I get a url of gravatar which is set in the database and this url has double quotes like :- "http://www.gravatar.com/avatar/5478144dd02f370affc2ad70f22af697?s=200&r=pg&d=identicon"

which means that the link exists in database but why error is showed up. I tried

<img src="{!!url(DB::table('users')->where('username',$game->initiator)->first()->photo_path)!!}" alt="" />

but this too didn't work for me.

Also when I just replace $game->initiator by a username, then image is displayed correctly. Those who think the variable $game->initiator is empty then they are wrong, I used dd() function to see whether it is empty or not and then I found it contains a user details.

I cannot do this thing in the controller because I have more than 1000 games and all have a initiator and a acceptor whose images are to be displayed.

EDIT : When I add {{dd($game)}} after the image then it worked and suppressed the error but the error is still view-able from the source of the html It looks like

<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) { root = doc.getElementById(root); 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); } }); }; function isCtrlKey(e) { return e.ctrlKey || e.metaKey; } addEventListener(root, 'mouseover', function (e) { if ('' != refStyle.innerHTML) { refStyle.innerHTML = ''; } }); a('mouseover', function (a) { 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) { 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 (doc.getSelection) { try { doc.getSelection().removeAllRanges(); } catch (e) { doc.getSelection().empty(); } } else { doc.selection.empty(); } } }); var indentRx = new RegExp('^('+(root.getAttribute('data-indent-pad') || ' ').replace(rxEsc, '\\$1')+')+', 'm'), elt = root.getElementsByTagName('A'), len = elt.length, i = 0, t = []; while (i < len) t.push(elt[i++]); elt = root.getElementsByTagName('SAMP'); len = elt.length; i = 0; while (i < len) t.push(elt[i++]); root = t; len = t.length; i = t = 0; while (i < len) { elt = root[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'; if ('sf-dump' != elt.parentNode.className) { 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 { t = a.nextSibling; elt.appendChild(a); t.parentNode.insertBefore(a, t); 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'; } } } } ++i; } }; })(document); </script>

Upvotes: 0

Views: 220

Answers (4)

Peter
Peter

Reputation: 2711

I had similar problem. with DB you get access to a record in database, but it is not an instance of a Model. Try to access the data by a model, in your case just use \App\User::where(.... and see if the error occures.

Upvotes: 0

Yash Lotan
Yash Lotan

Reputation: 377

The real problem arised because I have made some changes in the users table . I changes the datatype of balance from string to bigint and after that change I was getting the above errors.

You should also check if this had happened to you.

Solution : You should change the column from migration file or create another database or dump the values in that table.

In my case I changed the database.

Upvotes: 0

Dan H
Dan H

Reputation: 3623

Facades are supposed to live in global namespace. Try to use:

{{ \DB::table('users')->where('username', $game->initiator)->first()->photo_path }}

If it doesn't, your alias is not registering properly. Check config/app.php > aliases and make sure DB is listed there.

Upvotes: 0

Martin Bean
Martin Bean

Reputation: 39429

The error message is telling you exactly what’s wrong: you’re trying to get a property of non-object.

Because you’re method-chaining (DB::table('users')->where('username',$game->initiator)->first()->photo_path) it’s difficult to see exactly where the issue is, so you need to step through each method call and find out where it’s failing (it’ll return null rather than an object).

So in your code, start stepping through the methods…

dd(DB::table('users'));
dd(DB::table('users')->where('username', $game->initiator));

…and so on, until you find out which method returns null rather than an object.

Upvotes: 1

Related Questions