发送包含 Laravel Nova 操作的电子邮件

Send a email with Laravel Nova Actions

我想通过 Laravel Nova 上的按钮发送邮件并执行操作(我认为这是最合适的)。 我已经在 mailable 中保存了一个邮件模板,我已经制作了我的资源,但我不知道要放什么,因为我需要从该资源中检索信息,例如名称、价格或日期行已创建(在我的 table 中与我的资源相关)。

我的资源代码:

<?php

namespace App\Nova;

use App\Image;
use Gloudemans\Shoppingcart\Cart;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Nova\Fields\Currency;
use Laravel\Nova\Fields\Date;
use Laravel\Nova\Fields\Heading;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;
use Techouse\IntlDateTime\IntlDateTime;

class Order extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Order::class;

    public static $group = 'Paramètres';

    public static function label()
    {
        return __('Commandes');
    }
    public static function singularLabel()
    {
        return __('Commande');
    }

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'id';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id',
    ];

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make()->sortable(),
            Text::make('Nom du client', 'name'),
            IntlDateTime::make('Passée le', 'created_at')
                ->locale('fr')
                ->readonly(),
            Currency::make('Prix', 'total')->currency('EUR'),
            Text::make('Mode de paiement', 'gateway')
                ->readonly(),
            Text::make('Numéro de téléphone', 'phone'),
            Heading::make('Adresse de livraison'),
            Text::make('Adresse', 'address')
                ->hideFromIndex(),
            Text::make('Code postal', 'postal_code')
                ->hideFromIndex(),
            Text::make('Ville', 'city')
                ->hideFromIndex(),
            Text::make('Pays', 'country')
                ->hideFromIndex(),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}

我的邮寄代码:

<?php

namespace App\Mail;

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

class ResendOrder extends Mailable
{
    use Queueable, SerializesModels;

    /**
     * Elements de contact
     * @var array
     */
    public $contact;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(Array $contact)
    {
        $this->contact = $contact;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.orderconfirmation')
            ->subject("Confirmation de commande");
    }
}

最后是我的操作代码:

<?php

namespace App\Nova\Actions;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;

class EmailOrderConfirmation extends Action
{
    use InteractsWithQueue, Queueable;

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public $name = 'Renvoyer le mail de confirmation de commande';

    public function handle(ActionFields $fields, Collection $models)
    {
        return Action::message('Mail envoyé');
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        return [];
    }
}

我真的不知道去哪里,用什么,如果你能赐教,我将不胜感激,谢谢!

  // in EmailOrderConfirmation --nova action
  // declare what you are using
  // use Illuminate\Support\Facades\Mail;
  // use App\Mail\ResendOrder;
  
  public function handle(ActionFields $fields, Collection $models)
  {
        //loop over the orders that have been selected in nova
        foreach ($models as $order) {
            $contact = $order->contract; //however you are getting contract data
            //assuming you have a $order->user  order belongs to user relationship
            //send mail to the user, with the order/contract details to create your email
            Mail::to($order->user->email)->send(new ResendOrder($contact));
        }
        //return a message to nova
        return Action::message('Mail envoyé');
    }

    // in  Order /Nova resource
    // declare what you are using
    // use App\Nova\Actions\EmailOrderConfirmation;
      
    public function actions(Request $request)
    {
        return [new EmailOrderConfirmation];
    }