在mailable / markdown Laravel中传递变量

时间:2018-04-14 21:07:04

标签: php laravel email laravel-5 laravel-5.6

我正在尝试将此故障单的收集变量发送到Laravel中的降价电子邮件。

enter image description here

我收到此错误

  

“试图获取非对象的属性”并且无法弄清楚如何   将所有变量传递到我的降价电子邮件中。

以下是我要采取的步骤:

路线:

Route::post('/admin', 'AdminController@emailOpenStoreTicket')->name('email-open-store');

表单以收集票证数据:

<form action="{{route('email-open-store')}}" method="POST" enctype="multipart/form-data">
@csrf
{{method_field('post')}}
<input type="email" name="staff_email" required>
<textarea name="rec_message" required></textarea> 
<input type="hidden" name="subject" value="{{ $ot->subject}}">
...etc
<input type="hidden" name="street" value="{{ $ot->street }}">
<button type="submit">Send</button>
</form>

管理员控制器:

public function emailOpenStoreTicket(Request $request)
{
$validatedData = $request->validate([
'staff_email' => 'required|email',
'rec_message' => 'required|min:2'
]);
$ticket = new EmailOpenTicket;
$ticket->staff_email=$request->staff_email;
$ticket->rec_message=$request->rec_message;
$ticket->subject=Purifier::clean($request->subject);
$ticket->content=Purifier::clean($request->content);
$ticket->username=$request->username;
$ticket->user_id=$request->user_id;
$ticket->street=$request->street;
$ticket->first_name=$request->first_name;
$ticket->last_name=$request->last_name;
$ticket->email=$request->email;
$ticket->file_name=$request->file_name;

Mail::to($ticket->staff_email)->send(new EmailOpenTicket);
return back()->with('success','Your email message has been sent.');
}

邮件类如果我使用:

  public function __construct(Ticket $ticket)
{
  $this->ticket = $ticket;
}

错误如下:

  

类型错误:函数参数太少   传递了App \ Mail \ EmailOpenTicket :: __ construct(),0   第71行的app \ Http \ Controllers \ AdminController.php正好为1   预期

以上管理控制器中的以下行:$ticket = new EmailOpenTicket;

namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Ticket;
use App\Admin;

class EmailOpenTicket extends Mailable
{
    use Queueable, SerializesModels;
    public $ticket;
    /**
     * Create a new message instance.
     *
     * @return void
     */
    // public function __construct(Ticket $ticket)
    // {
    //     $this->ticket = $ticket;
    // }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->subject('New Open Maintenance Ticket')->markdown('admin.tickets.emailopenticket');
    }
}

然后是我的电子邮件视图:

我看到如果我添加['ticket' => $ticket],它将有助于提取变量...在这种情况下,它会跳过['ticket' => $ticket]并在下一行出错:

  

尝试获取非对象的属性

@component('mail::message', ['ticket' => $ticket])
#{{$ticket->rec_message}} 

 {{$ticket->first_name}} {{$ticket->last_name}} has submitted a new maintenance ticket.
Please review and complete as soon as possible

@component('mail::panel')
**Details:**
- **Ticket Type:** Maintenance Request
- **Ticket Number:** {{$ticket->st_id}}
- **Subject:** {{$ticket->subject}}
..etc

我已经在这2天了。如果有人可以伸出援助之手,将不胜感激

1 个答案:

答案 0 :(得分:2)

如果您的<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $('#register').click(function() { var name=$("#name").val(); var writer=$("#writer").val(); var publisher_name=$("#publisher_name").val(); var publisher_date=$("#publisher_date").val(); var description=$("#description").val(); var pages_num=$("#pages_num").val(); var image=$("#image").val(); var token=$("#token").val(); $.ajax({ type: "POST", data: "name" + name + "&writer" +writer + "&publisher_name" + publisher name + "&publisher_date" + publisher_date + "&description" + description + "&pages_num" + pages_num + "&image" + image + "&token" + token, url: 'test/store', }); }); }); </script> 班级&#39;构造函数定义了一个必需的参数,你需要在实例化类时传入它。

if (jsonObjectjsonObject.getJSONArray("weather").getJSONObject(0).has("id")) {
        weatherData.mCity = jsonObject.getString("name");
        weatherData.mIconname = updateWeatherIcon(weatherData.mCondition);
    }

此外,当您发送Mailable时,请确保传入您已创建的实例并将数据分配给。在您的代码示例中,您将实例化第二个EmailOpenTicket类。

// Assuming $ticket is already defined as a Ticket object:
$email = new EmailOpenTicket($ticket);

提供一些额外的建议:似乎EmailOpenTicket已经是数据库中包含数据的模型。如果是这种情况,您并不需要通过表单发送所有数据。您所需要的只是您要发送的故障单的ID。然后,您的控制器可以找到该票证,将其附加到Mailable,然后继续前进。

形式:

Mail::to($ticket->staff_email)->send($ticket);

控制器:

Ticket

可邮寄:

<form action="{{route('email-open-store')}}" method="POST" enctype="multipart/form-data">
    @csrf
    {{method_field('post')}}
    <input type="email" name="staff_email" required>
    <textarea name="rec_message" required></textarea> 
    <input type="hidden" name="ticket_id" value="{{ $ot->id }}">
    <button type="submit">Send</button>
</form>