Показать результаты запроса в другом блейде - PullRequest
0 голосов
/ 08 мая 2019

Первоначально я пытался показать результаты в модальном режиме, но изменил его, чтобы показать результат в другом блейде. Я хочу получить все комментарии к сообщению, используя его post_id. Но я получаю следующую ошибку: Метод GET не поддерживается для этого маршрута. Поддерживаемые методы: POST.

Клинок, откуда я получаю свой post_id после нажатия Все комментарии:

<section class="row posts">
        <div class="col-md-6 col-md-3-offset">
            <header><h3>other posts</h3></header>
            @foreach($posts as $post)
            <article class="post">
                <p>{{ $post->content }}</p>
                <div class="info">Posted by {{ $post->user->username }} on {{ $post->created_at }}</div>
                <div class="interaction">
                    <a href="#" class="like" id="like" data-postid="{{ $post->id }}">{{ Auth::user()->likes()->where('post_id', $post->id)->first() ? 'Liked' : 'Like' }}</a> |
                    <a href="#" class="comment" id="comment" data-postid="{{ $post->id }}">Comment</a> |
                    <a href="{{ route('show.comments',['post_id' => $post->id]) }}" class="allcomments" id="allcomments" data-postid="{{ $post->id }}">All Comments</a>
                    @if(Auth::user() == $post->user)
                        |
                        <a href="#" class="edit" data-postid="{{ $post->id }}">Edit</a> |
                        <a href="{{ route('post.delete',['post_id' => $post->id]) }}">Delete</a>
                    @endif
                </div>
            </article>
            @endforeach
        </div>
    </section>

Маршрут:

Route::middleware(['web'])->group(function()
{
Route::post('/showcomment/{post_id}',[
        'uses' => 'CommentController@getComments',
        'as' => 'show.comments',
        'middleware' => 'auth'
    ]);
}

Контроллер:

public function getComment($post_id)
    {
        $comments = Comment::where('post_id',$post_id)->orderBy('created_at','desc')->get();
        return view('showcomments',['comments' => $comments]);
    }

Блейд, на котором я хочу вывести результат:

@extends('layouts.master')

@section('content')
    <section class="row posts">
        <div class="col-md-6 col-md-3-offset">
            <header><h3>other Comments</h3></header>
            @foreach($comments as $comment)
                <article class="comment">
                    <p>{{ $comment->body }}</p>
                    <div class="info">Made by {{ $comment->user->username }} on {{ $comment->created_at }}</div>
                    <div class="interaction">
                        @if(Auth::user() == $comment->user)
                            |
                            <a href="#" class="edit" data-commentid="{{ $comment->id }}">Edit</a> |
                            <!--copy delete from post here including the modal-->
                        @endif
                    </div>
                </article>
            @endforeach
        </div>
    </section>
@endsection

1 Ответ

0 голосов
/ 08 мая 2019

По сути, это мой подход:

Route::get("/posts/{post_id}/comments", "PostsController@getComments");

Контроллер:

// get comments...
public function getComments ($postId) {
    $comments = Post::findOrFail($postId)->comments;

   return view('showcomments',['comments' => $comments];
}

Чтобы вышесказанное сработало: вы уже должны были определить отношение комментария к публикации в этом случае в модели сообщения:

// Post Model: a post has many comments

public function comments () {
   return $this->hasMany(Comments::class, 'post_id');
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...