skip to Main Content

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


  1. 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();
        });
    
    Login or Signup to reply.
  2. 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();
        });``
    
    Login or Signup to reply.
  3. 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('AppModelslike_video', 'amv_id', 'id');
    }
    

    IN VIEW:

    var_dump($liked);die();
    
    @foreach ($liked 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search