Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Gabotronix's avatar

Can't use PHP trait inside laravel model

Hi everybody, I have a trait I use to send mails using mailgun PHP sdk, I'm getting the following error when I try to send mails:

 Trait 'App\Models\sendMailgunSdkApi' not found

This is my Discount model where I use the trait inside sendDiscountMail1 method,

<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use App\Models\DiscountCode;
use App\Models\Order;
use Carbon\Carbon;
use App\Models\Scopes\Discounts;
use Mailgun\Mailgun;
use App\Traits\SendMail;


class Discount extends Model
{
    
    
    use Discounts, sendMailgunSdkApi;

      public static function sendMail1($order)
    {
        
        $mailData = 
        [
            'order' => $order
        ];

        $mail = $this->sendMailgunSdkApi('emails.discounts.discount-mail-1', $mailData, 'Disfruta de tu nuevo descuento en '.config('app.name').'.', '[email protected]');
        
        return response()->json([
            'mail' => $mail,
        ]);
    }


}

Why is it trying to look for a trait in my models folder?

My trait just in case:

<?php
namespace App\Traits;


trait SendMail
{


    public function sendMailgunSdkApi($view, $mailData, $subject, $to)
    {
        $html = view($view, compact('mailData'))->render();

        $result = app(Mailgun::class)->messages()->send(config('mail.mailgun.domain'), [
            'from' => config('mail.from.name').' <'.config('mail.from.address').'>',
            'to' => $to,
            'subject' => $subject,
            'html' => $html,
        ]);
        
        return $result;
    }

    

    
}
0 likes
9 replies
Sinnbeck's avatar

Because it goes by the namespace of the model. WHat is the correct namespace of the trait?

Add this to the above the class (replace with correct namespace)

use My\Namespace\sendMailgunSdkApi;
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Thanks for the update.. You are importing it wrong. You need to use the trait class name, not the method :)

<?php
namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use App\Models\DiscountCode;
use App\Models\Order;
use Carbon\Carbon;
use App\Models\Scopes\Discounts;
use Mailgun\Mailgun;
use App\Traits\SendMail;


class Discount extends Model
{
    
    
    use Discounts, SendMail;

      public static function sendMail1($order)
    {
        
        $mailData = 
        [
            'order' => $order
        ];

        $mail = $this->sendMailgunSdkApi('emails.discounts.discount-mail-1', $mailData, 'Disfruta de tu nuevo descuento en '.config('app.name').'.', '[email protected]');
        
        return response()->json([
            'mail' => $mail,
        ]);
    }


}
Gabotronix's avatar

You were right about that, but now I'm getting this error:

Using $this when not in object context
Gabotronix's avatar

Hi, this is unrelated but can you use CSS classes in your blade templates, I currently have to resort to inline styles but I feel this is not a good aproach for ongterm.

Please or to participate in this conversation.