Home  >  Article  >  Backend Development  >  Laravel novice problem of obtaining single row data

Laravel novice problem of obtaining single row data

WBOY
WBOYOriginal
2016-08-18 09:15:34994browse

Getting a single row of data, but returning so much data


Your own code

<code>public function index(){
    var_dump(AdminUser::find(1));
    return view('admin.sign.index');
}</code>

Model definition

<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>

Returned data
`

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) }`

Reply content:

Getting a single row of data, but returning so much data


Your own code

<code>public function index(){
    var_dump(AdminUser::find(1));
    return view('admin.sign.index');
}</code>

Model definition

<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>

Returned data
`

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) }`

It is recommended that you use dd(AdminUser::find(1)) to print it out and see. The structure is very clear

This is a single row of data. Is there something wrong? It’s just that the data exists in the form of objects. If you want it to be in the form of an array, you need to follow the toArray method. If you need json, follow the toJson method.

Return the AdminUser object, which is your
class AdminUser extends Model
so that you can easily use AdminUsercustomized and inherited methods from Model
__get and have been defined in Model __set, If you just need to get the attributes, you can also get the value directly
$adminUser->id like a normal object.

Laravel is very powerful, so database queries generally do not directly return an ordinary object or array. These are all to facilitate us to perform some possible subsequent operations. Just don’t be scared by him at the beginning, you will find that it’s really fun after you use it a lot~!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:thinkphp array mergeNext article:thinkphp array merge