I have followed this tutorial (https://mydnic.be/post/how-to-build-an-efficient-and-seo-friendly-multilingual-architecture-for-your-laravel-application) about laravel multilanguage and localization. Everything seems ok, except I want to CREATE a CRUD for inserting this posts with title and content in multiple language – and STORE it in database – and then read it out in index blade.
Can you show me an example of CRUD in this way in blade for CREATE and in Controller for CREATE and STORE function. How to make this to work?
This is my simple main CRUD, how to extend this to be able to creating and storing into multiple language when creating.
And how to extend the controller for storing in multiple language when using this translatable package from tutorial above (link).
CRUD:
<form method="POST" action="/posts">
@csrf
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title">
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea id="content" name="content" class="form-control"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Publish</button>
</div>
</form>
CONTROLLER
public function store(Request $request)
{
$post = Post::all();
$this->validate(request(), [
'title' => 'required',
'content' => 'required'
]);
$post = new Post;
$post->title = $request->title;
$post->content = $request->content;
$post->save();
return redirect('/');
THANKS 🙂
3
Answers
THIS IS WORKING WELL IN THIS SITUATION:
CONTROLLER:
}
BLADE FOR CREATE + CSS (in background):
WEB.PHP:
So based on the tutorial, you’ll have a column in your posts table called
locale
Then in your view, you can add a select field from which you can chose the locale
Then in your controller add the following line:
Put
locale
in your$fillable
array within the post model.I’m the author of the tutorial.
The whole point of that implementation is that you don’t have to worry about the model locale at all. The locale is set through the URL “/en/…”
So if you make a POST request to your model store URL like so :
The App Locale of your laravel application will be automatically set before you even reach the
PostController@store
method.Then, you can simply create your model like you would usually do (like in your exemple, that’s correct) , and the model will be stored with the according locale.
Now that your model is initially created with the defined locale, you should be able to edit it in another language.
So you can go to this URL:
/en/post/:id/edit
then switch to another locale :/fr/post/:id/edit
and you will notice that all input of the translatable fields are blank. That’s normal because the ‘fr’ translation of that model doesn’t exist yet.You can thus fill the form with the ‘fr’ translated field, then save (update the model). And the translation will be saved. Your model is now translated 🙂
Hope this helps !
PS you can have a look at the example code here https://github.com/mydnic/Laravel-Multilingual-SEO-Example