Partage
  • Partager sur Facebook
  • Partager sur Twitter

Trying to get property of non-object

    18 novembre 2017 à 14:53:54

    Bonjour à tous !

    J'essaye d'apprendre à utiliser Laravel, et du coup je suis un tuto que j'ai trouvé sur le net. Sauf que j'ai un problème au niveau de la création de l'espace commentaire surtout avec le Model "comment" il m'envoie  une erreur dont je ne sais absolument pas l'origine ni comment y résoudre.

    Voici le message d'erreur:

     ErrorException (E_NOTICE)
    
    Trying to get property of non-object
    Open: D:\Logiciels\wamp64\www\salope6\app\models\Comment.php
    
        	public static function boot(){
        		parent::boot();
        		self::deleted(function($comment){
        			$comment->post->counts_comment = $comment->post->comments->count();
        			$comment->post->save();
        		});
        		self::created(function($comment){
        			$comment->post->counts_comment = $comment->post->comments->count();
        			$comment->post->save();
        		});

    et  voici les lignes concernées:

        			$comment->post->counts_comment = $comment->post->comments->count();
        			$comment->post->save();

    Mon Model: Comment.php

    <?php
    
    class Comment extends \Eloquent {
    	protected $guarded = ['id','created_at'];
    
    	public static function boot(){
    		parent::boot();
    		self::deleted(function($comment){
    			$comment->post->counts_comment = $comment->post->comments->count();
    			$comment->post->save();
    		});
    		self::created(function($comment){
    			$comment->post->counts_comment = $comment->post->comments->count();
    			$comment->post->save();
    		});
    
    		return true;
    	}

    La route: routes.php

    Route::group(['before'=>'auth'],function(){
      Route::post('posts/{id}/comments/create',['as'=>'comments.create','uses'=>'CommentsController@create']);
    });
    

    Mon Contrôleur: CommentController.php

    <?php
    
    Class CommentsController extends BaseController{
    
    public function admin(){
    		$comments = Comment::all();
    		return View::make('comments.admin',compact('comments'));
    }
    
    
    		public function create($id){
    			  $post = Post::find($id);
    				$inputs = Input::all();
    				Comment::create([
    						'user_id' => Auth::user()->id,
    						'post_id' => $post->id,
    						'content' => $inputs['comment'],
    				]);
    				return Redirect::back()->with('success','Votre commentaire a bien été créé');
    			}
    }




    Et enfin, l'affichage Views: show.blade.php

    @extends('layouts.master')
    
    @section('content')
    
    
    <h2>{{ $post->name }}</h2>
    <p>Posté par : {{ $author->username }}
    
      @if($post->counts_comment == 0)
      Pas de commentaire
      @elseif($post->counts_comment == 1)
      1 Commentaire
      @else
      {{ $post->counts_comment }} Commentaires
      @endif
    
    </p>
    <p>{{ $post->content }}</p>
    
      <h3>Les Commentaires</h3>
    
      @if(!empty($comments))
      Pas encore de commentaire
      @else
          @foreach($comments as $comment)
          <h4>Commentaire posté par {{ $comment->user->username }}</h4>
          <p>{{ $comment->content  }}</p>
          @endforeach
    @endif
    
    @if(Auth::Check())
    <a href="{{ URL::route('posts.admin') }}">Modifier les postes</a>
    @endif
    
    <h1>Poster un commentaire</h1>
    
    @if(Auth::check())
    
    {{ Form::open(['route'=>['comments.create',$post->id],'method'=>'POST']) }}
    
        <div class="form-group">
        {{ Form::text('comment','',['class'=>'form-control']) }}
        </div>
    
        {{ Form::submit('Envoyer') }}
    
    {{ Form::close() }}
    @else
    Pour poster un commentaire <a href="{{ URL::route('users.login') }}">Connecter vous</a>
    @endif
    @stop
    

    Je vous remercie d'avance !





    -
    Edité par TakezoKensei 18 novembre 2017 à 14:58:44

    • Partager sur Facebook
    • Partager sur Twitter

    Trying to get property of non-object

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown