larel_b

@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!

dsmI
dsmI
3 months ago (7,350 XP)

@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?

larel_b

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

dsmI
dsmI
3 months ago (7,350 XP)

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

larel_b

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.

dsmI
dsmI
3 months ago (7,350 XP)

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

Snapey
Snapey
3 months ago (865,465 XP)

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.

jcmargentina

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.

larel_b

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 }

dsmI
dsmI
3 months ago (7,350 XP)

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

jcmargentina

@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:

Laravel Doc - Localization

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.

Snapey
Snapey
3 months ago (865,465 XP)

@jcmargentina you have the wrong idea. This is nothing to do with localization. Its a translation dictionary

dsmI
dsmI
3 months ago (7,350 XP)

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

Snapey
Snapey
3 months ago (865,465 XP)

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 sign in or create an account to participate in this conversation.