0

I can't get the update and delete functionality working. The create functionality works.

web.php:

Route::post('/income', [IncomeController::class, 'create']);
Route::put('/income/{id}', [IncomeController::class, 'update']);
Route::delete('/income/{id}', [IncomeController::class, 'delete']);

This is my controller:

class IncomeController extends Controller
{
    public function create(Request $request)
    {
        Income::create($request->all());
        return redirect()->route('home');
    }

    public function update(Request $request, Income $income)
    {
        $income->update($request->all());
        return redirect()->route('home');
    }

    public function delete(Income $income)
    {
        $income->delete();
        return redirect()->back();
    }
}

This is Income.vue which has create functionality that works:

<template>
    <IncomeUpdate v-for="income in incomes" :key="income.id" :income="income"/>
    
    <form @submit.prevent="create">
      <div>
        <div>
          <label>Description</label>
          <input v-model="form.description" type="text" />
        </div>
        <div>
          <label>Amount</label>
          <input v-model="form.amount" type="text" />
        </div>
        <div>
          <label>Day Deposited</label>
          <input v-model="form.day_deposited" type="text" />
        </div>
        <div>
          <button type="submit">Create Income</button>
        </div>
      </div>
    </form>
</template>

<script setup>
import { useForm } from '@inertiajs/vue3'
import IncomeUpdate from './IncomeUpdate.vue';
defineProps({
  incomes: Array,
})
const form = useForm({
  description: null,
  amount: null,
  day_deposited: null,
  frequency: "Monthly",
  notes: null
  })
const create = () => {
  form.post('/income');
  form.description = "";
  form.amount = "";
  form.frequency = "Monthly";
  form.day_deposited = "";
}
</script>

And this is IncomeUpdate.vue which has both update and delete functionality which DO NOT work:

<template>
    <form @submit.prevent="update">
      <div>
        <div @input="update">
          <input v-model="form.description" type="text" />
          <input v-model="form.amount" type="text" />
          <input v-model="form.day_deposited" type="text" />
          <input v-model="form.notes" type="text" />            
          <Link :href="`/income/${props.income.id}`" method="DELETE" as="button" preserve-scroll>&nbsp Delete</Link>
        </div>
      </div>
    </form>
  </template>
 
  <script setup>
  import { useForm } from '@inertiajs/vue3'
  import { Link } from '@inertiajs/vue3'
  const props = defineProps({
    income: Object,
  })
  const form = useForm({
    id: props.income.id,
    description: props.income.description,
    amount: props.income.amount,
    frequency: props.income.frequency,
    day_deposited: props.income.day_deposited,
    notes: props.income.notes,
  })
  const update = () => 
    form.put(`/income/${props.income.id}`, {preserveScroll:true})
  </script>

I can get it working with a resource controller! I'm just trying to play around with it to clean it up but I think there's something I don't understand. Thanks

1
  • 1
    What does not work specifically? Try to debug which side it is first. Is it the backend? Give a try to some terminal troubleshooting first or by using Postman. Once you're sure that the backend is working well, give a try to the Vue code to see if that is the part that does not work. On the frontend, inspecting the console and network tab is your way to move forward by inspecting the inputs/outputs of your frontend code. Commented Dec 23, 2024 at 8:33

0

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.