2017-09-19 2 views
0

J'utilise Automatically Send Tweets through Laravel Notifications mais je suis obtenir erreur in PostPublished.php (line 66) qui est cette ligne:notification Laravel Essayer d'obtenir la propriété de non-objet

if (! $post->user->twitter_username or $post->user->id == 1) return false; 

voici mon code complet dans PostPublished

<?php 

namespace App\Notifications; 

use Illuminate\Bus\Queueable; 
use Illuminate\Notifications\Notification; 
use Illuminate\Contracts\Queue\ShouldQueue; 
use Illuminate\Notifications\Messages\MailMessage; 
use NotificationChannels\Telegram\TelegramChannel; 
use NotificationChannels\Telegram\TelegramMessage; 
use NotificationChannels\Twitter\TwitterChannel; 
use NotificationChannels\Twitter\TwitterStatusUpdate; 
use App\Post; 
use App\User; 

class PostPublished extends Notification 
{ 
    use Queueable; 

    /** 
    * Create a new notification instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     // 
    } 

    /** 
    * Get the notification's delivery channels. 
    * 
    * @param mixed $notifiable 
    * @return array 
    */ 
    public function via($notifiable) 
    { 
     return [TwitterChannel::class, TelegramChannel::class]; 
    } 

    /** 
    * Get the mail representation of the notification. 
    * 
    * @param mixed $notifiable 
    * @return \Illuminate\Notifications\Messages\MailMessage 
    */ 
    public function toTelegram($post) 
    { 
     return TelegramMessage::create() 
      ->to('@lavtest') 
      ->content($post->title .' http://domainxxx.com/blog/article/'. $post->slug); 
    } 


    //twitter 
    public function toTwitter($post) 
    { 
     $title = $post->title; 
     if ($handle = $this->twitterHandle($post)) { 
      $title = $title .' via '. $handle; 
     } 
     return new TwitterStatusUpdate($title .' https://domainxxx.com/blog/article/'. $post->slug, [$post->image]); 
    } 
    protected function twitterHandle($post) 
    { 
     if (! $post->user->twitter_username or $post->user->id == 1) return false; 
     if ($post->user->twitter_username and ! starts_with($post->user->twitter_username, '@')) { 
      $post->user->twitter_username = '@'.$post->user->twitter_username; 
     } 
     return $post->user->twitter_username; 
    } 
    //end twitter 

    /** 
    * Get the array representation of the notification. 
    * 
    * @param mixed $notifiable 
    * @return array 
    */ 
    public function toArray($notifiable) 
    { 
     return [ 
      // 
     ]; 
    } 
} 

MISE À JOUR:

Je n'ai pas eu de relation entre les messages et l'utilisateur et j'ai fait que maintenant l'erreur a changé en Undefined property: stdClass::$errors in CouldNotSendNotification.php (line 9)

+0

Est-ce que '$ post-> user' existe? Je pense que c'est nul ou vide –

+0

C'est parce que le 'post' peut ne pas avoir un' utilisateur' associé – linuxartisan

+0

@linuxartisan comment y remédier? – mafortis

Répondre

0

Résolu

J'ai enlevé mon application twitter et créé nouveau, maintenant tout fonctionne.