Davi
Davi

Reputation: 77

Select only items from a column laravel 9

I'm trying to retrieve the video column from the posts table, but no item is displayed.

I am using the following syntax

public function shorts()
{

  
    $videos = Post::where('video', '>', 0)
    ->inRandomOrder()
    ->paginate(3);

    return view('shorts', compact('videos'));
}

I can recover using the following syntax. However, it returns all posts and I only wanted posts in which the video column is not empty.

public function shorts()
{

  
    $videos = Post::inRandomOrder()
        ->simplePaginate(1);

    return view('shorts', compact('videos'));
}

Does anyone have an idea how to resolve this issue? Thanks in advance. any help is welcome.

view file.

    @foreach ($videos->where('video', '!=', '') as $video)

        <div class="video">

            <a href="{{route('post.show', $video)}}">
                <h2>{{ $video->title }}</h2>
            </a>

                <video id="video" width="100%" height="450px"> <source src="{{url('uploads/', $video->video)}} " alt="VIDEO" ></video>

                <br/><br/>
                <div>
                    <br>

                    <div class="profileImageHome">

                        <img src="{{$video->user->getAvatar()}}" alt="avatar" class="w-14 h-14 rounded-xl">
                        <br>
                        <a href="{{url('http://127.0.0.1:8000/u/' . $video->user->username)}}">  u/{{$video->user->username}}</a>

                    </div>

                </div>
                <br>
            <hr/>
            @endforeach


            {{ $videos->links() }}

SCREESHOTS

enter image description here

enter image description here

Upvotes: 1

Views: 108

Answers (1)

Daniel
Daniel

Reputation: 191

https://laravel.com/docs/9.x/queries

Try it with

Post::whereNotNull('video')
    ->inRandomOrder()
    ->paginate(3);

i suppose the issue was you checking for > 0 (more than 0, but 0 is not NULL) and since it is a string you shouldnt check with integer where clauses.

Upvotes: 1

Related Questions