dd()处变量的值错误

时间:2019-07-09 06:48:32

标签: php database laravel eloquent

我想对我的文章发表评论(编辑文章)。我在$event_comm中创建了变量EventController,但是当我试图获取价值时,会返回[]。您能告诉我什么地方不对吗与我的代码,以及如何解决?我的表event_comment包含列iduser_idevent_idcomments,其中每篇文章的注释。

dd($ event_comm)

Collection {#823 ▼
  #items: array:8 [▼
    0 => EventComment {#824 ▼
      #table: "event_comments"
      #fillable: array:3 [▶]
      #connection: null
      #primaryKey: "id"
      #keyType: "int"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:1 [▼
        "comments" => "Comment1"
      ]
      #original: array:1 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
    1 => EventComment {#826 ▶}
    2 => EventComment {#825 ▶}
    3 => EventComment {#812 ▶}
    4 => EventComment {#811 ▶}
    5 => EventComment {#810 ▶}
    6 => EventComment {#809 ▶}
    7 => EventComment {#808 ▶}
  ]
}

** EventComment.php雄辩**

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class EventComment extends Model
{
   protected $table = 'event_comments';
    // public $timestamps = false;
    protected $fillable = [
        'user_id', 'event_id','comments'
    ];

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

    public function event()
    {
        return $this->belongsTo('App\Event','event_id');
    }
}

控制器

$article = \DB::table("events")
    ->where("id", $id)
    ->select("id", "subject", "information", "public", "category_id", "event_type_id", "country", "address", "city", "starts", "ends", "organizer", "website", "email", "telephone")
    ->first();
         $data['article'] = $article;
$event_comm = EventComment::where('comments', '=', 'user_id')->get();

dd($event_comm);

return view("admin.editEvent", $data)
    ->with(compact('event_comm'));

2 个答案:

答案 0 :(得分:2)

    public function event(Request $request){
              $comments = $request->comments;
              $event_comm = EventComment::with('event')->where('comments', $comments)->get();

    }
try once this 

答案 1 :(得分:1)

根据事件ID接收评论 因为您已经有这篇文章了

$event_comm = EventComment::where('event_id', $article->id)->get();

或者只是

$event_comm = EventComment::where('event_id', $id)->get();

因为您已经在控制器中传递了$ id

如果您只想在查询中获得评论。

$event_comm = EventComment::where('event_id', $id)->select('comments')->get();