基本上,我试图在用户、post和回复数据库之间建立一种关系。以下是模型:注释模型
<?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);
}
}
当我进入评论页面时,我得到以下错误:
未定义的属性:照亮\数据库\雄辩\关系\属于::$name。
这是我第一次使用关系,所以我查看了Laravel文档,但仍然不知道我做错了什么。基本上,我试图从用户数据库中获取用户名(使用comments user_id作为外键),获取评论详细信息(body,rating)并获取回复数据,使用post_id(在回复表中)作为外键,使用comments表主键id作为本地键。
我做了一些改变,关于你的Foreach循环中的一些关系,这将使它更快,更可用
你正在使用关系,并且仍然使用数组键找到用户,它更有可能在$评论上使用bcz$评论已经是模型,你可以很容易地应用关系。
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;
}
}
您将从模型而不是相关对象中获得关系定义。
代替
$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;
请注意这些行末尾去掉的括号。
在Laravel中,有可能把关系放在关系中吗? 我的两个模型: 商店 供应商 我的商店和供应商关系模型: null 正如您所看到的,我有许多商店和供应商,每个都可以通过SupplierStore表相互关联。 这很好用。 我的问题是,对于每一个关系(商店和供应商),可能有许多交货日。 我想如果我进入SupplierStore模型并添加: 会工作,但我似乎没有办法通过雄辩来访问这些数据? 我希望做这样
Laravel似乎是一个非常好的PHP框架,与一个好的ORM(雄辩)捆绑在一起。然而,laravel文档中缺少一些内容。文档中只有基本的内容。 无论如何,当涉及到雄辩和模型关系时,我有一个问题,当它跨越两个以上的模型。 例如,我有以下场景。 我有四个数据库表,即:,,,。模型/表格之间的关系如下所示: 用户可以属于多个位置,反之亦然。一个位置可以有许多包。 而我对应的模型关系如下: 我想做什么?:
我是新来的laravel和雄辩,我不确定这是否可能。但是我有2张桌子,有一对多的关系。一个是“位置”,一个是“用户”。一个位置可以有许多用户。 所以,如果我想获得所有用户的所有位置,我会这样做: 但是我也想知道每个位置有多少用户,我试着这样做 但那没有奏效。
我正试图用雄辩加载一个遥远的关系,但遇到了问题。涉及5个表,它们是用户、corporate_users、公司和两个哨兵表(组和users_groups)。 表的设置如下所示: 用户有一个合作用户(一对一) 公司用户归属到公司(多对一) 用户通过users_groups透视表与组建立多对多关系。 所有这些关系都是单独工作的。最初,我可以通过调用
我在中编写查询时遇到问题。 我的疑问是 现在我想把这个查询转换成laravel eloquent。
我有一个表事务与复合主键id,Nom,日期和一个表Cour与Nom作为主键和一个事务有关一个Cour。 我怎样才能得到一个有口才关系的特定交易的课程?