Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.23k stars 155 forks source link

Compatibility with BackpackForLaravel #273

Closed cristian-araujo closed 2 years ago

cristian-araujo commented 2 years ago

Is your feature request related to a problem? Please describe. Backpack to fill the model uses $this->crud->getStrippedSaveRequest() and it function returns only the fillable attributes return $this->getRequest()->only($this->getAllFieldNames());

so, in this instance if I want to use something like this

$data = [
  'author' => 'Gummibeer',
  'en' => ['title' => 'My first post'],
  'fr' => ['title' => 'Mon premier post'],
];

it doesn't work because Backpack is cleaning the request bag.

Describe the solution you'd like im not sure what's the better solution for this package, I only made a temporary fix for it.

Describe alternatives you've considered a temporary solution is extending the updateOperation use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation{ update as traitUpdate; } and customizing it to save all request fields.

first is necessary to change $translatedAttributes from protected to public and modifying the request bag

public function update()
    {
        $this->crud->hasAccessOrFail('update');

        $data = $this->crud->getRequest();
        $translatables_attributes = $this->crud->model->translatedAttributes;
        if($translatables_attributes) {
            $translatables = [];
            foreach ($translatables_attributes as $attribute) {
                if ($data->get($attribute)) {
                    $translatables[app()->getLocale()][$attribute] = $data->get($attribute);
                    $data->request->remove($attribute);
                }
            }
            $data->request->add($translatables);
        }
        // execute the FormRequest authorization and validation, if one is required
        $request = $this->crud->validateRequest();
        // update the row in the db
        $item = $this->crud->update($request->get($this->crud->model->getKeyName()),
            $data->all());
        $item->save();
        $this->data['entry'] = $this->crud->entry = $item;

        // show a success message
        \Alert::success(trans('backpack::crud.update_success'))->flash();

        // save the redirect choice for next time
        $this->crud->setSaveAction();

        return $this->crud->performSaveAction($item->getKey());
    }

in this way the update will works

and another thing I dont know why this way it doesnt work as new translation

App::setLocale('de');
$post->title = 'Mein cooler Beitrag';
$post->save();
github-actions[bot] commented 2 years ago

This issue is stale because it has been open 21 days with no activity. Remove stale label or comment or this will be closed in 7 days