Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with joined tables #149

Closed
nousename opened this issue May 3, 2020 · 7 comments
Closed

How to deal with joined tables #149

nousename opened this issue May 3, 2020 · 7 comments
Assignees
Labels

Comments

@nousename
Copy link

I have a regions table with a regions_translations - works fine
I have a properties table with a properties_translations - works fine

Where I am stuck at, and cannot find an example anywhere, is how to deal with the region_id in my properties table.
I hoped this would work
Property::withTranslation('region')->where("id", "=", $id)->get();
Unfortunately it does not, and only gets the property translations.
I have not got region name in my property $translatedAttributes
And am joining regions with
public function region() { return $this->hasOne('App\Region', 'region_id', 'region_id'); }
Know doubt it is something simple, but it is driving me mad :)

@Gummibeer
Copy link
Member

Hey,
at first your relationship definition doesn't join. Eloquent runs two separated queries.
So the solution should be as simple as retrieving your property model instance and call the relationship.

$property->region->name;

If name is translated on the region model you will receive the translated value with this.

Why I wanted to clear the join difference - because I will drop every real JOIN support in next major release. The new fallback resolvers are much more worth for me but they prevent from using joins with the same result.

@Gummibeer Gummibeer self-assigned this May 3, 2020
@minhvn
Copy link

minhvn commented May 4, 2020

@Gummibeer can example to load all posts have link to categories and subcategories on index with join or not join. Because have problem with join to display data and filter.
And when join, it's must auto get only current language.
Thank for your project.

@Gummibeer
Copy link
Member

Hey @minhvn ,
I don't have any knowledge about your database but I will try my best to post a code snippet that could work for your described situation.

assumptions

interface Post {
  public function categories(): BelongsToMany;
}

code

// replace by any logic you want to retrieve the wanted categories
$categories = Category::take(3); 

// this will retrieve all posts with a relation to any of the categories
$posts = Post::whereHas('categories', fn(Builder $q) => $q->whereKey($categories->pluck('id')->all()))->get();

// this will create a collection of all translated post titles
$posts->map->title; 

The examples uses PHP7.4 and collection higher order proxies.

@nousename
Copy link
Author

Excellent - just a hint for anyone else looking for this, when using get this does not work, will though with first or find.
$property->region->name;
you need to use
$property[0]->region->name;

When you're using get() you get a collection.
In this case you need to iterate over it to get properties.

@Gummibeer
Copy link
Member

Hey,
that was what I've done by using the higher order map call.

$posts->map->title; 

If you only need the first one the first() method on the query is for sure the right solution and in this case you can use ->title directly on $post.
My example was for @minhvn which I understood the way that he wants a list of posts matching a list of categories?

@minhvn
Copy link

minhvn commented May 8, 2020

Hey,
that was what I've done by using the higher order map call.

$posts->map->title; 

If you only need the first one the first() method on the query is for sure the right solution and in this case you can use ->title directly on $post.
My example was for @minhvn which I understood the way that he wants a list of posts matching a list of categories?

Hi bro, thank for that:
Example: I have 3 tables, Request, Items and Category.

  • Request have some item ref to categories, and have many items.
  • Items have some field ref to category.

How to get All request with translate from category.
Thank @Gummibeer Gum again.

@Gummibeer
Copy link
Member

To query if a model is translated in a given locale the package provides a translatedIn($locale) scope.
https://docs.astrotomic.info/laravel-translatable/package/scopes#translatedin-string-usdlocale-null

Everything else is default Eloquent model and relationship handling.
I suggest checking out the collections pluck(), map(), flatten() and collapse() methods which can help to get elements that are deep nested to the top level of a collection.
You should also eager load all relations from your translated category collection to prevent thousands of small queries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants