1

I'm using Laravel 8 with InertiaJS Vue2. I'm trying to pass data with hasMany relationship to the vue for editing. How do I put the relationship in Edit.vue for form editing and posting?

// 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>

2 Answers 2

3

You need to load the relationship manually with Eloquent Collections Load Method https://laravel.com/docs/8.x/eloquent-collections#method-load.

For your case

// 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')]);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

@Saw Giek Zhen's answer is correct, there is also another way to go about it

On the model who has the relation, in this case the Books you can add an attribute which will automatically include this relation in all requests

// books model
class Books extends Model
{
    public function locations()
    {
        return $this->hasMany(BookLocation::class);
    }

    protected $with = ['locations'];
}

this is called eager loading by default

you will then always have access to the locations as

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

also to get books without locations you can use the without function ;)

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

    public function showWithoutLocations(Request $request)
    {
        $book = Books::where('id',$request->id)->without('locations')->firstOrFail();
        return Inertia::render('Admin/Books/Edit', ['book' => $book]);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.