skip to Main Content

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


  1. Chosen as BEST ANSWER

    THIS IS WORKING WELL IN THIS SITUATION:

    CONTROLLER:

    public function create()
       {
    
           return view('services.new');
    
       }
    
       public function store(Request $request)
       {
    
      $service = new Service();
    
       $service->save();
    $this->validate($request, [
    
               'title2' => 'required|max:350',
               'content2' => 'required'
             ]);
    
         foreach (['en', 'bs'] as $locale) {
    
               $service->translateOrNew('en')->title = $request->title;
               $service->translateOrNew('en')->content = $request->content; 
               $service->translateOrNew('bs')->title = $request->title2;
               $service->translateOrNew('bs')->content = $request->content2; 
    
           }
    
    
           $service->translateOrNew('en')->title = $request->title;
           $service->translateOrNew('en')->content = $request->content;  
           $service->translateOrNew('bs')->title = $request->title2;
           $service->translateOrNew('bs')->content = $request->content2; 
           // $article->translateOrNew('en')->text = ['texten'];
           // $article->translateOrNew('ka')->name = ['nameka'];
           // $article->translateOrNew('ka')->text = ['textka'];
    
    
    
    
    
    
        // return $article;
        // exit();
    
       $service->save();
    
      return redirect()->back();
    

    }

    BLADE FOR CREATE + CSS (in background):

    <form action="{{route('service.store')}}" method="POST">
         {{csrf_field()}}
    
    
    
    
    
        <div class="tabset">
      <!-- Tab 1 -->
      <input type="radio" name="tabset" class="radio1" id="tab1" aria-controls="marzen" checked>
      <label for="tab1">Bosanski</label>
      <!-- Tab 2 -->
      <input type="radio" class="radio1" name="tabset" id="tab2" aria-controls="rauchbier">
      <label for="tab2">Engleski</label>
      {{-- <!-- Tab 3 -->
      <input type="radio" name="tabset" id="tab3" aria-controls="dunkles">
      <label for="tab3">Dunkles Bock</label> --}}
    
      <div class="tab-panels">
        <section id="marzen" class="tab-panel">
          <h2>Dodaj novu uslugu</h2>
            <div class="form-group">
    <lebal>Naslov*(bs)</lebal>
    <input type="text" class="form-control" name="title2">
    </div>
    
      <div class="form-group">
    <lebal>Opis*(bs)</lebal>
    <textarea class="form-control" name="content2"></textarea>
    </div>
      </section>
        <section id="rauchbier" class="tab-panel">
          <h2>Dodaj novu uslugu</h2>
             <div class="form-group">
    <lebal>Title (EN)</lebal>
    <input type="text" class="form-control" name="title">
    </div>
    
      <div class="form-group">
    <lebal>Description (EN)</lebal>
    <textarea class="form-control" name="content"></textarea>
    </div>
        </section>
        <section id="dunkles" class="tab-panel">
          <h2>Tab3</h2>
    
        </section>
      </div>
         <input type="submit" value="Submit">
    
            </form>
    

    WEB.PHP:

    Route::post('/create',[
     'uses' => 'ServicesController@store',
     'as' => 'service.store'
    ]);
    

  2. 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

    <div class="form-group">
       <label for="locale">Locale</label>
       <select id="locale" name="locale" class="form-control">
           <option value="en">English</option>
           <option value="fr">French</option>
       </select>
    </div>
    

    Then in your controller add the following line:

    $post->locale = $request->locale;
    

    Put locale in your $fillable array within the post model.

    Login or Signup to reply.
  3. 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 :

    POST /en/post {payload}
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search