-
Notifications
You must be signed in to change notification settings - Fork 321
Fetching model by translated field #44
Comments
I use whereHas: $query->whereHas(
'translations',
function ($query) use ($slug) {
$query->where('slug', $slug);
$query->where('locale', App::getLocale());
}
); You can put this code in an eloquent query scope |
Thanks @sdebacker , thats works well, but the query it outputs is a bit strange:
It does a bit of a useless subquery imo, I tried to replace it by $query->with(), but that gave some unwanted effects. |
You can also use join if you prefer. Especially for big lists, the query will be faster. |
I tried using the join method, but I'm having problems properly accessing attributes from the returned object afterwards. I was adding a new method in the Translatable model to test:
It returns an object of type 'Illuminate\Database\Eloquent\Collection'. In the case I'm testing with, I get 1 result back so I just access it with $result[0]. When I vardump that, I can get an attribute + its correct result by accessing $result[0]->getAttributes()['myfieldname']. But if I just do $result[0]->myfieldname, it returns NULL. Any idea about that? |
Why not rewrite your first attempt like this: public function showArticle($slug)
{
$article = Article::select('articles.id', 'article_translations.slug')
->join('article_translations', 'articles.id', '=', 'article_translations.article_id')
->where('slug', $slug)
->firstOrFail();
return \View::make('blog::blog.show')->with('article', $article);
} |
I wrote that method in the Translatable model so I don't have to remake this kind of query for different translated models. I adapted my code a bit to work like your solution with the Model::select structure and it solves the problem, thanks :). |
Nice |
Hello @sdebacker and @jonasva I'm new to laravel and tried something similar ArticleController
This only shows
I have more fields on the Articles table like category_id, image, created_by, updated_by They do not show up. When i use ->with('category') it shows null How to solve this? |
@shrikanth003, you can add the required columns in |
@sdebacker Thanks for replying. I have used join tables instead of select and it displays the complete table fields. Ah ok, i was using this package, so i thought i should ask here. Here on i should laracasts |
I have a model Article with an ArticleTranslation model to store its translations. ArticleTranslation has a field called 'slug' which I would like to use to navigate to my blog articles. Something like example.com/blog/slug-to-an-article-in-english. (same approach as somewhat every blog site out there right). Now when someone hits that URL, I would have to fetch an article object based on the translated slug.
So I have this in my controller for that:
When I run that query I get the following error:
"Unknown column 'slug' in 'where clause' "
The problem is that by using the Model::where only allows me to search into the Model itself but not by fields the translated model. I know I could run a query on the ArticleTranslation model, but that would only return an ArticleTranslation object and any fields in the Article model itself would be inaccessible (unless I use additional queries). Would it be possible to add a method that allows you to query on the joined table of a Model + it's ModelTranslation?
In my example that would result in a query like:
select * from articles inner join article_translations on articles.id = article_translations.article_id where slug = 'my-slug' and locale = 'current locale';
The text was updated successfully, but these errors were encountered: