需要解决 laravel 5 中的未定义索引错误
Need to solve undefined index error in laravel 5
我正在尝试使用 laravel 5 和 facebook SDK 将 posts 安排到 FB 页面。
我需要在post评论现在或安排稍后评论之间做出选择。
使用下面的代码,我可以安排 post。但是如果我想 post 立即抛出异常:
"ErrorException in FacebookController.php line 118: Undefined index: pub_schedule". Line 118 is "if ($input['pub_schedule'] !=1)"
这是怎么回事?
这是我的表格:
<div class="form-group">
<label class="col-sm-4 control-label">Veröffentlichen auf</label>
<div class="col-sm-8">
{!! Form::label('pub_facebook', 'Facebook') !!}
{!! Form::checkbox('pub_facebook') !!}
{!! Form::label('pub_website', 'Meiner Webseite') !!}
{!! Form::checkbox('pub_website') !!}
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Veröffentlichen am</label>
<div class="col-sm-8">
{!! Form::label('pub_now', 'Sofort Veröffentlichen') !!}
{!! Form::checkbox('pub_now') !!}
{!! Form::label('pub_schedule', 'Für Später Planen') !!}
{!! Form::checkbox('pub_schedule') !!}
</div>
</div>
<div class="form-group">
{!! Form::label('published_at', 'Veröffentlichungsdatum') !!}
{!! Form::input('datetime','published_at', Carbon\Carbon::now()->format('Y-m-d H:i'), ['class' => 'form-control']) !!}
</div>
这是我的 ArticlesController:
public function store(ArticleRequest $request) {
// Only if the user is a FB user then this function will execute.
if(Auth::user()->facebook_user_id != 0)
{
//Checkbox Publish on FB
if($request->pub_facebook !=0) {
//Radio Button Schedule
if ($request->pub_schedule!=0) {
if(strtotime($request->get('published_at')) < strtotime("+11 minutes") ){
// send a message to user to change the scheduled time.
return 'Please schedule you post again: +11 Minutes';
}
} else {
$graphNode = FacebookController::postToFBPage($request->all());
}
}
}
这是我的 Facebook 控制器:
public static function postToFBPage($input, $page_id = '')
{
$page_id == '' ? $page_id = '' : $page_id;
// Creating a new FB object.
$fb = new \Facebook\Facebook([
'app_id' => env('FACEBOOK_APP_ID'),
'app_secret' => env('FACEBOOK_APP_SECRET'),
'default_graph_version' => env('FACEBOOK_GRAPH_VERSION'),
]);
// Sample and some of the possible fields that can be send when posting to FB.
// $linkData = [
// 'link' => 'absolute link here',
// 'message' => 'Main body content here',
// 'picture' => 'path/to/the/image.jpg'
// ];
//to post immediately
if ($input['pub_schedule'] !=1) {
$linkData = [
'message' => $input['title'] . "\n" . $input['body'] . "\n" . $input['price']
];
// to schedule a post to fb
} else {
$linkData = [
'message' => $input['title'] . "\n" . $input['body'] . "\n" . $input['price'] ,
"published" => false,
'scheduled_publish_time' => strtotime($input['published_at']),
];
}
我认为在您的表单中,该字段是可选的。因此,不会设置该值。做这样的事情:
if (isset($input['pub_schedule']) && $input['pub_schedule'] !=1) {
$linkData = [
'message' => $input['title'] . "\n" . $input['body'] . "\n" . $input['price']
];
}
我正在尝试使用 laravel 5 和 facebook SDK 将 posts 安排到 FB 页面。
我需要在post评论现在或安排稍后评论之间做出选择。
使用下面的代码,我可以安排 post。但是如果我想 post 立即抛出异常:
"ErrorException in FacebookController.php line 118: Undefined index: pub_schedule". Line 118 is "if ($input['pub_schedule'] !=1)"
这是怎么回事?
这是我的表格:
<div class="form-group">
<label class="col-sm-4 control-label">Veröffentlichen auf</label>
<div class="col-sm-8">
{!! Form::label('pub_facebook', 'Facebook') !!}
{!! Form::checkbox('pub_facebook') !!}
{!! Form::label('pub_website', 'Meiner Webseite') !!}
{!! Form::checkbox('pub_website') !!}
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label">Veröffentlichen am</label>
<div class="col-sm-8">
{!! Form::label('pub_now', 'Sofort Veröffentlichen') !!}
{!! Form::checkbox('pub_now') !!}
{!! Form::label('pub_schedule', 'Für Später Planen') !!}
{!! Form::checkbox('pub_schedule') !!}
</div>
</div>
<div class="form-group">
{!! Form::label('published_at', 'Veröffentlichungsdatum') !!}
{!! Form::input('datetime','published_at', Carbon\Carbon::now()->format('Y-m-d H:i'), ['class' => 'form-control']) !!}
</div>
这是我的 ArticlesController:
public function store(ArticleRequest $request) {
// Only if the user is a FB user then this function will execute.
if(Auth::user()->facebook_user_id != 0)
{
//Checkbox Publish on FB
if($request->pub_facebook !=0) {
//Radio Button Schedule
if ($request->pub_schedule!=0) {
if(strtotime($request->get('published_at')) < strtotime("+11 minutes") ){
// send a message to user to change the scheduled time.
return 'Please schedule you post again: +11 Minutes';
}
} else {
$graphNode = FacebookController::postToFBPage($request->all());
}
}
}
这是我的 Facebook 控制器:
public static function postToFBPage($input, $page_id = '')
{
$page_id == '' ? $page_id = '' : $page_id;
// Creating a new FB object.
$fb = new \Facebook\Facebook([
'app_id' => env('FACEBOOK_APP_ID'),
'app_secret' => env('FACEBOOK_APP_SECRET'),
'default_graph_version' => env('FACEBOOK_GRAPH_VERSION'),
]);
// Sample and some of the possible fields that can be send when posting to FB.
// $linkData = [
// 'link' => 'absolute link here',
// 'message' => 'Main body content here',
// 'picture' => 'path/to/the/image.jpg'
// ];
//to post immediately
if ($input['pub_schedule'] !=1) {
$linkData = [
'message' => $input['title'] . "\n" . $input['body'] . "\n" . $input['price']
];
// to schedule a post to fb
} else {
$linkData = [
'message' => $input['title'] . "\n" . $input['body'] . "\n" . $input['price'] ,
"published" => false,
'scheduled_publish_time' => strtotime($input['published_at']),
];
}
我认为在您的表单中,该字段是可选的。因此,不会设置该值。做这样的事情:
if (isset($input['pub_schedule']) && $input['pub_schedule'] !=1) {
$linkData = [
'message' => $input['title'] . "\n" . $input['body'] . "\n" . $input['price']
];
}