Search code examples
phpjquerylaravel-8bootstrap-5bootstrap5-modal

how to open each post using modal in laravel 8


When I click on a post, it fetches post.blade.php and comment.blade.php and runs it in bootstrap modal.

The following issues exist while processing the current job

  1. Include error
    I just made a modal connection and tried @include('post.blade.php') in it.
    Error thrown because there is no $post variable used in the file

What I want to make is to call post.blade.php and comment.blade.php by passing the $post->id value as a parameter when clicking the post title to get data.

content.blade.php

  <a href="" data-bs-toggle="modal" data-bs-target="#open_post_modal">

    <!-- Modal -->
    <div class="modal fade" id="open_post_modal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                {{--            <div class="modal-header">--}}
                {{--                <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>--}}
                {{--                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>--}}
                {{--            </div>--}}
                <div class="modal-body">
                    @include('layouts.post')
                    {{--                @extends('layouts.post')--}}
{{--                    @extends('layouts.post')--}}
{{--                    @extends('layouts.comment')--}}
                </div>
                {{--            <div class="modal-footer">--}}
                {{--                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>--}}
                {{--                <button type="button" class="btn btn-primary">Save changes</button>--}}
                {{--            </div>--}}
            </div>
        </div>
    </div>

postcontroller

    public function show($id)
    {
        $post = Post::with('channel')
            ->withCount('comments')
            ->with('likes')
            ->with('user')
            ->where('posts.id', '=', $id)
//            ->get()
            ->first();

        $comments = Comment::where('postID', '=', $id)
            ->with('user')
            ->with('likes')
            ->orderBy('group', 'desc')
            ->orderBy('order', 'asc')
            ->orderBy('depth', 'asc')
            ->get();

How can i solve this problem ?

enter image description here

enter image description here

======================= EDIT ===============

A page showing a list of posts ( index.blade.php )

The above page does not have the data of each post.

I wrote that the modal was connected to the content I wrote before editing.

Can I open it in bootstrap modal when a post is clicked?
If it is possible to open modally, how can I do it?


Solution

  • In your, Routes/web.php is where you need pass your class through to a blade view.

    Route::get('/content/{id}', [PostController::class, 'show'])->where(['id' => '[0-9]+']);
    

    In your controller, you would then specify your return, in this case it would be a view

    return view('content', ['data' => $data]);