【问题标题】:Need to solve undefined index error in laravel 5需要解决 laravel 5 中未定义的索引错误
【发布时间】:2016-09-01 06:37:17
【问题描述】:

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

我需要选择现在发表评论或稍后再发表评论。

使用下面的代码,我可以安排我的帖子。但是,如果我想立即发布,则会引发异常:

“FacebookController.php 第 118 行中的错误异常:未定义索引:pub_schedule”。第 118 行是“如果 ($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']),
    ];
  }

【问题讨论】:

  • 确保您发布了您将要使用的所有必需变量,否则您可以检查您的帖子变量是否已按照答案中给出的方式设置
  • 检查变量是否实际设置。

标签: php laravel facebook-sdk-4.0


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-10-07
    • 1970-01-01
    • 2016-03-17
    • 2019-11-16
    • 2022-06-22
    • 1970-01-01
    • 2018-01-17
    • 1970-01-01
    • 2016-04-15
    相关资源
    最近更新 更多