【发布时间】:2015-06-21 12:15:31
【问题描述】:
嗯,我对 Laravel 非常陌生。所以我实际上是在学习 Laracast 的基础知识。我有一张叫songs的表,型号Song.php如下:
<?php namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent{
}
还有一个控制器SongsController.php:
use App\Song;
use DB;
class SongsController extends Controller
{
function index() {
$songs = Song::get();
$songs2 = DB::table('songs')->get();dd($songs2);
//$mysong2 = Song::whereSlug('as-long-as-you-love-me')->first();
}
}
所以我希望 $songs 和 $songs2 相同并产生相同的结果。但dd($songs) 产生如下结果:
Collection {#146 ▼
#items: array:1 [▼
0 => Song {#147 ▼
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:6 [▼
"id" => "1"
"title" => "As Long as you Love me"
"slug" => "as-long-as-you-love-me"
"lyrics" => null
"created_at" => "2015-06-21 00:00:00"
"updated_at" => "2015-06-21 00:00:00"
]
#original: array:6 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]
}
和$songs2 产生(我认为是实际的,但不确定):
array:1 [▼
0 => {#145 ▼
+"id": "1"
+"title": "As Long as you Love me"
+"slug": "as-long-as-you-love-me"
+"lyrics": null
+"created_at": "2015-06-21 00:00:00"
+"updated_at": "2015-06-21 00:00:00"
}
]
所以我想知道我应该使用哪一个?我做错了什么吗?
【问题讨论】: