Comments (10)
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
from laravel-translatable.
Thanks @sdebacker , thats works well, but the query it outputs is a bit strange:
select * from `articles` where (select count(*) from `article_translations` where `article_translations`.`article_id` = `articles`.`id` and `slug` = 'my-slug' and `locale` = 'en') >= 1
It does a bit of a useless subquery imo, I tried to replace it by $query->with(), but that gave some unwanted effects.
from laravel-translatable.
You can also use join if you prefer. Especially for big lists, the query will be faster.
from laravel-translatable.
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:
public static function findByTranslatedField($column, $operator, $value)
{
$instance = new static;
$model = $instance->getNewTranslationInstance(App::getLocale());
$result = $instance::join($model->getTableName(), $instance->getTableName().'.id', '=', $instance->getRelationKey())->where($column, $operator, $value)->get();
echo '<pre>';
var_dump($result);
echo '</pre>';
exit;
}
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?
from laravel-translatable.
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);
}
from laravel-translatable.
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 :).
from laravel-translatable.
Nice
from laravel-translatable.
Hello @sdebacker and @jonasva
I'm new to laravel and tried something similar
ArticleController
public function show(ArticleCategory $articlecategory, $slug)
{
$locale = Lang::locale();
$article = Article::select('articles.id', 'article_translations.slug')
->join('article_translations', 'articles.id', '=', 'article_translations.article_id')
->where('slug', $slug)->withTranslations($locale)
->firstOrFail();
$article->addPageView();
return $article;
}
This only shows
{
"id": 2,
"slug": "yoga-asana",
"page_views": 16,
"translations": [
{
"id": 3,
"article_id": 2,
"title": "Yoga Aasana",
"slug": "yoga-asana",
"subtitle": "Yoga Asanas for a healthy morning",
"content": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
"locale": "en",
"created_at": "2018-03-01 00:00:00",
"updated_at": "2018-03-01 00:00:00"
}
]
}
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?
from laravel-translatable.
@shrikanth003, you can add the required columns in Article::select('articles.id', 'article_translations.slug')
. Please use https://laracasts.com/discuss or https://laravel.io/forum for this kind of questions.
from laravel-translatable.
@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
from laravel-translatable.
Related Issues (20)
- Add Laravel 5.8 support HOT 1
- Saving in translated model instead of saving in original model HOT 4
- Fetching Model by translated field and determine locale HOT 3
- Extra Fields or Properties For Translated Text HOT 2
- Can't translate to Portuguese HOT 3
- Display translated attribute even if no translation for current locale and fallback locale? HOT 3
- Has this package a possibility to get translations of related models HOT 2
- Ability to save in both languages HOT 8
- ID coming back with different data HOT 4
- Query where locale not working properly HOT 2
- What would be the best way to handle admin panel with translations? HOT 4
- how i can custom $translatedAttributes from outside model HOT 2
- Multiple Databases HOT 2
- Fallback to default locale with numeric field is not working as expected HOT 3
- get all and Fallback if translation is missing #23 HOT 2
- Wrong column name HOT 2
- Problem with relations HOT 3
- Prefix for default language HOT 6
- When I change local I don't get the translation HOT 7
- how to change the category translation on the fly? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from laravel-translatable.