我正在Windows中创建一个任务调度程序,只要保证在两个月后到期,就会发送一封电子邮件。运行Trying to get property of non-object
之后我收到异常php artisan schedule:run
这是我创建的命令SendEndRenewal.php
<?php
namespace App\Console\Commands;
use App\Device;
use App\Mail\EndRenewal;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use Illuminate\Console\Command;
class SendEndRenewal extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'send:renewals';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Email ending renewals';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$str = "";
$devices = Device::whereYear('endDate','=',Carbon::now()->year)->whereMonth('endDate','=',Carbon::now()->month+2)->get();
if (!empty($devices)) {
foreach ($devices as $device) {
Mail::to('vjztingson@gmail.com')->send(new EndRenewal($device));
$str .= "sent " . $device->name . " ";
}
}
$this->info($str);
}
}
这是我的电子邮件对象EndRenewal.php
<?php
namespace App\Mail;
use App\Device;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class EndRenewal extends Mailable
{
use Queueable, SerializesModels;
public $device;
/**
* Create a new message instance.
*
* @return void
*/
public function __construct(Device $device)
{
$this->device = $device;
}
/**
* Build the message.
*
* @return $this
*/
public function build()
{
return $this->view('devices.show', ['device'=>$this->device]);
}
}
当我在build()
中的EndRenewal.php
中返回测试空白页时,它会起作用,但是当我传递一个对象并返回该对象的视图时它不起作用。我尝试了protected $device
而不是公开它仍然没有用。请帮忙。谢谢!
我尝试了print_r
它返回的传递对象
App\Device Object
(
[fillable:protected] => Array
(
[0] => name
[1] => serial
[2] => endDate
[3] => startDate
[4] => renewal_id
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 1
[name] => Test Device 1 for Mail
[startDate] => 2017-07-04
[endDate] => 2017-09-04
[created_at] => 2017-07-04 15:07:43
[updated_at] => 2017-07-04 15:07:43
[renewal_id] => 11
[serial] => Test Device 1 for Mail
)
[original:protected] => Array
(
[id] => 1
[name] => Test Device 1 for Mail
[startDate] => 2017-07-04
[endDate] => 2017-09-04
[created_at] => 2017-07-04 15:07:43
[updated_at] => 2017-07-04 15:07:43
[renewal_id] => 11
[serial] => Test Device 1 for Mail
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
这是视图
@extends('layouts.app')
@section('pagetitle', 'Device Information')
@section('content')
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="/css/index.css">
<link rel="stylesheet" type="text/css" href="/css/show.css">
<div class="container">
<div class="flow">
@if (Session::has('message'))
<div class="alert alert-info">{{ Session::get('message') }}</div>
@endif
@if (Session::has('error'))
<div class="alert alert-danger">{{ Session::get('error') }}</div>
@endif
<div class="panel panel-default">
<div class="panel-heading">Renewal Information</div>
<div class="panel-body">
<p><label>Company: </label>{{ $device->renewal->client->company }}</p>
<p><label>Renewal Code: </label>{{ $device->renewal->code }}</p>
<p><label>Contact Number: </label>{{ $device->renewal->client->contactNo }}</p>
</div>
</div>
<div class="panel panel-default">
@if (Session::has('deviceMessage'))
<div class="alert alert-info">{{ Session::get('deviceMessage') }}</div>
@endif
@if (Session::has('deviceError'))
<div class="alert alert-danger">{{ Session::get('deviceError') }}</div>
@endif
<div class="panel-heading">Device</div>
<table class="table table-condensed">
<thead>
<tr>
<th>Name</th>
<th>Serial</th>
<th>Start Date</th>
<th>End Date</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$device->name}}</a></td>
<td>{{$device->serial}}</td>
<td>{{$device->startDate}}</a></td>
<td>{{$device->endDate}}</a></td>
<td>
<a data-toggle="modal" data-target="#editDevice" class="btn btn-primary">Edit Device</a>
@if (Auth::user()->type == 'Admin')
{!! Form::open(['method' => 'DELETE', 'route' => ['devices.destroy', $device, $device->renewal]]) !!}
{!! Form::submit('DELETE', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
@endif
</td>
</tr>
</tbody>
</table>
</div>
@if (Auth::user()->type == 'Admin')
<div class="panel panel-default">
<div class="panel-heading">Client Information</div>
<div class="panel-body">
<p><label>Company:</label>{{ $device->renewal->client->company }}</p>
<p><label>Name:</label>{{$device->renewal->client->firstName}} {{str_limit($device->renewal->client->middleName, 1, '.')}} {{$device->renewal->client->lastName}}</p>
<p><label>Contact No:</label>{{ $device->renewal->client->contactNo }}</p>
<p><label>Address:</label>{{ $device->renewal->client->address }}</p>
<p><label>Website:</label>{{ $device->renewal->client->website }}</p>
<p><label>Industry:</label>{{ $device->renewal->client->industry }}</p>
<a href="{{ route('clients.show', $device->renewal->client) }}" class="btn btn-info">View Profile</a>
</div>
</div>
@endif
</div>
</div>
<div class="modal fade container" id="editDevice">
{!! Form::model($device, [ 'method'=>'PUT', 'route' => ['devices.update', $device] ]) !!}
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Edit Device</h4>
</div>
<div class="modal-body">
<p>
{!! Form::label('name', 'Name:', ['class' => 'control-label']) !!}
{!! Form::text('name', null, ['class' => 'form-control', 'readonly', 'value'=>'{{ $device->name }}']) !!}
</p>
<p>
{!! Form::label('serial', 'Serial Number:', ['class' => 'control-label']) !!}
{!! Form::text('serial', null, ['class' => 'form-control', 'readonly', 'value'=>'{{ $device->serial }}']) !!}
</p>
<p>
{!! Form::label('startDate', 'Warranty Start:', ['class' => 'control-label']) !!}
{!! Form::date('startDate', null, ['value'=>'{{ $device->startDate }}']) !!}
{!! Form::label('endDate', 'Warranty End:', ['class' => 'control-label']) !!}
{!! Form::date('endDate', null, ['value'=>'{{ $device->endDate }}']) !!}
</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success"> Submit </button>
<button type="button" class="btn btn-default" data-dismiss="modal"> Close </button>
</div>
</div>
{!! Form::close() !!}
</div>
@endsection