string(10) "admin_user" ["connection":protected]=&g..."/> string(10) "admin_user" ["connection":protected]=&g...">

Rumah  >  Artikel  >  pembangunan bahagian belakang  >  laravel新手获取单行数据问题

laravel新手获取单行数据问题

WBOY
WBOYasal
2016-08-18 09:15:34959semak imbas

获取单行数据,却返回如何这么多的数据


自己的代码

<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/d5xFWGz0nH3IjIeHyzNcjRjqn3i5W9dTGIvOQB5wtVeSHi" ["status"]=> int(1) ["create_time"]=> int(1483423452) } ["original":protected]=> array(5) { ["id"]=> int(1) ["username"]=> string(7) "xingren" ["password"]=> string(60) "$2y$10$h0t4Hu/d5xFWGz0nH3IjIeHyzNcjRjqn3i5W9dTGIvOQB5wtVeSHi" ["status"]=> 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/d5xFWGz0nH3IjIeHyzNcjRjqn3i5W9dTGIvOQB5wtVeSHi" ["status"]=> int(1) ["create_time"]=> int(1483423452) } ["original":protected]=> array(5) { ["id"]=> int(1) ["username"]=> string(7) "xingren" ["password"]=> string(60) "$2y$10$h0t4Hu/d5xFWGz0nH3IjIeHyzNcjRjqn3i5W9dTGIvOQB5wtVeSHi" ["status"]=> 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的方法
Model中已经定义了__get__set
如果你只是需要获取属性也可以像普通对象一样直接$adminUser->id获取值。

laravel是很强大的,所以数据库的查询一般都不会直接返回一个普通的对象或者是数组,这些都是为了方便我们进行一些可能的后续操作。不要一开始就被他吓到就好,用多了你就会发现真的很爽~!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn