amaelftah/laravel-trix

No documentation for saving a post that already exists using update()

EricPaulson opened this issue · 7 comments

Let's say I create a new Post using Trix editor, and it works great (it does). However, I'm having trouble understanding out to save an existing Post.

Post::create() utilizes the editor perfectly, and the text shows up in trix_rich_texts.

However, when I do an update(), it rightly complains that the trix-specific fields don't exist. How do you edit the trix data?

I think I have just figured it out. It looks like I just need to add

'flyer-trixFields' => request('flyer-trixFields'),

To my update statement, and then it saves properly.

thanks @EricPaulson and you are right .

Hi - I'm having the same problem. I've already added it to my update but it still gives me the message "SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cmspage-trixFields' in 'field list' "

My code is:
$cmspage = new Cmspage;

`

    $cmspage::where('id', $id)
        ->update([
        'cmspage_id' => $request->cmspage_id,
        'category' => $request->category,
        'page_name' => $request->page_name,
        'version_control' => $request->version_control,
        'cmspage-trixFields' => request('cmspage-trixFields'),
        'attachment-cmspage-trixFields' => request('attachment-cmspage-trixFields'),
        'user_id' => $user->id
    ]);`

Please can you advise?

hey @mattgroom can you post your Cmspage model how it look like

Sure - the create works fine, just not the update. My model file is:

`<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Te7aHoudini\LaravelTrix\Traits\HasTrixRichText;

class Cmspage extends Model
{
use HasTrixRichText;
use SoftDeletes;

protected $guarded = [];
protected $dates = ['deleted_at'];

protected $fillable = [
    'cmspage_id', 'category', 'page_name', 'version_control', 'cmspage-trixFields', 'attachment-cmspage-trixFields'
];

public function cmspages()
{
    return $this->hasMany(Cmspage::class);
}

public function childrenCmspages()
{
    return $this->hasMany(Cmspage::class)->with('cmspages');
}

public function trixRender($field)
{

    return $this->trixRichText->where('field', $field)->first()->content;

// $bob = $this->trixFields;
dd($bob);
}

public function trixImageRender($field)
{
    return $this->trixAttachments->where('field', $field)->first()->attachment;
}

}
`

@mattgroom ok got it . then i think what's happeing you are updating directly through query . and what needs to happen is retrieve eloquent models first then update . something like that

 Cmspage::find($id)
        ->update([
        'cmspage_id' => $request->cmspage_id,
        'category' => $request->category,
        'page_name' => $request->page_name,
        'version_control' => $request->version_control,
        'cmspage-trixFields' => request('cmspage-trixFields'),
        'attachment-cmspage-trixFields' => request('attachment-cmspage-trixFields'),
        'user_id' => $user->id
    ]);`

Ah - had a feeling it would be something simple, many thanks - have tried and this works for me perfectly now! Thanks for the quick reply!