Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.04k stars 885 forks source link

[Bug] Translatable slugs not getting translated #5556

Closed ablunier closed 1 month ago

ablunier commented 1 month ago

Bug report

What I did

I configured my models as documented here and when I translate my model the slug fields gets stored in JSON but the content is the same in all languages (the fallback language).

What I expected to happen

The expected behaviour is to get the slug translated in each language, generated from the configured source.

What happened

The slug field gets stored in JSON but the content is the same in all languages (the fallback language).

What I've already tried to fix it

Change the Traits a check all the documentation of both packages (Backpack and Eloquent Sluggable).

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there?

Yes

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

8.2.12

PHP EXTENSIONS:

Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, random, Reflection, SPL, session, standard, sodium, mysqlnd, PDO, xml, bcmath, bz2, calendar, ctype, curl, dba, dom, enchant, mbstring, FFI, fileinfo, ftp, gd, gettext, gmp, iconv, igbinary, imagick, imap, intl, ldap, exif, msgpack, mysqli, odbc, pdo_dblib, PDO_Firebird, pdo_mysql, PDO_ODBC, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, pspell, readline, redis, shmop, SimpleXML, snmp, soap, sockets, sqlite3, sysvmsg, sysvsem, sysvshm, tidy, tokenizer, xmlreader, xmlrpc, xmlwriter, xsl, zip, memcached, Zend OPcache

LARAVEL VERSION:

10.48.15.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.3.4 backpack/crud: 6.7.17 backpack/filemanager: 3.0.8 backpack/generators: v4.0.5 backpack/pro: 2.0.23 backpack/settings: 3.1.1 backpack/theme-tabler: 1.2.10

karandatwani92 commented 1 month ago

Hey @ablunier

I just tried to make the slug field translatable, and I was able to store the slug in multiple languages. Screenshot 2024-07-11 at 3 26 53 PM

I followed the docs example to have it:

  1. Added a json type slug column to the DB table

  2. Created a slug field:

    CRUD::field([   // Text
    'name'  => 'slug',
    'target'  => 'name',
    'label' => "Slug",
    'type'  => 'slug',
    'tab'   => 'Texts',
    ]);
  3. Added the following to the Model:

    
    + use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\Sluggable;
    + use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\SluggableScopeHelpers;
    + use Backpack\CRUD\app\Models\Traits\SpatieTranslatable\HasTranslations;

class Product extends Model implements HasMedia { use CrudTrait; use LogsActivity;



I hope it helps.
ablunier commented 1 month ago

Hi @karandatwani92, thanks for your reply.

After checking again our code and comparing it to yours, we did a couple more tests and found out that the issue only arises when you don't have the slug field. I don't know if it is the expected behaviour, but at least it is not documented that the slug field is required. And it is also confusing that if you don't want a translatable slug it is not required.