Ad

I Am Trying To Get The Columns From An Array But Can Be Able To Get It In Laravel

I am trying to fetch the columns from another table through the id but whenever I do I get this error if I use foreach in the view

Property [id] does not exist on this collection instance. (View: C:\xampp\htdocs\myUniMentor\resources\views\userProfile\viewUserProfilePage.blade.php)

but if I run code without foreach command like $users->reviews I get the array of review table and also all the related content to that specific id and doing this $users->reviews->comments gives me an error

Property [comments] does not exist on this collection instance. (View: C:\xampp\htdocs\myUniMentor\resources\views\userProfile\viewUserProfilePage.blade.php)

My questions are:

  1. How can I display the comments using the models?
  2. How $users->reviews is returning only the columns associated with the id? is it because I passed the id in belongsTo()?

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');
    }
}
Ad

Answer

Please change the following:

  1. In your HTML

    @foreach($users->reviews as $review)
     <p>{{ $review->comments }}</p> 
    @endforeach
    
  2. In your Review.php, Since a review belong to a User, Change to

    public function user()
    {
      return $this->belongsTo('App\User');
    }
    
Ad
source: stackoverflow.com
Ad