Home > php教程 > php手册 > laravel--Why are the attributes not defined in the model, but the values ​​are taken out? Where do these attributes come from?

laravel--Why are the attributes not defined in the model, but the values ​​are taken out? Where do these attributes come from?

WBOY
Release: 2016-09-20 03:30:23
Original
1601 people have browsed it

Look at this code in laravel model,

<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> getLimitUsersAttribute()
  {
    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$this</span>->user_limit - <span style="color: #800080;">$this</span>-><span style="color: #000000;">user_count;
  }</span>
Copy after login

But there is no definition in the model,

那么user_limit和<span>user_count属性,是从哪里来的?<br>laravel本身一种机制,可以直接在模型当中调用数据库里字段,这个属性就是</span>Lesson模型对应的lession表里的2个字段
Copy after login

使用一下看看,控制器是这样来使用
Copy after login

方法在看一看
Copy after login

那么这样输出的结果应该是65,看看结果吧
Copy after login

Copy after login
<span><br><br></span>
Copy after login
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template