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.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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!
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.
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.
Sorry then, I can't follow your issue.
Is your problem with storing the word or showing the query
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?
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
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
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
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.
But thats irrelevant. Your controller is the one passing the word to the view.
@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.
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.
@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
@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.
@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
@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?
@dsml You could just specify only the languages you need to pass to the view when you display it. For example if you are the Spanish controller and you only need spanish words, just only include:
$spanish_words = Spanish::query()->get();
return view('langs.index')->with('words', $spanish_words);
You don't have to pass it all to the view at once, only what you need at that point in the controller.
The first parameter in the with method allows you to call it something else, but you want to stick with the same name in the view, but maybe more descriptive names in the controller.
Glad to see that it's working bud!
@larel_b If I use $data instead of array, how do I need to specify the action if I want to link to a specific word by using the route function or If I want to use an if statement to output to the screen name or body etc for particular language?
The data array is different every time you send it to the view. I think your logic is not correct for what you want to do. You only need to send what you need. If you are sending words for just a single language using the same view, the view at that time only needs the words for that language. I wouldn't send it as an array with everything in it. Send it as I wrote above.
For example:
In the controller:
$spanish_words = Spanish::query()->get();
return view('langs.index')->with('words', $spanish_words);
In the view (It knows what controller called it, so the $words collection will have the right words in it):
@foreach($words as $word) {{ $word->name }} @endforeach
@larel_b I'm sorry I actually meant the below.
{!! Form::open(['route' => 'english.store']) !!}
For the store and edit view. How can I replace the hard-coded english with the $data variable.
In the controller:
return view('langs.index')->with('words', $spanish_words)->with('language', 'spanish');
In the view:
{!! Form::open(['route' => $language.'store']) !!}
Use the same code in each controller, but only changing the language.
$larel_b Thank you for your help. All it's working fine now. Actually it's very easy. How did I not think of it idk.
Mark one of his answers correct please.
Actually, as you get your head around the fact that things can be indirect (eg a variable holds the name of a model for instance), you will realise that its better to also have a single controller and the language you are using is just data.
my friend ... you are sunking yourself in a glass of water.
:D
you need to refact your code, for a more elegant solution.
If you dont do it now ... you will regret in the future believe me.
Agreed. What happens when there are 100 languages?
@dsml Try this:
Create a Language controller.
In the routes for languages, pass in the language you want say English as a parameter.
In the show method:
public function show($language) { //Query the database using the language }
@jcmargentina And what do I need to refactor? I have two controllers, two tables, two models that extends Model, and thanks to @larel_b I have now single index, show, create, edit view working for both the controllers. If you mean refactoring the controllers I need to say am new to Laravel and I don't now how to make a single controller creating things in two different tables in my database. Cause basically I don't want to have languages all mixed up together in my database table. Therefore they need to stay separately.
@dsml , please dont take my comment in a bad way.
I do understand you, nobody here knows Laravel in a complete way at all, I garanted you that.
When I say "refactor", is to Re-Think your solution, maybe someone already had the same problem ... a multi language app is something really common this days.
Also ... did you check the official laravel doc ?
I dont know if you neccesary need a datatable with your keywords ... or not ... but just for you to know Laravel already comes with a multi-language solution ... check this out:
also ... you can find a lot of tutorials well explained in google.
And just in case ... you do need to have all your keywords in a database ... I am sure the Laravel approach will give you a good understanding about how to adjust a scalable solution to your needs.
I hope you solve your problem, wish you the best.
@jcmargentina you have the wrong idea. This is nothing to do with localization. Its a translation dictionary
@jcmargentina @Snapey It is indeed a dictionary. It will serve for English and Spanish and it will contain all of the words in English and Spanish. That's more than 100k per language, so that's the reason I created to separate controllers to store the words in to different tables, rather than going with one and end up with a table with more than 200k records in it.
rather than going with one and end up with a table with more than 200k records in it.
you keep saying that like its an issue to have that many rows.
But thats not what I said. I said the controller should not need to be aware of the language. It can be told what model to use when it is instantiated.
Please or to participate in this conversation.