sdebacker / TypiCMS

Multilingual CMS built with Laravel 4.2
MIT License
474 stars 119 forks source link

Create a Category with Eloquent? #112

Closed dragonattack closed 9 years ago

dragonattack commented 9 years ago

How to create any module entry (for example a Category) with Eloquent (I'm trying to use that from seeder)?

The problem with translation relations, so this could be just a Laravel question, but I'm new to this, so still can't figure it out.

Category::create(['title' => 'test 1', 'slug' => 'test-1', 'status' => 1]);

but of course this gives a error

[PDOException]
  SQLSTATE[42S22]: Column not found: 1054 Unknown column 'title' in 'field list'

How to apply a translation field and do that right? Thank you.

dragonattack commented 9 years ago

actually a simple

Category::create([]);

or

$c = new Category;
$c->save();

gives error too

[ErrorException]
  Trying to get property of non-object

but it adds an empty entry to the DB though.

sdebacker commented 9 years ago

Did you try:

Category::create(['en' => ['title' => 'test 1', 'slug' => 'test-1', 'status' => 1]]);
dragonattack commented 9 years ago

this gives the same error:

[ErrorException]
  Trying to get property of non-object
dragonattack commented 9 years ago

for Category it is possible to workaround like this:

$id = DB::table('categories')->insertGetId([]);

$c = Category::find($id);
$c->title = 'Title 1';
$c->slug = 'title-1';
$c->status = 1;
$c->save();

It's not very pretty, but it still works. But that won't work for Project

$id = DB::table('projects')->insertGetId([]);

$p = Project::find($id);
$p->title = 'Title 1';
$p->slug = 'title-1';
$p->status = 1;
$p->save();

Gives the above error

dragonattack commented 9 years ago

I did it this way and it works without any errors!

try {
    $id = DB::table('projects')->insertGetId([]);

    $p = Project::find($id);
    $p->title = 'Title 1';
    $p->slug = 'title-1';
    $p->status = 1;
    $p->save();
} catch (\Exception $e) {
    dd($e);
}

I really not that good at it to understand why it only works that way and not the other.

dragonattack commented 9 years ago

And this not always works, and same code works from time to time. Well, it's weird.. Someone please help! :)

sdebacker commented 9 years ago

Perhaps a namespace problem ?