Laravel 模型使用scope前缀的方法

Laravel中模型中可以定义scope开头方法,这类方法可以模型直接使用。这类方法也称作查询作用域

例子:

现有Post模型,内部定义一个scopeTitle()方法

    public function scopeTitle($query, $title="") {
        return $query->where('title', $title);
    }

第一个参数不可省略。 第二个可以调用时传入。
在控制器中使用该方法

$posts = Post::where('id', '<', 3)->title('test 2')->orderBy('id', 'desc')->get();
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vue 的模板中,`<slot>` 元素用于将父组件中的内容传递给子组件进行渲染。当父组件中有多个插槽时,可以使用 `name` 属性来标识每个插槽。而子组件中可以使用 `slot` 元素来指定插槽的位置。 有时候,我们需要在插槽中使用子组件中的数据或方法,此时就需要用到 `slot-scope` 或 `scope`。 `slot-scope` 是 Vue 2.x 中提供的一个特殊属性,它允许我们在父组件中访问子组件的数据。它的语法如下: ```html <template> <child-component> <template slot-scope="props"> <p>{{ props.message }}</p> <button @click="props.handleClick">Click me</button> </template> </child-component> </template> ``` 在上面的例子中,子组件会将 `message` 和 `handleClick` 两个数据传递给父组件中的插槽。在插槽中,我们使用 `slot-scope` 来声明一个名为 `props` 的变量,用于访问子组件中的数据和方法。 在 Vue 2.6.0 之后,`slot-scope` 被废弃了,取而代之的是 `v-slot` 和 `#`。使用 `v-slot` 或 `#` 的语法如下: ```html <template> <child-component> <template v-slot:default="props"> <p>{{ props.message }}</p> <button @click="props.handleClick">Click me</button> </template> </child-component> </template> ``` 在上面的例子中,我们使用 `v-slot` 来声明一个名为 `default` 的插槽,并使用 `props` 来访问子组件中的数据和方法。另外,也可以使用 `#` 来简化语法: ```html <template> <child-component> <template #default="props"> <p>{{ props.message }}</p> <button @click="props.handleClick">Click me</button> </template> </child-component> </template> ``` 需要注意的是,使用 `v-slot` 或 `#` 时,`<slot>` 元素必须放在子组件的根元素中。如果子组件中有多个插槽,可以使用 `name` 属性来标识每个插槽。例如: ```html <template> <child-component> <template #header="props"> <h1>{{ props.title }}</h1> </template> <template #body="props"> <p>{{ props.message }}</p> <button @click="props.handleClick">Click me</button> </template> </child-component> </template> ``` 在上面的例子中,我们使用 `#header` 和 `#body` 来指定不同的插槽,并在插槽中使用 `props` 来访问子组件中的数据和方法
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值