2

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?

2 Answers 2

10

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your quick reply . is there any other way than using static keyword
Welcome @SannySinghs! Check the second example, It's not using static keyword, instead it's using Scope.
0

someone answer me like this .

add following in SessionsController

$type = ...
$data = ...

$email = new Email;
$email->sendMail($type,$data);

1 Comment

Did you check my second example, without static keyword, it's possible using your approach but why not use Laravel style syntax, which is easy to use too, Class::mentodName() even it's not static.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.