Use same views for multiple controllers

Published 6 months ago by dsmI

Hi, I wanted to ask if it is possible to reuse index, show, edit, create views if multiple controllers accrued. For my dictionary I've split the controllers per language and what I want is to not have to keep creating views for new languages.

Can u please also provide me with an example. Thank you!

Snapey
Snapey
6 months ago (960,075 XP)

when you return from the controller, you just specify which view you want.

As long as you standardise on the variables that the view needs then you can specify the same named views in all controllers.

dsmI
dsmI
6 months ago (7,350 XP)

I've added views already in all of my controllers. But for example, if I want to create a word in a particular language I need to specify which controller performs the store action for that particular language, as I am using multiple databases to separate them. Same with the show view. There I need to specify either $english->name or $spanish->name. I tried to use an if with url() to make them show separately when url is english/word_name, but it does not work.

Snapey
Snapey
6 months ago (960,075 XP)

Sorry then, I can't follow your issue.

Is your problem with storing the word or showing the query

larel_b

Hmm, this is hard to follow. Are you saying that you need to create links in the view with different locations based on the language?

dsmI
dsmI
6 months ago (7,350 XP)

I have three languages, each language has it's own database. Each language has also it's own controller and model. But for those I want to use single view, not to have to create 3x index, show, edit, create views.

For the index view I want a collection of the words per language when the url is either english or spanish. Same for the show view, where I want to show the single word when the url is either english or spanish. Not to show two words from two different languages.

I tried this for the index, it's working but not for show, edit and create.

@if($url == url('english'))
    @foreach($english as $en)
        {{ $en->name }}
    @endforeach
    @elseif($url == url('spanish'))
        @foreach($spanish as $es)
            {{ $es->name }}
        @endforeach
@endif
larel_b

Why not just call them $words (since they're all words) and pass them to the view?

In the different controllers:

return('same.view')->with('words', $spanish_words);

or

return('same.view')->with('words', $english_words);

In the view:

@foreach($words as $word)
    {{ $word->name }}
@endforeach
Snapey
Snapey
6 months ago (960,075 XP)

Have each controller pass $nativeWord and $translatedWords

Your view can show both without caring which direction the translation is in.

If you show multiple words on a page then translatedWords could be an array with the language and the word.

Your view could know what is the native language because the controller can pass it a variable to the view - no need to mess about sniffing the URL

dsmI
dsmI
6 months ago (7,350 XP)

Simply because a language has 100k words and it's better practice to split them for better monitoring of the database. It gets extreme heavy if you have 300k words in one database.

Snapey
Snapey
6 months ago (960,075 XP)

But thats irrelevant. Your controller is the one passing the word to the view.

larel_b

@dsml Just pass in the correct words from the controller and the view will display them. Make sure you use the same variable name when passed to the view as I wrote above.

dsmI
dsmI
6 months ago (7,350 XP)

Yes, but they're controllers, one for Spanish and one for English. Both showing, editing and creating words in two different languages. If the languages grow up to six I have to repeat myself and create more views for each controller. And that's what I simply want to prevent. Therefore I asked if I can use one single view for the curds for the two different controllers which are performing actions per language.

Snapey
Snapey
6 months ago (960,075 XP)
    @foreach($thisLanguage as $from)
        {{ $from->name }}
    @endforeach
    
    @foreach($otherLanguage as $to)
            {{ $to->name }}
    @endforeach

no specific languages mentioned here so it can be used by either controller as long as they pass $thisLanguage and $otherLanguage to the view

dsmI
dsmI
6 months ago (7,350 XP)

@Snapey if I do it this way and go to my url and write /english and expect to get the collection of the English words I then get an error Undefined variable: $to. I need to output just the words for the particular language.

larel_b

@dsmI I think either you are overcomplicating this or maybe I just don't understand. You can use the exact same view for multiple controllers. Just return the same view for different controllers as a response. In the view, use the same variable name such as $words and/or $translated_words. Just specify the the from and to:

Look at this:

In the controller:

return view('same.view')->with('$words, $whatever_language_you_want)->with($translated_words, $whatever_language_yo_want);

In the view:

@foreach($words as $word)
    {{ $word->name }}
@endforeach

@foreach($translatedWords as $tWord)
        {{ $tWord->name }}
@endforeach

If you don't need both to show, then just use:

In the controller:

return view('same.view')->with('$words, $whatever_language_you_want);

In the view:

@foreach($words as $word)
    {{ $word->name }}
@endforeach
dsmI
dsmI
6 months ago (7,350 XP)

@larel_b this is what I did.

use App\Spanish;

public function index()
{
    $data = [
        'english' => English::query()->get(),
        'spanish' => Spanish::query()->get()
    ];
    return view('langs.index')->with($data);
}

The same I applied to English. It's working. Will that now work for edit and create? Also do I need to extend my array as the languages grow?

Please sign in or create an account to participate in this conversation.