I have an array variable $screenshots that I am trying to pass to my Laravel view. Usually, I would use the @foreach and loop through the array, but I want to pass the full array to a Vue component by defining it as a prop. I want to do this so that I can loop over the array in the component. I am getting the htmlentities() expects parameter 1 to be string, array given error.

使用VueJS和Laravel的正确方法是什么?

Here is my blade template:

@section('content')

    <ticket-edit id="edit-ticket" single-ticket="{{ $ticket }}" screenshots="{{ $files }}">

    </ticket-edit>

@endsection

这是我的自定义组件(不同的文件):

<script>
    export default {

        template: '#edit-ticket-template',

        props: ['SingleTicket', 'screenshots'],

        data: function() {
            return {
                ticket: [],
                screenshots: []
            };
        },

        methods: {
            getTicket() {
                return this.ticket = JSON.parse(this.SingleTicket);
            },

            getScreenshots() {
                return this.screenshots = JSON.parse(this.files);
            },

            createNotes: function () {
                var ticketNotes = $('.summernote');
                ticketNotes.summernote({
                    height: 260,
                    toolbar: [
                        ['style', ['bold', 'italic', 'underline', 'clear', 'strikethrough']],
                        ['fontsize', ['fontsize']],
                        ['para', ['ul', 'ol']],
                    ]
                });
            }
        },

        created: function() {
            this.getTicket();
            this.getScreenshots();
        },

        ready: function() {
            this.createNotes();
        }

    }
</script>

EDIT: When I am adding attachments, I am using json_encode to encode the path to the attachments. Then when I retrieve them, I run json_decode in my model like so $files = json_decode($ticket->screenshots); So my controller looks like this:

public function edit($sub_domain, $id)
{
    $ticket = Ticket::find($id);
    $files = json_decode($ticket->screenshots);

    return view('templates.tickets-single', compact('ticket', 'files'));
}

推荐答案

This works - it was hard to find this answer on the web so I hope it helps! You have to bind it.

 <edit-ticket-template
      v-bind:SingleTicket="{{  json_encode($ticket) }}"
      v-bind: screenshots ="{{  json_encode($files) }}"
  >
  </edit-ticket-template>

Yeah I don't think you need to json_encode the single ticket but you get the point.

Laravel相关问答推荐

我是否需要/是否可以从控制器运行LARLAVEL备份?

拉威尔望远镜没有显示请求细节

Laravel 通过 API 嵌套 url 发布

如何使用 laravel 更新单个值

使用Laravel Blade Formatter和PHP Intelephense进行 VSCode 格式化

Laravel 5 控制台( artisan )命令单元测试

Laravel 没有定义

Laravel 生产问题 - 使用 Laravel 4.1.x 更新composer

laravel 预期响应代码 250 但得到代码530

错误:您的要求无法解析为一组可安装的软件包.

Cookie::forget 不工作 laravel 5.1

如何在 Laravel 查询中使用多个 OR、AND 条件

Laravel 5 - 为包创建 Artisan 命令

如何将 Facebook PHP SDK 与 Laravel 5.4 集成?

WhereHas Laravel 中的关系计数条件是什么

如何在 laravel 中使用 GROUP_CONCAT

判断输入是否来自控制台

在 laravel 的自定义路径中创建模型

Eloquent 模型不更新 updated_at 时间戳

如何在 laravel 表单验证错误消息中给出自定义字段名称