polyglot
polyglot copied to clipboard
Trying to get property of non-object
Hi,
Thanks for writing this package.
After adding this package to my Laravel 4.2 project I tried to get this to work. I set up everything the way I think it's supposed to be (as others pointed out the documentation could be better). Saving to the database doesn't seem to be a problem but when I want to read I get the following error:
Trying to get property of non-object (View: /Applications/MAMP/htdocs/*my view*.blade.php)
Models:
use Polyglot\Polyglot;
class Page extends Polyglot {
use SoftDeletingTrait;
protected $fillable = [
'lang',
'meta_title',
'meta_description',
'title',
'page_title',
'page_content',
];
protected $polyglot = [
'meta_title',
'meta_description',
'title',
'page_title',
'page_content',
];
// ...
}
class PageLang extends Eloquent {
public $timestamps = false;
protected $fillable = [
'page_id',
'lang',
'meta_title',
'meta_description',
'title',
'page_title',
'page_content',
];
}
My blade template:
$page->nl->title
/*
This is what's causing the error
$page->title doesn't produce errors but is, of course, empty
*/
Been stuck on this for a while now. Any help is greatly appreciated :-)
Turns out there's two things going on:
- I should've filled the locales() array in the config file
- In the database there should be a record for the language you're trying to get.
I think it's better to, if a language isn't present, to just return an empty string instead of an error. Is this possible?
Database:
id | page_id | lang | title |
---|---|---|---|
1 | 1 | nl | titel |
2 | 1 | fr | titre |
3 | 1 | en | title |
Code:
$page->nl->title // we're OK
$page->fr->title // we're OK
$page->en->title // we're OK
$page->es->title // Returns error. Spanish isn't in the database. Would be better to return an empty string, or default to $page->title? But don't give an error>