Home > Software design >  Select only items from a column laravel 9
Select only items from a column laravel 9

Time:11-17

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 >

            <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 >

                        <img src="{{$video->user->getAvatar()}}" alt="avatar" >
                        <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

CodePudding user response:

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.

  • Related