我正在尝试从数组中获取列,但可以在 laravel 中获取它
I am trying to get the columns from an array but can be able to get it in laravel
我正在尝试通过 id 从另一个 table 获取列,但是如果我在视图
中使用 foreach
,每当我这样做时都会收到此错误
Property [id] does not exist on this collection instance. (View: C:\xampp\htdocs\myUniMentor\resources\views\userProfile\viewUserProfilePage.blade.php)
但是如果我 运行 没有像 $users->reviews
这样的 foreach
命令的代码,我会得到 review
table 的数组以及所有相关的内容具体的 id 并这样做 $users->reviews->comments
给我一个错误
Property [comments] does not exist on this collection instance. (View: 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');
}
}
不清楚您要在 blade 文件中做什么。但是如果要打印用户所有评论的所有评论。
显示用户个人资料blade
@section('content2')
<h3>Reviews</h3>
@foreach($user->reviews as $review)
<p>{{ $review->comments }}</p>
@endforeach
@endsection
说明
reviews()
是user
的一对多关系,$user->reviews
是collection的一对多关系。因此,如果您想访问 review
的任何 属性,您需要循环遍历 collection.
请更改以下内容:
在你的HTML
@foreach($users->reviews as $review)
<p>{{ $review->comments }}</p>
@endforeach
在您的 Review.php 中,由于评论属于用户,因此更改为
public function user()
{
return $this->belongsTo('App\User');
}
我正在尝试通过 id 从另一个 table 获取列,但是如果我在视图
中使用foreach
,每当我这样做时都会收到此错误
Property [id] does not exist on this collection instance. (View: C:\xampp\htdocs\myUniMentor\resources\views\userProfile\viewUserProfilePage.blade.php)
但是如果我 运行 没有像 $users->reviews
这样的 foreach
命令的代码,我会得到 review
table 的数组以及所有相关的内容具体的 id 并这样做 $users->reviews->comments
给我一个错误
Property [comments] does not exist on this collection instance. (View: 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');
}
}
不清楚您要在 blade 文件中做什么。但是如果要打印用户所有评论的所有评论。
显示用户个人资料blade
@section('content2')
<h3>Reviews</h3>
@foreach($user->reviews as $review)
<p>{{ $review->comments }}</p>
@endforeach
@endsection
说明
reviews()
是user
的一对多关系,$user->reviews
是collection的一对多关系。因此,如果您想访问 review
的任何 属性,您需要循环遍历 collection.
请更改以下内容:
在你的HTML
@foreach($users->reviews as $review) <p>{{ $review->comments }}</p> @endforeach
在您的 Review.php 中,由于评论属于用户,因此更改为
public function user() { return $this->belongsTo('App\User'); }