取得單行數據,卻回傳如何這麼多的數據
自己的程式碼
<code>public function index(){ var_dump(AdminUser::find(1)); return view('admin.sign.index'); }</code>
模型定義
<code><?php namespace App; use Illuminate\Database\Eloquent\Model; class AdminUser extends Model { protected $table='admin_user'; //验证正好密码 public function check_admin($username,$pwd){ $admin_user=$this->where('username',$username)->select(['password'])->first(); return $admin_user; } }</code>
回傳的資料
`
object(AppAdminUser)#207 (24) { ["table":protected]=> string(10) "admin_user" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(5) { ["id"]=> int(1) ["username"]=> string(7) "xingren" ["password"]=> string(60) "$2y$10$h0t4Hu/d5xFWGz0nH3IjIeHyzNcjRjqn3i5W9dTGIvOQB5xFWGz0nH3IjIeHyzNcjRjqn3i5W9dTGIvOQB5wtVeSHi" ["status"]=> int(1) ["create_time"]wtVeSHi" ["status"]=> int(1) ["create_time"]wtVeSHi" ["status"]=> int(1) ["create_time"]wtVeSHi" ["status"]=> int(1) ["create_time"] [M5(1483): 485] ["id"]=> int(1) ["username"]=> string(7) "xingren" ["password"]=> string(60) "$2y$10$h0t4Hu/d5xFWGz0nH3IjIeHyzNcjRjqn3GIi5WdTT; => int(1) ["create_time"]=> int(1483423452) } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } [" visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected] => array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts": protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array (0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) }`
取得單行數據,卻回傳如何這麼多的數據
自己的程式碼
<code>public function index(){ var_dump(AdminUser::find(1)); return view('admin.sign.index'); }</code>
模型定義
<code><?php namespace App; use Illuminate\Database\Eloquent\Model; class AdminUser extends Model { protected $table='admin_user'; //验证正好密码 public function check_admin($username,$pwd){ $admin_user=$this->where('username',$username)->select(['password'])->first(); return $admin_user; } }</code>
回傳的資料
`
object(AppAdminUser)#207 (24) { ["table":protected]=> string(10) "admin_user" ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(5) { ["id"]=> int(1) ["username"]=> string(7) "xingren" ["password"]=> string(60) "$2y$10$h0t4Hu/d5xFWGz0nH3IjIeHyzNcjRjqn3i5W9dTGIvOQB5xFWGz0nH3IjIeHyzNcjRjqn3i5W9dTGIvOQB5wtVeSHi" ["status"]=> int(1) ["create_time"]wtVeSHi" ["status"]=> int(1) ["create_time"]wtVeSHi" ["status"]=> int(1) ["create_time"]wtVeSHi" ["status"]=> int(1) ["create_time"] [M5(1483): 485] ["id"]=> int(1) ["username"]=> string(7) "xingren" ["password"]=> string(60) "$2y$10$h0t4Hu/d5xFWGz0nH3IjIeHyzNcjRjqn3GIi5WdTT; => int(1) ["create_time"]=> int(1483423452) } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } [" visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected] => array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["casts": protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array (0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) }`
建議你用 dd(AdminUser::find(1))
印出來看看 這樣看的結構很清晰
這就是單行數據,哪裡不對嗎,只是這些數據以對象的形式存在,如果你希望是數組的形式,需要跟toArray方法,如果是需要json,跟toJson方法。
返回AdminUser對象,就是你上面的class AdminUser extends Model
這樣你可以輕鬆的使用AdminUser
自訂的和繼承自Model
的方法
ModeMode __set
,如果你只是需要取得屬性也可以像普通物件一樣直接
$adminUser->id取得值。
laravel是很強大的,所以資料庫的查詢一般都不會直接回傳一個普通的物件或是數組,這些都是為了方便我們進行一些可能的後續操作。不要一開始就被他嚇到就好,用多了你就會發現真的很爽~!