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('AppModelslike_video');
}
This is my view file
@foreach ($liked->amvs as $amv)
<div class="col">
<a href="{{url("amvtube/watch/$amv->id")}}" class="text-decoration-none">
<div class="card mb-2 rounded-0" style="width: 18rem;">
<img src="{{asset('/image/'.$amv->thumb)}}" alt="thumbnail" width="286px" height="161px">
<div class="card-body">
<h5 class="card-title">{{Str::limit($amv->title, 40, '...')}}</h5>
<span class="card-text">{{$amv->user->name}} / Genre: {{$amv->category->name}}</span> <br>
<span class="text-muted">
{{$amv->view}} View {{$amv->created_at->diffForHumans()}}
</span>
</div>
</div>
</a>
</div>
@endforeach
3
Answers
The foriegn key in your like_videos is wrong.
The format that Laravel looks for by default is table name with id appended.
mention the error laravel app is showing?
possible that your
users
table hasid
and you’re usingforeignId('user_id')
which model can’t find so use same name asid()
field with yourforeignId()
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:
Updated answer:
Add this to your Contoller:
}
This to
Amv
model:IN VIEW:
@endforeach