Php 拉威尔8号-罐头盒';无法从数据库中获取数据
我想从名为follower_id的数据库中获取数据,但当我尝试获取数据时,会出现错误。当我尝试使用foreach时,我返回null,我使用数据库和关系,如果我键入Php 拉威尔8号-罐头盒';无法从数据库中获取数据,php,laravel,Php,Laravel,我想从名为follower_id的数据库中获取数据,但当我尝试获取数据时,会出现错误。当我尝试使用foreach时,我返回null,我使用数据库和关系,如果我键入{{$followers},我就可以得到数据。另一方面,如果我键入{{$follows->follower\u id}我会得到以下错误: 此集合实例上不存在属性[follower_id] 那么我该如何解决这个问题呢?顺便说一句,我是Laravel和数据库关系的初学者 我的控制器是: public function getProfile(
{{$followers}
,我就可以得到数据。另一方面,如果我键入{{$follows->follower\u id}
我会得到以下错误:
此集合实例上不存在属性[follower_id]
那么我该如何解决这个问题呢?顺便说一句,我是Laravel和数据库关系的初学者
我的控制器是:
public function getProfile($username){
$follow = Follow::all();
$user = User::where('username', $username)->first();
if(isset($user)){
return view('design2.profile', ['user'=>$user,'follow'=>$follow]);
}else{
return redirect()->route('home');
}
}
<h6><span class="text-secondary"><strong>{{$follow->follower_id}} follower</strong></span></h6>
public $table = "follow";
protected $fillable = [
'follower_id',
'user_id',
];
public function user(){
return $this->belongsTo('App\Models\User');
}
protected $fillable = [
'name',
'email',
'username',
'password',
'info',
'twitter_name',
'instagram_name',
'photo'
];
public function follows(){
return $this->hasMany('App\Models\Follow');
}
我的刀片是:
public function getProfile($username){
$follow = Follow::all();
$user = User::where('username', $username)->first();
if(isset($user)){
return view('design2.profile', ['user'=>$user,'follow'=>$follow]);
}else{
return redirect()->route('home');
}
}
<h6><span class="text-secondary"><strong>{{$follow->follower_id}} follower</strong></span></h6>
public $table = "follow";
protected $fillable = [
'follower_id',
'user_id',
];
public function user(){
return $this->belongsTo('App\Models\User');
}
protected $fillable = [
'name',
'email',
'username',
'password',
'info',
'twitter_name',
'instagram_name',
'photo'
];
public function follows(){
return $this->hasMany('App\Models\Follow');
}
我的用户模型:
public function getProfile($username){
$follow = Follow::all();
$user = User::where('username', $username)->first();
if(isset($user)){
return view('design2.profile', ['user'=>$user,'follow'=>$follow]);
}else{
return redirect()->route('home');
}
}
<h6><span class="text-secondary"><strong>{{$follow->follower_id}} follower</strong></span></h6>
public $table = "follow";
protected $fillable = [
'follower_id',
'user_id',
];
public function user(){
return $this->belongsTo('App\Models\User');
}
protected $fillable = [
'name',
'email',
'username',
'password',
'info',
'twitter_name',
'instagram_name',
'photo'
];
public function follows(){
return $this->hasMany('App\Models\Follow');
}
你需要的是获得一个用户的所有追随者。 让我来为您编码,这样您就有了比现在更标准的代码 在您的用户模型中
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The followers that belong to the User
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function followers()
{
return $this->belongsToMany(User::class, 'follow', 'user_id', 'follower_id');
}
}
在你的刀刃上
@foreach($user->followers as $follower)
<h6>
<span class="text-secondary">
<strong>{{$follower->name}}</strong>
</span>
</h6>
@foreach
@foreach($user->followers as$followers)
{{$follower->name}
@弗雷奇
仅此而已。您尝试调试该问题的原因是什么?看起来“$follow”是一个实体集合
follow::all()
将为您提供一个类似于数组的集合。您需要在视图中循环该属性,并从每个模型中获取follow_id。如果您搜索“此集合实例上不存在属性”,则Stackoverflow上会有数千个结果。在发布另一个重复问题之前,请尝试调试您的问题。它们可能并不都是你的具体情况,但其中一个肯定足够相似,可以帮助你,或者阅读这篇文章上的评论。但没有必要创建另一个帐户。还有一个问题:如果你尝试过,那么展示一下你尝试了什么,以及为什么它不起作用。“它不起作用”是不够的描述。谢谢你,但正如你从前面的问题知道的,我想计算一下。我尝试了{{$follower->name->count()}},但出现了错误。我怎么能算得上我真的知道我想要什么。只是一个计数数据。我并没有问我如何从数据库中获取姓名。所以我的问题是我怎么数数?在你的问题中,你只放置了{{$follows->follower_id}给你一个错误,这就是我们要猜测你想要的。follower_id这是一个数字。你数不清。就像数到10一样。10等于10,就这样。所以我想数一数有多少个10。