drzfnr0275 2018-08-16 11:47
浏览 12

在连接时使用列作为表名

I want to get some rows from posts table. I use eloquant and, I don't know how do I make it.

I have five tables :

  • suspended_users (foreign_key - article_id -> post_translations.id)
  • post_translations (post_id (posts-reviews or specs) and post_table)
  • posts (mean contents)
  • reviews
  • specs

I want to jump those three tables from suspended_users.

And I tried to below way inside of Suspect model.

But doesn't work!

$this->join("post_translations as pt", "pt.id", "=", "suspended_users.article_id")
            ->join($this->post_type." as p", "p.id", "=", "post_translations.post_id")
            ->select($this->post_type.".*")
            ->get();
  • 写回答

2条回答 默认 最新

  • dongzhang6021 2018-08-16 11:56
    关注

    You can try to use DB::raw function. Try this:

    $this->join(\DB::raw("post_translations as pt"), "pt.id", "=", "suspended_users.article_id")
                ->join(\DB::raw($this->post_type." as p"), "p.id", "=", "post_translations.post_id")
                ->select($this->post_type.".*")
                ->get();
    
    评论

报告相同问题?