Skip to content

Not working in Laravel 8 #195

@maciekish

Description

@maciekish

Describe the bug
Hi, After upgrading my working Laravel 7 instance to Laravel 8 using a Laravel Shift, laravel-translatable no longer works. It seems like the translatedAttributes are ignored and SQL constraints fail because data is missing. I have done some initial troubleshooting and it seems like static::saved in bootTranslateble in Translatable.php never gets called. bootTranslateble does however get called as part of Laravel initialization, it just seems to ignore static::saved.

To Reproduce
Model:

<?php

namespace App;

use ...
use \Astrotomic\Translatable\Translatable;

public $translatedAttributes = ['title', 'body'];

class Page extends Model
{
    public static function boot()
    {
        parent::boot();

        static::creating(function ($self) {
            $self->slug = Str::slug($self->title, '-');
        });
    }
...

Controller store(Request $request) function looks like this:

    public function store(Request $request)
    {
        $validator = Validator::make($data = $request->all(), Page::$rules);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        $page = Page::create($data);

        return redirect()->action('PageController@index');
    }

As part of my trubleshooting, i have tried to modify the $data array according to the latest documentation, but this still produces the same error:

    public function store(Request $request)
    {
        $validator = Validator::make($data = $request->all(), Page::$rules);

        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }

        //Modify $data for troubleshooting to match documentation format.
        $locale = \App::getLocale();
        $data['slug'] = Str::slug($data['title'], '-');

        //Nest title and body under the proper locale identifier in the array 
        $data[$locale]['title'] = $data['title'];
        $data[$locale]['body'] = $data['body'];

        //Unset the previous root level values
        unset($data['title']);
        unset($data['body']);

        $page = Page::create($data);

        return redirect()->action('PageController@index');
    }

Expected behavior
The object should be created properly with some of the data in the pages table and the title and body attributes in the page_translation table.

Versions (please complete the following information)

  • PHP: 7.4.8
  • Database: MySQL 5.7.30-0ubuntu0.18.04.1-log
  • Laravel: 8.13.0
  • Package: v11.9.0

Additional context
It seems like laravel-translatable is recognized and loaded by Laravel but the events are never fired. I can manually save data like this after creating an object, but it was never necessary before.

$page->translate(\App::getLocale())->title = $data['title'];

I have verified that $data contains something for both the title and body.

Exception
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'pages_slug_unique' (SQL: insert into pages (slug, updated_at, created_at) values (, 2020-11-07 17:43:56, 2020-11-07 17:43:56))

The place where the slug should be is blank in the values.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions