Help us understand the problem. What is going on with this article?

Laravel4でDBテーブルの値をselectで取り出す方法

More than 5 years have passed since last update.

下記のようなテーブルがあるとする。

mysql> select name from company_basic_info;
+-----------------------------+
| name                        |
+-----------------------------+
| お試し有限会社              |
| サンプル株式会社            |
| なんとか証券会社            |
| 合同会社あいうえお          |
+-----------------------------+
4 rows in set (0.00 sec)

このテーブルから会社名をすべて取り出したい。
その場合は、コントローラに下記メソッドを実装する。

function index(){
    $inData = DB::table('company_basic_info')->get();
    return View::make('piyo')->with('inData',$inData);
}

ここで、piyo は、ビューであり、
views/piyo.php
とする。その中身は下記のように実装する。

<?php
foreach(  $inData as $value ){
    echo $value->name . '<br/>';
}
?>

あとは、routes.php に下記を設定して、

Route::get('hoge/', 'HogeController@index');

http://localhost/hoge
にアクセスして、下記がブラウザに表示されたらOK。

お試し有限会社
合同会社あいうえお
なんとか証券会社
サンプル株式会社
kenichiro-yamato
サーバサイドエンジニア
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした