需要在laravel 5中解决未定义的索引错误

时间:2016-09-01 06:37:17

标签: php laravel facebook-sdk-4.0

我正在尝试使用laravel 5和facebook SDK将帖子安排到FB页面。

我需要在发布评论现在之间进行选择,或者稍后安排发布。

使用我的代码,我可以安排我的帖子。但如果我想立即发布一个异常被抛出:

  

“FacebookController.php第118行中的ErrorException:未定义的索引:pub_schedule”。第118行是“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());
      }
    }
  }

这是我的FacebookController:

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']),
    ];
  }

1 个答案:

答案 0 :(得分:1)

我认为在您的表单中,该字段是可选的。因此,不会设置该值。做这样的事情:

if (isset($input['pub_schedule']) && $input['pub_schedule'] !=1) {
    $linkData = [
    'message' => $input['title'] . "\n" . $input['body'] . "\n" . $input['price']
    ];
}
相关问题