0

I'm trying to retrieve data like view and markdown names (not rendering view) example: emails.user.welcome for each mailable class in the app/Mail directory.

Var dump UserWelcome mailable class:

Route::get('rendermail', function() {

     $email = ( new App\Mail\UserWelcome() );

     return dd($email);

});

Var dump output

UserWelcome {#441 ▼
  +locale: null
  +from: []
  +to: []
  +cc: []
  +bcc: []
  +replyTo: []
  +subject: null
  #markdown: null
  #html: null
  +view: null
  +textView: null
  +viewData: []
  +attachments: []
  +rawAttachments: []
  +callbacks: []
  +connection: null
  +queue: null
  +chainConnection: null
  +chainQueue: null
  +delay: null
  +chained: []
Yassine Qoraiche
  • 1,077
  • 14
  • 32

1 Answers1

-1

routes/web.php

$return = [];
$files = scandir("./Mail");
foreach ($files as $key => $value) {
    $fullFileName = explode(".", $value);
    if ($fullFileName[1] === "php") {
        $fileName = $fullFileName[0];
        $className = "App\Mail\". $fileName ."()";

        array_push($return, (new $className)->returnArray());
    }
}
return $return;

Add a returnArray method in all MailableClasses

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class testMail extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->form = "Test";
        $this->view = "home";
        $this->subject = "home";
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function returnArray()
    {
        return [$this->view];
    }
}
  • 1
    Yes, you are right, but you don't provide any solution, I think this should be a comment, not an answer. – Yassine Qoraiche Nov 26 '18 at 19:15
  • I cannot comment because of missing reputation. Moreover I dont know nothing about your UserWelcome::class but you could do somithing like: $email = App\Mail\UserWelcome::all();. In the case it is a Model the method should already exist, otherwise you have to write you own method – Artur Smolen Nov 26 '18 at 19:20
  • I don't that's possible since the mailable class is not a model, do you have any idea about creating a method App\Mail\UserWelcome::all() to get view name? – Yassine Qoraiche Nov 26 '18 at 19:28
  • Unfortunately, this is not what I want to achieve, this will only list files and return class names inside the app\mail path. my goal is to get the view names from the mailable class defined in the build method `$this->view('emails.orders.shipped');`. – Yassine Qoraiche Nov 26 '18 at 19:59
  • No, this should return a new Intstance of each .php-file in the app\Mail dir. I have not tested it but regarding to https://stackoverflow.com/questions/534159/instantiate-a-class-from-a-variable-in-php it should work – Artur Smolen Nov 26 '18 at 20:04
  • You are writing, that you want to receive the data for each mailable class in the app/Mail directory. The markdown and view will be displaying by returning a new Instance like you have already done, ONLY if you have predefined this values as described – Artur Smolen Nov 26 '18 at 20:21
  • i appreciate your help, but I don't have a problem with returning the class instance, but I want to get the mailable class defined view/markdown name in the build method example: `emails.user.welcome`. – Yassine Qoraiche Nov 26 '18 at 20:32