gpt4 book ai didi

php - 从 laravel 4 中的 Controller 调用自定义模型方法

转载 作者:行者123 更新时间:2023-12-02 06:18:10 24 4
gpt4 key购买 nike

我正在尝试从我的 SessionsController 调用我的 Email 模型中的自定义方法。这是我的模型

<?php

class Email extends Eloquent {
protected $guarded = array();

public static $rules = array();

public function sendMail($type,$data)
{
echo "yes";
}
}

从我的 SessionsController 我想调用 sendMail 方法。我应该怎么调用它?

最佳答案

你也可以这样做,使用

class Email extends Eloquent {
public static function sendMail($type, $data)
{
//...
}
}

然后从 Controller 调用

Email::sendMail('someType', $dataArray);

或者,您可以使用Scope(而不是static)

class Email extends Eloquent {
public function scopeSendMail($query, $type, $data)
{
// You can use $query here
// i.e. $query->find(1);
}
}

然后从 Controller 调用它

Email::sendMail('someType', $dataArray);

同时检查 this answer .

关于php - 从 laravel 4 中的 Controller 调用自定义模型方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com