【问题标题】:Laravel Inertia - Passing data Model->with hasMany relationshipLaravel Inertia - 传递数据模型-> 与 hasMany 关系
【发布时间】:2021-05-30 17:05:06
【问题描述】:

我正在使用 Laravel 8 和 InertiaJS Vue2。我正在尝试将具有 hasMany 关系的数据传递给 vue 进行编辑。如何将关系放入 Edit.vue 以进行表单编辑和发布?

// Controller
class BooksController extends Controller
{
    public function show(Request $request)
    {
        $book = Books::where('id',$request->id)->with('locations')->firstOrFail();
        return Inertia::render('Admin/Books/Edit', ['book' => $book]);
    }
}
// books model
class Books extends Model
{
    public function locations()
    {
        return $this->hasMany(BookLocation::class);
    }
}
// Edit.vue
<template>
    ...
    <div v-for="location in book.locations">
        <div>
            <jet-label for="location" value="Location" />
            <jet-input name="location" type="text" class="mt-1 block w-full" v-model="???"/>
        </div>
    </div>
</template>

...

<script>
data() {
    return {
        form: this.$inertia.form({
            locations: this.book.locations???
        })
    }
},
</script>

【问题讨论】:

    标签: vuejs2 laravel-8 inertiajs


    【解决方案1】:

    您需要使用 Eloquent Collections Load Method https://laravel.com/docs/8.x/eloquent-collections#method-load 手动加载关系。

    根据你的情况

    // Controller
    class BooksController extends Controller
    {
        public function show(Request $request)
        {
            $book = Books::where('id',$request->id)->with('locations')->firstOrFail();
            return Inertia::render('Admin/Books/Edit', ['book' => $book->load('locations')]);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-24
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 2019-03-04
      • 1970-01-01
      相关资源
      最近更新 更多