Home > database >  self-termination how to open each post using modal in laravel 8
self-termination how to open each post using modal in laravel 8

Time:09-16

I didn't properly ask the question I was trying to ask. I'm going to try to organize the content again and post it. Sorry to everyone who replies.

CodePudding user response:

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]);

CodePudding user response:

As per the Stack Trace screenshot, you've attached I think You didn't pass your $post data to your blade.php from the controller.

check your code what you missed in there :)

CodePudding user response:

Before posting the question, I solved the problem as written below.

** Problem.
When I clicked on a post, a bootstrap modal opened and I had to put data from the controller in it.

** Process.
content.blade.php

<a href="" data-bs-toggle="modal" data-bs-post-id="{{ $post->id }}" data-bs-target="#open_post_modal">

 var open_post_modal = document.getElementById('open_post_modal')
    open_post_modal.addEventListener('show.bs.modal', function (event) {
        // Button that triggered the modal
        var button = event.relatedTarget;
        // Extract info from data-bs-* attributes
        var postID = button.getAttribute('data-bs-post-id');
        // postID = JSON.parse(post);

        // If necessary, you could initiate an AJAX request here
        // and then do the updating in a callback.
        // Update the modal's content.
        // var modalBody = open_post_modal.querySelector('.modal-content');
        var modalBody = $(".modal-content");
        $.ajax({
            url: '/post/' postID,
            type: 'get',
            // data: {
            // 'type': type,
            // },
            success: function(data) {
                // modalBody.innerHTML = data;
                modalBody.html(data);
            },
            error: function(err) {
                console.log(err);
            }
        })

    })

PostController.php

    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();

        return view('post.show', compact('post', 'comments'));
    }
  • Related