Code Monkey home page Code Monkey logo

Comments (6)

maciekish avatar maciekish commented on May 31, 2024

I should mention that the model works in normal use for example in the edit or index views that i have. Its only eager loading that's broken.

from laravel-translatable.

dimsav avatar dimsav commented on May 31, 2024

The translations are not eager loaded out of the box. You have to add them in the with() method when you need them, as you would do with any other eloquent model.

In your case, I guess it would be something like this:

$game = Game::with('players', 'rounds', 'rounds.questions', 'rounds.questions.translations')->find($game->id);

from laravel-translatable.

maciekish avatar maciekish commented on May 31, 2024

Thanks for the quick answer, however that fetches all translations. Is there any way i can choose to get only a specific language?

I realize i can loop over the result and "fix" that but i don't want to pull more data from the database than necessary.

from laravel-translatable.

dimsav avatar dimsav commented on May 31, 2024

Have a look at the laravel documentation to see how you can limit eager loaded data. Search for "Eager Load Constraints" in the eloquent page.

from laravel-translatable.

dimsav avatar dimsav commented on May 31, 2024

Closing as not relevant to the repository.

from laravel-translatable.

BartHuis avatar BartHuis commented on May 31, 2024

@dimsav Hi, thanks for this module :D but, i get problems with this too. Using with, using constraints, everything.

category controller:

        $category = \App\Category::find(1)->with(['products','products.translations'=>function($query){$query->select(['product_id', 'name'])->where('locale','=','nl');},'categoryFeatures'])->firstOrFail();
        return view('category', ['category'=>$category,'ancestors'=>$ancestors]);

category view:

            @if(! empty($category->products))
                @forelse ($category->products as $product)
                    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 categorybox">
                        <a href="{{$product->product_url}}">
                            <div class="category">
                                @if( ! empty($product->productGallery[0]["thumb_link"]))
                                    <img src="{{$product->productGallery[0]["thumb_link"]}}" />
                                @endif
                                {{ var_dump($product->translations) }} ({{trans('routing.product')}} {{$product->id}})
                            </div>
                        </a>
                    </div>
                @empty
                @endforelse
            @endif

this way i get over 300 queries, one for each translation of every product. When i remove the translation from the view it has only 10 queries on my whole page. so it seems like it's not passing the translations on the forst query?

Bart

from laravel-translatable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.