我试图通过id从另一个表中获取列,但是每当我在视图中使用foreach
时,都会出现此错误
属性[id]在此集合实例上不存在。 (视图:C:\ xampp \ htdocs \ myUniMentor \ resources \ views \ userProfile \ viewUserProfilePage.blade.php)
但是如果我运行的代码没有foreach
这样的$users->reviews
命令,则会得到review
表的数组以及该特定ID的所有相关内容,然后执行$users->reviews->comments
给我一个错误
属性[comments]在此集合实例上不存在。 (视图:C:\ xampp \ htdocs \ myUniMentor \ resources \ views \ userProfile \ viewUserProfilePage.blade.php)
我的问题是:
$users->reviews
如何仅返回与ID关联的列?是因为我在belongsTo()
中传递了ID?ReviewController.php
public function showUserProfile($id) {
$users = User::find($id);
// echo $users->first_name;
return view('userProfile.viewUserProfilePage', compact('users'));
}
}
public function addNewReview($id) {
$stars = Input::get("rating");
$message = Input::get("message");
// $users = User::find($id);
$users = User::where('id', $id)->first();
// echo $stars;
// echo $message;
// echo $users;
// echo $users->id;
// echo Auth::user()->id;
// die();
$reviews = new Review();
$reviews->user_id = $users->id;
$reviews->given_by = Auth::user()->first_name . ' ' . Auth::user()->last_name;
$reviews->stars = $stars;
$reviews->comments = $message;
$reviews->save();
Session::flash('message','Your Review has been added to the Mentor');
return redirect('show-user-profile/' . $users->id);
}
show-user-profile.blade.php
@section('content2')
<h3>Reviews</h3>
<p>{{ $users->reviews->comments }}</p>
@endsection
Review.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Review extends Model
{
protected $fillable = [
'comments', 'stars', 'given_by',
];
public function users()
{
return $this->belongsTo('App\User', 'user_id','id');
}
}
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\UserType;
use App\Subject;
use App\SubjectKeyword;
use App\Review;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name', 'last_name', 'type', 'username', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function getAllUsers() {
return User::all();
}
public function userTypes()
{
return $this->belongsTo('App\Users');
}
// public function subjects()
// {
// return $this->belongsToMany('App\Subject');
// }
public function subjects(){
return $this->belongsTo('App\Subject','subject_id','id');
}
public function reviews(){
return $this->hasMany('App\Review');
}
public function subjectKeywords(){
return $this->hasMany('App\SubjectKeyword');
}
}
答案 0 :(得分:0)
目前尚不清楚您要在刀片文件中执行的操作。但是,如果您要打印用户所有评论的所有评论。
显示用户个人资料刀片
@section('content2')
<h3>Reviews</h3>
@foreach($user->reviews as $review)
<p>{{ $review->comments }}</p>
@endforeach
@endsection
说明
reviews()
是user
的一对多关系,$user->reviews
是一个集合。因此,如果要访问review
的任何属性,则需要遍历集合。
答案 1 :(得分:0)
请更改以下内容:
在您的HTML中
@foreach($users->reviews as $review)
<p>{{ $review->comments }}</p>
@endforeach
在您的Review.php中,由于评论属于用户,因此更改为
public function user()
{
return $this->belongsTo('App\User');
}