Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call custom model's method from controller in laravel4

I am trying to call a custom method in my Email model from my SessionsController. This is my model

<?php

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

    public static $rules = array();

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

From my SessionsController I wanna call sendMail method. How am I supposed to call it?

like image 964
Sanny Singhs Avatar asked Jan 26 '26 06:01

Sanny Singhs


1 Answers

You can do it either, using

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

And call from controller

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

Or, you can use Scope (instead of static)

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

And call it from controller

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

Also check this answer.

like image 83
The Alpha Avatar answered Jan 27 '26 19:01

The Alpha



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!