0

I want to store a data customer with this system, I was copy from other my system. the other system is worked fine but why this system not work as the other

FormCustomer

import { PlaceholderPattern } from '@/components/ui/placeholder-pattern';
import { Card, CardHeader, CardFooter, CardTitle, CardDescription, CardContent } from '@/components/ui/card';
import InputError from "@/components/input-error";
import { Button } from '@/components/ui/button';
import { ArrowLeftIcon, LoaderCircle } from 'lucide-react';
import AppLayout from '@/layouts/app-layout';
import { type BreadcrumbItem } from '@/types';
import { Head, Link, useForm } from '@inertiajs/react';
import { Textarea, Select, Input } from '@headlessui/react';
import { error } from 'console';
import React from 'react';

export default function Formcustomers({ ...props }) {

    const { customer , isView, isEdit } = props;

    // Add this at the top of your component, after the hooks
    console.log('Initial data:', {
        name: customer?.name || '',
        code: customer?.code || '',
        customer_logo: customer?.customer_logo || '',
        telp: customer?.telp || '',
        address: customer?.address || '',
    });


    const breadcrumbs: BreadcrumbItem[] = [
    {
        title: `${isView ? 'Show' : (isEdit ? 'Update' : 'Create' )} Customer`,
        href: route('customers.create'),
    },
];

    const {data, setData, post, put, processing, errors, reset} = useForm({
        name: customer?.name || '',
        code: customer?.code || '',
        customer_logo: null as File | null,
        telp: customer?.telp || '',
        address: customer?.address || '',
    });

    const submit = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault();

        if(isEdit){
            put(route('customers.update', customer.id ), {
                onSuccess: () => reset(),
            });
        } else {
            post(route('customers.store'), {
                onSuccess: () => reset(),
            });
        }
        // console.log('data', data);
    };

    const handleFileUpload = (e: React.ChangeEvent<HTMLInputElement>) => {
        if (e.target.files && e.target.files.length > 0) {
            setData('customer_logo', e.target.files[0]);
        }
    };

    return (
        <AppLayout breadcrumbs={breadcrumbs}>
            <Head title="Add Customer" />
            <div className="flex h-full flex-1 flex-col gap-4 rounded-xl p-4">
                <div className='ml-auto'>
                    <Link 
                        as='button' 
                        href={route('customers.index')} 
                        className='flex items-center bg-red-500 hover:opacity-85 p-2 rounded-lg text-md text-white w-fit cursor-pointer'>
                        <ArrowLeftIcon /> Back to List
                    </Link>
                </div>
                <Card>
                    <CardHeader>
                        <CardTitle>{isView ? 'Show' : isEdit ? 'Update' : 'Add'} Customer Data</CardTitle>
                    </CardHeader>
                    <CardContent>
                        <form onSubmit={submit} className='flex flex-col gap-4' autoComplete='off'>
                            <div className='grid gap-6'>
                                {/* Project Code */}
                                <div className='grid gap-2'>
                                    <label htmlFor="name">Customer Name</label>
                                    <Input 
                                        value={data.name}
                                        onChange={(e) => setData('name', e.target.value)}
                                        id='name'
                                        name='name'
                                        type='text'
                                        placeholder='Customer Name'
                                        autoFocus
                                        tabIndex={1}
                                        disabled={isView || processing}
                                    />
                                    {errors.name && <InputError message={errors.name} />}
                                </div>
                                {/* End Project Code */}
                                {/* Part No */}
                                <div className='grid gap-2'>
                                    <label htmlFor="code">Customer Code</label>
                                    <Input 
                                        value={data.code}
                                        onChange={(e) => setData('code', e.target.value)}
                                        id='code'
                                        name='code'
                                        type='text'
                                        placeholder='Customer Code'
                                        autoFocus
                                        tabIndex={1}
                                        disabled={isView || processing}
                                    />
                                    {errors.code && <InputError message={errors.code} />}
                                </div>
                                {/* End Part No */}
                                {/* Image Upload */}
                                <div className='grid gap-2'>
                                    <label htmlFor="customer_logo">Customer Logo</label>
                                    <Input onChange={handleFileUpload} id='customer_logo' name='customer_logo' type='file' autoFocus tabIndex={4} />
                                    {errors.customer_logo && <InputError message={errors.customer_logo} />}
                                </div> 
                                {/* End Image Upload*/}
                                {/* Part No */}
                                <div className='grid gap-2'>
                                    <label htmlFor="telp">Customer Telp</label>
                                    <Input 
                                        value={data.telp}
                                        onChange={(e) => setData('telp', e.target.value)}
                                        id='telp'
                                        name='telp'
                                        type='text'
                                        placeholder='(022) 65432111'
                                        autoFocus
                                        tabIndex={1}
                                        disabled={isView || processing}
                                    />
                                    {errors.telp && <InputError message={errors.telp} />}
                                </div>
                                {/* End Part No */}
                                {/* Description */}
                                <div className='grid gap-2'>
                                    <label htmlFor="address">Address</label>
                                    <Textarea 
                                        value={data.address}
                                        onChange={(e) => setData('address', e.target.value)}
                                        id='address'
                                        name='address'
                                        placeholder='Address'
                                        autoFocus
                                        tabIndex={1}
                                        rows={2}
                                        disabled={isView || processing}
                                    />
                                    {errors.address && <InputError message={errors.address} />}
                                </div>
                                {/* Description */}
                            </div>
                            {!isView && (
                                <Button type="submit" className="mt-2 w-fit cursor-pointer" tabIndex={4}>
                                    {processing && <LoaderCircle className='h4 w-4 animate-spin' />}
                                    {processing ? (isEdit ? 'Updating...' : 'Creating...') : isEdit ? 'Update' : 'Save'} Customer
                                </Button>
                            )}
                        </form>
                    </CardContent>
                </Card>
            </div>
        </AppLayout>
    );
}

ModelCustomer

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    protected $table = 'customers';

    protected $fillable = [
        'name', 'code', 'customer_logo', 'customer_logo_originalname', 'telp', 'address'
    ];
}

ControllerCustomer

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Customer;
use Exception;
use Inertia\Inertia;
use Illuminate\Http\Request;
use App\Http\Requests\CustomerFormRequest;
use Illuminate\Support\Facedes\Log;

class CustomerController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $customers = Customer::latest()->get()->map(fn($customer) => [
            'id' => $customer->id,
            'name' => $customer->name,
            'code' => $customer->code,
            'telp' => $customer->telp,
            'address' => $customer->address,
            'customer_logo' => $customer->customer_logo,
            'customer_logo_originalname' => $customer->customer_logo_originalname,
            'created_at' => $customer->created_at->format('dMy h:m'),
            'updated_at' => $customer->updated_at->format('dMy h:m'),
        ]);
        return Inertia::render('customers/indexcustomers', [
            'customers' => $customers,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        return Inertia::render('customers/formcustomers');
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(CustomerFormRequest $request)
    {
        try {
            $customerLogo = null;
            if ($request->file('customer_logo')) {
                $customerLogo = $request->file('customer_logo');
                $customerLogoOriginalName = $customerLogo->getClientOriginalName();
                $customerLogo = $customerLogo->store('erp/customer', 'public');
            }

            $customer = Customer::create([
                'name' => $request->name,
                'code' => strtoupper($request->code),
                'telp' => $request->telp,
                'address' => $request->address,
                'customer_logo' => $customerLogo,
                'customer_logo_originalname' => $customerLogoOriginalName,
            ]);

            if ($customer) {
                return redirect()->route('customers.index')
                    ->with('success', 'Customer Data inputted')
                ;
            }
            return redirect()->back()
                ->with('error', 'Customer not inputted, check again')
            ;
        } catch (Exception $e) {
            Log::error('Customer creation failed: ' . $e->getMessage());
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(Customer $customer)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Customer $customer)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(CustomerFormRequest $request, Customer $customer)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Customer $customer)
    {
        //
    }
}

And this SS for the ERROR [SQLSTATE[42S02]: Base table or view not found: 11461

1
  • Have you applied your migrations on the DB? Commented Jul 10 at 11:35

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.