Home > Blockchain >  Can't get the model relationship right. It said property doesn't exist in Eloquent builder
Can't get the model relationship right. It said property doesn't exist in Eloquent builder

Time:10-11

Something is wrong with my codes. I knew my concept is right in my own thinking but not working....I want to take out the videos that logged in user liked.
This is my liked table and it stored amv_id and user_id

Schema::create('like_videos', function (Blueprint $table) {
        $table->id();
        $table->foreignId("amv_id");
        $table->foreignId("user_id");
        $table->timestamps();
    });

This is my amv table and it stored everything of videos

Schema::create('amvs', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->string('desc');
        $table->integer('view');
        $table->integer('like');
        $table->integer('dislike');
        $table->integer('category_id');
        $table->foreignId('user_id');
        $table->string('video');
        $table->string('thumb');
        $table->timestamps();
    });

This is my user table and it stored user infos

Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->integer('sub');
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->integer('suspend');
        $table->rememberToken();
        $table->timestamps();
    });

This is my controller codes

public function likedvideo() {
    $id = auth()->user()->id;
    $auth = auth()->user();
    $liked = like_video::with('amvs')->where("user_id", "=", $id)->latest();
    return view("amvs.likedvideos", [
        'liked' => $liked,
        'auth' => $auth,
    ]);
}

I used model relationship on Amv Model

public function amvs() {
    return $this->hasMany('App\Models\like_video');
}

This is my view file

@foreach ($liked->amvs as $amv)
                <div >
                    <a href="{{url("amvtube/watch/$amv->id")}}" >
                        <div  style="width: 18rem;">
                            <img src="{{asset('/image/'.$amv->thumb)}}" alt="thumbnail" width="286px" height="161px">
                            <div >
                                <h5 >{{Str::limit($amv->title, 40, '...')}}</h5>
                                <span >{{$amv->user->name}} / Genre: {{$amv->category->name}}</span> <br>
                                <span >
                                    {{$amv->view}} View {{$amv->created_at->diffForHumans()}}
                                </span>
                            </div>
                        </div>
                    </a>
                </div>
            @endforeach

CodePudding user response:

The foriegn key in your like_videos is wrong.
The format that Laravel looks for by default is table name with id appended.

Schema::create('like_videos', function (Blueprint $table) {
        $table->id();
        $table->foreignId("amvs_id");
        $table->foreignId("user_id");
        $table->timestamps();
    });

CodePudding user response:

mention the error laravel app is showing? possible that your users table has id and you're using foreignId('user_id') which model can't find so use same name as id() field with your foreignId() fields or make relations as:

$table->unsignedBigInteger('user_id');

then connect with foreign:

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

MODIFY TABLE SCHEMA LIKE THIS:

    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->integer('sub');
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->integer('suspend');
        $table->rememberToken();
        $table->timestamps();
    });

    Schema::create('amvs', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->string('desc');
        $table->integer('view');
        $table->integer('like');
        $table->integer('dislike');
        $table->integer('category_id');
        $table->foreignId('user_id')->contrained('users');
        $table->string('video');
        $table->string('thumb');
        $table->timestamps();
    });

    Schema::create('like_videos', function (Blueprint $table) {
        $table->id();
        $table->foreignId("amv_id")->constrained('amvs');
        $table->foreignId("user_id")->constrained('users');
        $table->timestamps();
    });``

CodePudding user response:

Updated answer:

Add this to your Contoller:

public function likedvideo() {
$id = auth()->user()->id;
$data["auth"] = auth()->user();
$data["liked"] = Amv::with('liked_videos')->where("user_id", "=", $id)->get();
return view("amvs.likedvideos",$data);

}

This to Amv model:

public function liked_videos() {
    return $this->hasMany('App\Models\like_video', 'amv_id', 'id');
}

IN VIEW:

var_dump($liked);die();

@foreach ($liked as $amv)
<div >
    <a href="{{url("amvtube/watch/$amv->id")}}" >
        <div  style="width: 18rem;">
             <img src="{{asset('/image/'.$amv->thumb)}}" alt="thumbnail" width="286px" height="161px">
            <div >
                <h5 >{{Str::limit($amv->title, 40, '...')}}</h5>
                <span >{{$amv->user->name}} / Genre: {{$amv->category->name}}</span> <br>
                <span >
                    {{$amv->view}} View {{$amv->created_at->diffForHumans()}}
                </span>
            </div>
        </div>
    </a>
</div>

@endforeach

  • Related