Code Monkey home page Code Monkey logo

Comments (10)

sdebacker avatar sdebacker commented on May 31, 2024

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.

jonasva avatar jonasva commented on May 31, 2024

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.

sdebacker avatar sdebacker commented on May 31, 2024

You can also use join if you prefer. Especially for big lists, the query will be faster.

from laravel-translatable.

jonasva avatar jonasva commented on May 31, 2024

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.

sdebacker avatar sdebacker commented on May 31, 2024

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.

jonasva avatar jonasva commented on May 31, 2024

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.

sdebacker avatar sdebacker commented on May 31, 2024

Nice

from laravel-translatable.

 avatar commented on May 31, 2024

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.

sdebacker avatar sdebacker commented on May 31, 2024

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

 avatar commented on May 31, 2024

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

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.