2017-04-19 41 views
0

我在我的foreach語句之一中收到錯誤。自從我將OAuth登錄到我的項目後發生這種情況。問題出在我發佈帖子後,一旦發佈文章的人的姓名出現錯誤,並且在執行OAuth之前它一直在運行良好。嘗試在Laravel 5.4中獲取OAuth2之後的非對象屬性

搜索錯誤,它說:

ErrorException in 90b25bff626151e496cbeba41de0d0cc13613e1c.php line 90: 

The line 90 have: `<li><?php echo e($post->user->name); ?> &middot;</li>` 

這裏是我的index.blade.php

@foreach ($posts as $post) 

     <div class="col-sm-4 col-xs-12"> <!-- News --> 
      <div class="thumbnail" onclick="location.href='{{ url('news/'.$post->slug) }}';"> 
      <div class="imageWrapper"> 
       <img src="{{ asset('imags/' . $post->postimg) }}" alt="feature-collection-image"> 
       <!-- <div class="masking"><a href="{{ url('news/'.$post->slug) }}" class="btn viewBtn">Leer Artículo</a></div> --> 
      </div> 
      <ul class="list-inline post-author"> 
       <li>{{ $post->user->name }} &middot;</li> 
       <li><span class="badge">{{ $post->comments()->count() }}</span> comentarios</li> 
      </ul> 
      <div class="caption post-info"> 
       <h4>{{ substr($post->title, 0, 150) }}{{ strlen($post->title) > 150 ? "..." : "" }}</h4> 
       <p> 
       {{ substr(strip_tags($post->body), 0, 200) }}{{ strlen(strip_tags($post->body)) > 200 ? ". Leer más" : "" }} 
       </p> 
      </div> 
      </div> 
     </div> 

     @endforeach 

在foreach中的OAuth之前,這是工作好,文章以創作者的文章顯示完美名稱和我的新聞/單,但現在...

我不知道會發生什麼。

PD:我將OAuth配置放在我的LoginController和Config/Services.php上,如果我刪除{{ $post->user->name }}一切正常,但不會顯示帖子創建者的名字。

+0

您是否有任何帖子的用戶密鑰與用戶不匹配?你的錯誤表明它找不到你的鏈中的'user'部分。隨後它無法檢索名稱。 – James

+0

在管理儀表板和帖子/顯示「我可以看到帖子列表」的名字顯示正確,但現在在索引和新聞/單我不能..不知道爲什麼.. –

+0

如果它在一個頁面上,而不是另一方面,那麼你**必須**以不同的方式做事。查看您在控制器中如何檢索它以及如何顯示它。在某個地方一定有區別 – James

回答

0

現在工作很好,我認爲這是一個糟糕的遷移。

我做了一個新的表名爲social_providers

public function up() 
{ 
    Schema::create('social_providers', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('user_id')->unsigned()->references('id')->on('users'); 

     $table->string('provider_id'); 
     $table->string('provider'); 
     $table->timestamps(); 
    }); 
} 

我所做的是回退所有的遷移和我改變這樣的:

public function up() 
{ 
    Schema::create('social_providers', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('user_id')->unsigned(); 
     $table->foreign('user_id')->references('id')->on('users'); 
     $table->string('provider_id'); 
     $table->string('provider'); 
     $table->timestamps(); 
    }); 
} 

再次運行我的服務器「PHP工匠服務」創建新的管理員,新的用戶和新的職位..現在正在工作,但我不知道如果這是問題。

相關問題