【发布时间】: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]->price;
标签: laravel