Php dd()处变量的值错误
我想为我的文章(编辑文章)获取评论。我在Php dd()处变量的值错误,php,database,laravel,eloquent,Php,Database,Laravel,Eloquent,我想为我的文章(编辑文章)获取评论。我在EventController中创建了变量$event\u comm,但当我试图获取此返回值[]时。你能告诉我我的代码有什么问题以及如何修复吗?。我的表格event\u comment有列id,user\u id,event\u id,comments,其中是每篇文章的注释 dd($event\u comm) **EventComment.php雄辩** <?php namespace App; use Illuminate\Database\E
EventController
中创建了变量$event\u comm
,但当我试图获取此返回值[]时。你能告诉我我的代码有什么问题以及如何修复吗?。我的表格event\u comment
有列id
,user\u id
,event\u id
,comments
,其中是每篇文章的注释
dd($event\u comm)
**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');
}
}
根据事件id接收评论 既然你已经有文章了,就做这个吧
$event\u comm=EventComment::where('event\u id',$article->id)->get()代码>
或者干脆
$event\u comm=EventComment::where('event\u id',$id)->get()代码>
因为您已经在控制器中传递了$id
如果您只想在查询中获取注释
$event\u comm=EventComment::where('event\u id',$id)->select('comments')->get()代码>您正在将“用户id”作为字符串传递给查询。不是可变的。这可能就是它出错的地方。你说的comments=user\u id
是什么意思?他们都是对的?你到底想问什么?你是什么意思?他们只是一列,对吗?所以这是不可能的。你有用户表吗?您可以先查询它,然后在那里为您的用户获取id_id@AndreiNagy我仍然很困惑,但我回答了。看看它是否能帮助你这将不会得到事件的相对评论!将得到所有相同的评论这是部分工作。现在我得到了[{“id”:13,“用户id”:“542”,“事件id”:“1806”,“评论”:“testul meu:)”,“创建于”:“2019-07-09 06:04:26”,“更新于”:“2019-07-09 06:04:26”}]
。我怎么能只选择“评论”?$event\u comm->comments
但你需要使用foreach($event\u comm as$comm)
然后称它为$comm->comment
你需要foreach它,因为它是一个集合或数组。欢迎你!下一次请澄清,以便我们可以帮助您更快。现在,若我想知道用户的姓名,如何才能做到这一点?因为我有每个发表评论的用户的user\u id
?就像<代码>评论人…:$通信->注释
。用户的名称位于表users
,列name
。这是因为您有两个foreach,您可以删除$event\u命令,只使用$event\u user
<?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'));
public function event(Request $request){
$comments = $request->comments;
$event_comm = EventComment::with('event')->where('comments', $comments)->get();
}
try once this