DEV Community

Cover image for Laravel With dynamic columns Translatable and Multi Lang in slug
Salma
Salma

Posted on

Laravel With dynamic columns Translatable and Multi Lang in slug

Aloha There !

in this tutorial
Use this package to make transtable columns dynamic and make slug transtable because it so useful in SEO
We are using this two packages :

This package provides to us To make the column contain many lang without need to make 3 or 4 columns to one lang
as example If we have title in arabic , english and Spanish
we will have
title_ar
title_en
title_es
another columns if we make have column called description
it is not good to performance , more data in db and not perfectly when get data

install this package with this
composer require spatie/laravel-translatable

If you want to have another fallback_locale than the app fallback locale (see config/app.php), you could publish the config file:

php artisan vendor:publish --provider="Spatie\Translatable\TranslatableServiceProvider"

in our modal add this trait
use HasTranslations;

then make install to sluggable package
from there
composer require spatie/laravel-sluggable

and add this trait in modal

HasTranslatableSlug

and put every column who have translate in this variable in this like it

public $translatable = ['name', 'slug'];

Add This function in modal
this function create slug and when we reterive the slug with two langague
put
usingLanguage(false)

/**
     * Get the options for generating the slug.
     */
    public function getSlugOptions() : SlugOptions
    {
        return SlugOptions::createWithLocales(['en', 'ar'])
            ->generateSlugsFrom(function($model, $locale) {
                return "{$model->name}";
            })
            ->saveSlugsTo('slug')
            ->usingLanguage(false);
    }

Enter fullscreen mode Exit fullscreen mode

*the final view for modal
*

Image description

in the migration we create the column in this way
$table->json('name');
$table->json('slug');

*in create form
*

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<a href="{{route('posts.create')}}">Create</a>
<form action="{{route('posts.store')}}" method="post">
    @csrf
    <input type="text" name="name[ar]">
    <input type="text" name="name[en]">

    <input type="submit" value="Save">
</form>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

*in web.php
*

Route::group(['prefix' => LaravelLocalization::setLocale()], function()
{
    Route::get('/', function () {
        return view('welcome');
    });

       Route::resource('/posts',PostController::class)->except('delete','update');

});
Enter fullscreen mode Exit fullscreen mode

*in Post Controller
*

/**
     * 
     * Get All posts
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function index()
    {
        $posts = Post::all();
        return view('index', compact('posts'));
    }


    /**
     * Create new post 
     * 
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function create()
    {
        return view('create');
    }

    /**
     * Store new post 
     * 
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(Request $request)
    {
        $post = Post::create($request->all());
        return redirect()->route('posts.index');
    }

    /**
     * Show post
     * @param $slug
     * @return \Illuminate\Http\JsonResponse
     */
    public function show($slug)
    {
        $post = Post::where('slug','like','%"'.$slug.'"%')->first();
        return response()->json($post);
    }
Enter fullscreen mode Exit fullscreen mode

You Can see all of this code in this repo
https://github.com/salmazz/Multi-Langague-Example-Laravel-

If u have any question please contact to me !
hope it will be useful thank u

Top comments (0)