Home  >  Article  >  Backend Development  >  About laravel's getter: get{xxxx}Attribute and data type issues

About laravel's getter: get{xxxx}Attribute and data type issues

WBOY
WBOYOriginal
2016-08-08 09:06:521430browse

<code>laracast上教学用的方法,成功获取了tag_list</code>

About laravel's getter: get{xxxx}Attribute and data type issues

<code>模仿写的获取role_list,报错了</code>

About laravel's getter: get{xxxx}Attribute and data type issues

<code>如果直接在controller里面这样直接用$user->roles是能成功获取的</code>

About laravel's getter: get{xxxx}Attribute and data type issues

Question:

1. What is the reason for the above three pictures?

2. Can it be solved so that the content found through $id in the traditional resource controller can also be directly called using the xxxx in get{xxxx}Attribute?

3. In the picture below, are the results obtained by these two parameter transfer methods the same structure?

About laravel's getter: get{xxxx}Attribute and data type issues

Reply content:

<code>laracast上教学用的方法,成功获取了tag_list</code>

About laravel's getter: get{xxxx}Attribute and data type issues

<code>模仿写的获取role_list,报错了</code>

About laravel's getter: get{xxxx}Attribute and data type issues

<code>如果直接在controller里面这样直接用$user->roles是能成功获取的</code>

About laravel's getter: get{xxxx}Attribute and data type issues

Question:

1. What is the reason for the above three pictures?

2. Can it be solved so that the content found through $id in the traditional resource controller can also be directly called using the xxxx in get{xxxx}Attribute?

3. In the picture below, are the results obtained by these two parameter transfer methods the same structure?

About laravel's getter: get{xxxx}Attribute and data type issues

<code><?php

/**
 * Created by PhpStorm.
 * User: shellus
 * Date: 2016-08-05
 * Time: 12:57
 */
class Test
{

    /**
     * 魔术方法,访问类成员时,会转向调用到此方法
     */
    function __get($name)
    {
        return call_user_func(array($this, $name));
    }

    public function users(){
        return ['a','b','c'];
    }
}

$test = new Test();
var_dump($test -> users);</code>

Is this what you are asking?

1, as mentioned above, it is obtained through the magic method
2, all subclass instances of model can be used in this way
3, all returned are model subclasses

Why can’t I like it yet

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