0

My User Model:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'firstName', 'secondName', 'email', 'city', 'phoneNumber', 'password', 'profilePicture'
    ];

    /**
     * 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 ratings()
    {
        return $this->belongsToMany(Ratings::class, 'ratings_has_users', 'users_id', 'ratings_id')->withTimestamps();
    }

}

My Ratings Model:

<?php

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class Ratings extends Model
{
    public function user(){
        return $this->belongsToMany(User::class, 'ratings_has_users', 'users_id', 'ratings_id')->withTimestamps();
    }
}

MIgration to create table 'ratings_has_users'

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRatingsHasUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ratings_has_users', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('users_id');
            $table->unsignedBigInteger('ratings_id');
            $table->timestamps();
        });

        Schema::table('ratings_has_users', function (Blueprint $table) {
            $table->foreign('users_id')
                ->references('id')
                ->on('users');
        });

        Schema::table('ratings_has_users', function (Blueprint $table) {
            $table->foreign('ratings_id')
                ->references('id')
                ->on('ratings');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('ratings_has_users');
    }
}

Migration to create 'users' table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('firstName',55);
            $table->string('secondName',55);
            $table->string('email',55)->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('city',55);
            $table->text('description')->nullable();
            $table->string('phoneNumber',11);
            $table->string('profilePicture',255)->default('profile.jpg');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

Migration to create 'ratings' table:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRatingsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ratings', function (Blueprint $table) {
            $table->id();
            $table->integer('stars');
            $table->mediumText('note');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('ratings');
    }
}

And code in my blade:

@foreach($ratings as $rating)
    {{ dd($rating->user->firstName) }}
@endforeach

I don't know why I see this error message: Property [firstName] does not exist on this collection instance. (View: /data/www/twoj-trener/resources/views/trainer_page/reviews.blade.php)

When I change code on this:

@foreach($ratings as $rating)
    {{ dd($rating->user) }}
@endforeach

I got this:

Illuminate\Database\Eloquent\Collection {#1291 ▼
  #items: []
}
Sothot
  • 11
  • 1
  • 2
  • 3
    `Rating`'s `user` relationship method returns a `BelongsToMany` (plural), will always be a Collection – lagbox Aug 12 '20 at 09:30
  • similar issue to here I would assume: https://stackoverflow.com/questions/41366092/property-title-does-not-exist-on-this-collection-instance – meewog Aug 12 '20 at 09:30

2 Answers2

0

Ratings has user(better name users, because belongsToMany) relation method. If belongsToMany relation that relation method(in this case user) will give the Collection (list related records).

You can do something like below

$rating->user->first()->firstName
  • This ```$rating->user->first()->firstName``` not working I got ```Trying to get property 'firstName' of non-object``` – Sothot Aug 12 '20 at 11:53
  • @Sothot are you sure rating has related user? If `$rating->user->first()` is null then getting properties will not work – Mihail Simbirsky Aug 13 '20 at 08:59
0

The relation between rating and user should be users() (with an S for plural) since it's a belongToMany one with a pivot table.

If you want ratings to be unique per user, consider changing your migration of ratings table

    public function up()
    {
        Schema::create('ratings', function (Blueprint $table) {
            $table->id();
            $table->integer('stars');
            $table->mediumText('note');
            $table->unsignedBigInteger('user_id');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users');
        });
    }

and change the relation to belongsTo

if your structure is correct as it is, you will need to get the user before accessing its attribute

@foreach($ratings as $rating)
    {{ dd($rating->users->first() ?? $rating->users->first()->firstName) }}
@endforeach
N69S
  • 16,110
  • 3
  • 22
  • 36