【问题标题】:Get an attribute from one array in Mail blade Template从邮件刀片模板中的一个数组获取属性
【发布时间】:2019-11-24 06:03:43
【问题描述】:

我无法在 Laravel 的 Mail 类中访问属性“price”。我有一个错误

未定义索引:价格(查看:C:\laragon\www\hr-english\resources\views\external__emails\registered-course.blade.php)

我认为问题出在控制器上。我必须查询数据库以检查课程的价格,因为在我的 registered_courses 表中,我有一个与 courses 相关的外键,它返回给我的标题课程及其价格。

当我从查询中获取这些数据并将变量发送到刀片时,会出现顶部显示的错误。

我的控制器

public function store(Request $request)
{
    try {

        $data = $this->getData($request);
        $email = Auth::user()->email;
        $name = Auth::user(); 
        $msg = $data;
        $price = DB::table('courses')->select('price')->where('id', '=', $request['course_id'])->get();

        RegisteredCourse::create($data);
        Mail::to($email)->queue(new RegistCourse($msg, $email, $name, $price));

        return redirect()->route('registeredCourse.index')
                         ->with('sucess_message', 'Registered course was sucessfully added');

    } catch(Exception $exception) {

        return back()->withInput()
                     ->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']);
    }

}

我的邮件

class RegistCourse extends Mailable
{
    use Queueable, SerializesModels;

    public $subject = 'Registered Course';

    public $msg;
    public $email;
    public $name;
    public $price;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($msg, $email, $name, $price)
    {
        $this->msg = $msg;
        $this->email = $email;
        $this->name = $name;
        $this->price = $price;

    }

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

这是我的刀片模板

<body>
    <div class="container">
        <div class="row">
            <div>
                <img src="{{asset('images/logo_leon.png')}}" alt="logo_leon" width="55" id="logo_login"><span style="color:gray">HOLYROOD ENGLISH SCHOOL</span>
            </div>
            <br>
            <div>
                <p>Thank you very much for your purchase, {{$name['name']}}. You have just registered in one of our courses.</p>
                <p>
                   
                    <table>
                        <tr>
                            <th>Name</th>
                            <th>Course</th>
                            <th>Date of purchase</th>
                        </tr>
                        <tr>
                        <td>{{$msg['course_id']}}</td>
                        <td>{{$price['price']}}</td>
                        </tr>
                        
                    </table>
                    
                    </p>   
                </p>
                <p>See you in class. Surely we enjoy learning English.</p>
                <p>If you have any questions, do not hesitate to contact us through any of our contact forms.</p>
                <br>
                <p>Equipo Holyrood English School</p>
            </div>
        </div>
    </div>
</body>
</html>

【问题讨论】:

  • 你的价格是逗号分隔符???
  • 嗨,A.A 诺曼。我不太明白你想说什么。这是我第一个使用 Laravel 的项目。我认为问题与查询生成器的使用有关,因为返回一个数组,但我不知道会发生什么。如果确实将 {{$price['price']}} 更改为 {{$price}} 它会在屏幕上显示一些东西。我刚刚上传了图片。
  • 哪些数据属性不可用?
  • 谢谢,但没用。我有一个异常“不能使用 stdClass 类型的对象作为数组”
  • 添加了一个答案@Noel,关于$price,应该是$price[0]-&gt;price

标签: laravel


【解决方案1】:

在您的代码中:

$data = $this->getData($request);
    $email = Auth::user()->email;
    $name = Auth::user(); // Should be Auth::user()->name (if name exists)
    $msg = $data;
    // The get() method returns an array even if there is one row.
    $price = DB::table('courses')->select('price')->where('id', '=', $request['course_id'])->get();

所以,$price 在视图中应该是$price[0]-&gt;price,或者使用first() 方法而不是get()。所以,名称应该是user模型的属性,Auth::user()会产生一个对象。

【讨论】:

    【解决方案2】:

    注意我用来将数据发送到视图的 '->with' 函数。

    您的邮件类别:

    class SuccessBooking extends Mailable
        {
            use Queueable, SerializesModels;
        
            /**
             * Create a new message instance.
             *
             * @return void
             */
        
            public $booking;
            public $user;
            public $pdf_path;
            public $rideCode;
        
            public function __construct($user, $booking, $pdf_path, $rideCode)
            {
                    $this->booking = $booking;
                    $this->user = $user;
                    $this->pdf_path = $pdf_path;
                    $this->rideCode = $rideCode;
            }
        
            /**
             * Build the message.
             *
             * @return $this
             */
            public function build()
            {
                return $this->from('noreply@coride.com', "Co Ride Receipt")
                    ->view('email/successbookinginvoice')
                    ->attach(public_path($this->pdf_path))
                    ->with([
                    'user' => $this->user,
                    'code' => $this->rideCode,
                    'path' => public_path($this->pdf_path),
                    'booking' => $this->booking,
                    ]
                );
            }
        }
    

    你的刀片模板

    @section('content')
        {{--below we access a particular item in the object user--}}
        <h5>Dear {{$user->firstName}}, congratulations on your successful booking.</h5>
        
        {{--below we just access code, it's not an object--}}
        <h5>Dear {{$code}}, congratulations on your successful booking.</h5> 
    @endsection
    

    【讨论】:

      猜你喜欢
      • 2019-02-23
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 2019-05-27
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 2016-04-20
      相关资源
      最近更新 更多