php - 如何将关系数据传递给 Laravel 5.3 中的可邮寄件?

标签 php laravel laravel-5.3

为 laravel 使用背包(喜欢它)。

因此,每次用户存储数据时我都有这段代码(FinanceCrudController):

    $finance = $request->all();
    if ($request->input('shouldPay') == 'Yes') {
        Mail::to($request->user())->send(new NewBill($finance));
        return parent::storeCrud();
    } 
    else {
        return parent::storeCrud();
    }

这就是我的 Mailable (NewBill) 的样子:

class NewBill extends Mailable
{
    use Queueable, SerializesModels;


    /**
     * The finance instance.
     *
     * @var Finance
     */
    public $finance;


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

    /**
     * Build the message.
    *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.newbill');
    }
}

然后我可以通过在 newbill.blade.php 中调用 finance 表来传递数据,如下所示:

{{ $finance['name'] }}

但是,我不太清楚的是如何获取关系数据。

我正在使用类别表来处理类别。所以,如果我在 Blade View 中调用类别:

{{ $finance['category_id'] }}

我只会获取类别表中字段的 ID 号。

如何在电子邮件中生成实际类别名称而不是字段 ID?

最佳答案

在您的可邮寄类上定义的任何公共(public)属性都将自动提供给 View 。所以,你可以这样做:

public $finance;
public $category;

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

public function build()
{
    $this->category = \App\Category::where('id', $this->finance['category_id'])->first();
    return $this->view('emails.newbill');
}

然后在 View 中使用$category->name访问类别名称。

https://laravel.com/docs/5.3/mail#view-data

关于php - 如何将关系数据传递给 Laravel 5.3 中的可邮寄件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41325710/

相关文章:

PHP-获取数组中的项目数

php - session_destroy() 根本不起作用

javascript - 从同一文件获取 JSON 数据

mysql - 伪造无序数据包

php - 预测/纠正全文搜索

javascript - 未找到 Angular 异步验证器对象

Laravel 5.3 如何在通知电子邮件中显示用户名

php - 根据 laravel 中的相同类别制作 'Related To' 部分

php - 如何在 html 文件中搜索简单字符串?

php - Laravel 5.3 API 中的搜索功能