Laravel中的Websockets-第2部分:设置评论API和AJAX



我们都急于编写一些代码来与Websocket交互,但是首先我们需要建立必要的代码基础,以开始向用户提交和显示评论。设置好零件后,我们可以轻松地向其中添加Websocket,以进一步增强体验。

这一集将创建一个供我们与之交互的小型API,并将创建一些结构来保存我们的评论。

首先,我们需要创建数据库迁移来存储我们的注释。这将需要为我们的注释创建迁移,模型和控制器。

我们将为注释添加2个API端点。第一种是显示特定帖子的所有评论。第二个端点将允许我们通过API保存新评论。

然后通过设置前端来使用此API。我们将创建两个Vue.js方法来处理此问题。第一个是getComments(),它获取当前帖子的所有评论,第二个是postComments(),它将为当前帖子发布一个新评论。

下一步:实际上,我们连接了Websocket,以便在实时提交新评论时将其显示在页面上!

====书面教学====

“掌握Laravel中的Websockets”大师系列页面:

Mastering Websockets with Laravel Echo

第2部分:设置注释AJAX(书面教程)

Websockets in Laravel – Part 2: Creating Comments AJAX


====这一系列的更多内容。 ====

“掌握Laravel中的Websocket”系列的完整播放列表:

====下载源代码====

该系列的Github代码仓库:
https://github.com/DevMarketer/Laravel_Echo_Tutorial

下载此剧集的源代码:
https://github.com/DevMarketer/Laravel_Echo_Tutorial/releases/tag/Part2

====跟我来====

订阅新版本!

订阅DevMarketer Insider(电子邮件)
https://confirmsubscription.com/h/d/5EDC91CF554832D1

Twitter-http://twitter.com/_jacurtis
(问我问题!)

====问题? ====

在下面发表评论,我或其他人可以为您提供帮助。
对于快速问题,您可能还想在Twitter上问我,我几乎立即做出回应。

给我发电子邮件[email protected]

感谢您的支持! 。

28 comments
  1. In the store method of CommentController where you fetch the comment the from database again using with user, we can instead just use $comment->load('user'); and it will eager load the user. Thanks very much for this great detailed tutorial.

  2. if you got 404 not found error. then use full url in axios.get function. like this.
    axios.get('/echo/public/api/posts/'+this.post.id+'/comments')

  3. you are too slow man…
    I would like to advise you to make short length tutorials and kindly check after a few lines of code so if a view is watching he/she does not have to go back to the beginning of the tutorial to check where they have made a mistake.

  4. Awesome tutorial, although I encounter an error: basically vue inspector cannot find any Vue component. even in network tab Vue.js is not pulled into the application. Any idea? I also tried to run npm run development again :S….with not results. I would appreciate any help with this! thanks 🙂

  5. I get only curly braces at comment {{comment.user.name}} ..
    when I installed vue extension for chrome, it says "vue.js not detected"
    I also added the cdn script tag inside app.blade.php . What can be the problem?

  6. I have some little problem , when I save a comment it is giving 500 error code in network and there written that the problem is with mass assignment though I have the same code as yours. What should I do to solve this problem?

  7. I have a small question if you dont mind, what is better, to write the vue script inside the blade like you are doing here or write it in seperate component file? Seems easier to do it in blade since you get easy access to laravel data instead of having to make api calls for everything from the component, but this way youre making the code itself public right? which way is generally used in production?

  8. Alex thanks for great tutorials, I am a newbie to Laravel and I learnt a lot with you! I just have a problem, I can not load the comments to a posts. It returns an error post 403(Forbidden)
    Thanks again!

Comments are closed.