2017-01-02 166 views
0

基本上,我試圖創建用戶,發佈和回覆數據庫之間的關係。下面是型號: 評論模型laravel雄辯關係錯誤

<?php 

namespace App\Eloquent; 

use Illuminate\Database\Eloquent\Model; 

class comments extends Model 
{ 
    public $timestamps = true; 
    protected $table = 'comments'; 
    protected $guarded = ['id']; 

    public function userInfo() 
    { 
     return $this->belongsTo('App\Eloquent\User', 'user_id'); 
    } 
    public function reply() 
    { 
     return $this->hasOne('App\Eloquent\reply', 'post_id'); 
    } 
} 

應答方式:

<?php 

namespace App\Eloquent; 

use Illuminate\Database\Eloquent\Model; 

class reply extends Model 
{ 
    public $timestamps = true; 
    protected $table = 'replies'; 
    protected $guarded = ['id']; 


    function user() 
    { 
     return $this->belongsTo('App\Eloquent\User', 'user_id'); 
    } 
} 

主要代碼:

<?php 

namespace App\Http\Controllers; 

use App\Eloquent\comments; 
use App\Eloquent\reply; 
use Illuminate\Http\Request; 

class CommentsController extends Controller 
{ 
    public function index() 
    { 
     $commentsData = []; 
     $replyData = []; 
      $comments = comments::all(); 
     foreach ($comments as $comment) 
     { 
      if($comments !== null) { 
       $user = comments::find($comment->user_id)->userInfo(); 
       $reply = comments::find($comment->id)->reply(); 
      } 
      if(reply::all() !== null) { 
       $user_reply = reply::find($comment->id)->user(); 
      } 
      $commentsData[$comment->id]['name'] = $user->name; 
      $commentsData[$comment->id]['message'] = $comment->body; 
      $commentsData[$comment->id]['rating'] = $comment->rating; 
      $commentsData[$comment->id]['timestamp'] = $comment->created_at; 
      foreach($reply as $re) 
      { 
       $replyData[$re->post_id][$re->id]['name'] = $user_reply->name; 
       $replyData[$re->post_id][$re->id]['body'] = $reply->body; 
      } 

     } 

     return view('comments')->with('comments', $commentsData)->with('reply', $replyData); 
    } 
} 

當我accesing評論頁面,我發現了以下錯誤:未定義的屬性:Illuminate \ Database \ Eloquent \ Relations \ BelongsTo :: $ name。 這是我第一次使用關係,所以我檢查了laravel文檔,但仍然不知道我做錯了什麼。基本上我試圖得到的是從用戶數據庫(使用評論user_id作爲外國)獲得用戶的名稱,獲取評論詳細信息(正文,評級),並獲得答覆數據,使用post_id(在回覆表)作爲外國和評論表主鍵ID作爲本地鍵。

回答

0

您從模型中獲取關係定義而不是相關對象。

在這些線路的盡頭更換

$user = comments::find($comment->user_id)->userInfo(); 
$reply = comments::find($comment->id)->reply(); 
$user_reply = reply::find($comment->id)->user(); 

$user = comments::find($comment->user_id)->userInfo; 
$reply = comments::find($comment->id)->reply; 
$user_reply = reply::find($comment->id)->user; 

注意去掉括號。

0

我有一個關於你的foreach循環一些關係,這將使其更快,更可用所做的更改

You are using relationship and still finding user using array key it's more likely to use on $comment bcz $comment is already Model and you can apply relationship on that easily. as same as for replay Model.

foreach ($comments as $comment) 
    { 
     $user = $comment->userInfo(); 
     $reply = $comment->reply(); 

     $commentsData[$comment->id]['name'] = $user->name; 
     $commentsData[$comment->id]['message'] = $comment->body; 
     $commentsData[$comment->id]['rating'] = $comment->rating; 
     $commentsData[$comment->id]['timestamp'] = $comment->created_at; 
      foreach($reply as $re) 
      { 
       $replyData[$re->post_id][$re->id]['name'] = $re->user()->name; 
       $replyData[$re->post_id][$re->id]['body'] = $re->body; 
      } 
     }