找到你要的答案

Q:How can I perform MySQL queries using the Laravel framework?

Q:我如何使用laravel框架执行MySQL查询?

So I recently looked into the Laravel framework as I've been wanting to do for several months and so far It looks great and has some really helpful stuff in it.

However I've been trying to actually use database queries so that I can make use of real data but when I've tried to I haven't had much success.

I've configured the \config\database.php MySQL section, then tried using DB:: and all I got was an error:

the DB class in controllers doesn't exist

I have been reading through all the documentation and found nothing

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use DB;

class SongsController extends Controller {

        public function getSongs(){
        $songs = DB::table('songs')->get();

                return $songs;

        }      

}

View:

<?php  
        foreach ($songs as $song){
    var_dump($song->song);
        }

My mysql in \config\database.php:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'songs'),
    'username'  => env('DB_USERNAME', 'root'),
    'password'  => env('DB_PASSWORD', 'password'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],

所以最近我看着laravel框架,我一直想做几个月,到目前为止,它看起来很好,它有一些真正有用的东西。

然而,我一直在尝试使用数据库查询,这样我就可以使用真实的数据,但当我试图我没有太多的成功。

我已经配置\配置\ database.php MySQL的部分,然后试着用分贝::和所有我得到的是一个错误:

控制器中的DB类不存在

我一直在阅读所有的文件,没有发现任何东西

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use DB;

class SongsController extends Controller {

        public function getSongs(){
        $songs = DB::table('songs')->get();

                return $songs;

        }      

}

观:

<?php  
        foreach ($songs as $song){
    var_dump($song->song);
        }

我\配置\ database.php MySQL:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => env('DB_HOST', 'localhost'),
    'database'  => env('DB_DATABASE', 'songs'),
    'username'  => env('DB_USERNAME', 'root'),
    'password'  => env('DB_PASSWORD', 'password'),
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'strict'    => false,
],
answer1: 回答1:

In your mysql config:

'database'  => env('DB_DATABASE', 'songs'),

It says: look if enviroment variable is set, if it exists in your .env file (in your root), use it, if not, use default "songs".

http://laravel.com/docs/5.0/configuration#environment-configuration

在你的MySQL的配置:

'database'  => env('DB_DATABASE', 'songs'),

它说:如果环境变量设置,如果它存在于你的。Restore.env文件(在你的根),如果不使用它,使用默认的“歌”。

HTTP:/ / laravel。COM /文档/ 5 /配置#环境配置

answer2: 回答2:

You need to add use DB; in the top of your file. Remember Laravel5 uses namespaces.

<?php namespace App\Http\Controllers;
use DB;

您需要在文件的顶部添加使用DB。记得laravel5使用命名空间。

<?php namespace App\Http\Controllers;
use DB;
php  mysql  laravel  laravel-5