PHP code example of fomvasss / laravel-translatable
1. Go to this page and download the library: Download fomvasss/laravel-translatable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fomvasss / laravel-translatable example snippets
use Fomvasss\LaravelTranslatable\Traits\HasTranslations;
class Article extends Model
{
use HasTranslations;
//...
}
class ArticleController extends Controller
{
public function index(Request $request)
{
// Select by config('app.locale'):
$articles = \App\Model\Article::byLang()->paginate();
// OR by request
$articles = \App\Model\Article::byLang($request->lang)->paginate();
// ...
}
public function store(Request $request)
{
// Let's create an article in English (en)
$article1 = \App\Model\Article::create([
'name' => 'Article 1, for EN language',
'langcode' => 'en',
]);
// For the saved article ($article1) will be auto-generated UUID
// Example: 70cf3963-cf41-464c-9d81-411d3a524789
// We will create a translation into Ukrainian (uk) for the article ($article1)
$article2 = \App\Model\Article::create([
'name' => 'Стаття 1, для UK мови',
'langcode' => 'uk',
'translation_uuid' => $article1->uuid,
]);
// OR
$article2 = \App\Model\Article::create(['name' => 'Стаття 1, для UK мови']);
$article2->saveTranslatable('uk', $article1->uuid);
// A couple langcode & translation_uuid must be unique
// ...
}
public function show($id)
{
$article = \App\Model\Article::findOrFail($id);
// Get related translations list for article
$translation = $article->getTranslationList();
return view('stow', compact('article', 'translation'));
}
}
Schema::create('articles', function (Blueprint $table) {
...
$table->translatable();
});
// To drop columns
Schema::table('articles', function (Blueprint $table) {
$table->dropTranslatable();
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.