0

For example,
there are two tables articles_1 and articles_2,they have the same fields:

id
title
content
slug

When there is one table, I can query an article like this:

public function show($slug)
{
    $article = Article::findOrFail($slug);

    return view('show', compact('article'));
}

Now there are two tables,I want to query an article,how to write the code?

zwl1619
  • 4,002
  • 14
  • 54
  • 110
  • 1
    If all the fields are same, can you describe why are you using two tables ? – Sagar Gautam Sep 03 '17 at 11:00
  • 1
    Perhaps you should address the feedback on [your first question](https://stackoverflow.com/questions/46022462/mysql-how-to-set-the-starting-value-of-auto-increment-field) before asking more questions about the same problem. – Tim Biegeleisen Sep 03 '17 at 11:01
  • @Tim Biegeleisen Thanks,I have voted your comment on that question,and I use string instead of auto-increament number. – zwl1619 Sep 03 '17 at 11:10

2 Answers2

1

you could use UNION

$table1_items = \DB::table('articles_1')
            ->select(\DB::raw("id, title, content,slug"))

$table2_items  = \DB::table('software_items')
            ->select(\DB::raw("id, title, content,slug"))

$results = $table1_items->union($table2_items)->get();
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You can think of creating View at Database layer itself. The View will use UNION to combine results from both table.

MKR
  • 19,739
  • 4
  • 23
  • 33