Nicho's avatar
Level 1

Laravel : not store into the database

Hello i'm creating a website for my final assignment on my last semester on college.

I'm creating a simple website where i can input some value, and upload files.

Right now i'm having a problem

The problem is that, when the forms are filled, with the uploaded files as well, it won't deliver the value of that form to the database

How do i resolve this problem??

The Controller :

public function store(Request $request)
    {
        //
        $request->validate([
            'name'=>'required',
            'city'=>'required',
            'category'=>'required',
            'desc'=>'required',
            'pdf'=>'required|mimes:pdf|max:2048',
            'est'=>'required',
            'price'=>'required|numeric',
            'image'=>'required|image|mimes:jpeg,png,jpg|max:2048',
            'packs'=>'required|numeric'
        ]);

        $package = new Package();
        
        $package->name = $request->input('name');
        $package->city = $request->input('city');
        $package->category = $request->input('category');

        $pdf = $request->file;
        $pdfname = time().'.'.$pdf->getClientOriginalExtension();
        $request->file->move('brosur',$pdfname);
        $package->pdf=$pdfname;

        $package->desc = $request->input('desc');
        $package->est = $request->input('est');
        $package->price = $request->input('price');

        $image = $request->file;
        $imagename = time().'.'.$image->getClientOriginalExtension();
        $request->file->move('images',$imagename);
        $package->image=$imagename;

        $package->packs = $request->input('packs');

        $package->status = 'Pending';

        if(Auth::id()){
            $package->user_id = Auth::user()->id;
        }
        $package->save();
        return redirect('/main');
    }

The blade.php :

<div class="container-fluid">
        <div class="row">
            <div class="col-sm-6">
                <div class="card mx-auto" style="width: 40rem; border-radius: 25px; outline-style: solid">
                    <div class="card-header">
                        <center>Holiday Package</center>
                    </div>
                    <div class="card-body">
                        <section class="col">
                            <form action="{{url('/store')}}" method="POST">
                                @csrf
                                <div class="form-group">
                                    <label>Nama Paket</label>
                                    <input type="text" required class="form-control @error ('name') is-invalid @enderror" id="name" name="name" autocomplete="off">
                                </div>
                                <div class="form-group">
                                    <label>Kota</label>
                                    <input type="text" required class="form-control @error ('city') is-invalid @enderror" id="city" name="city" autocomplete="off">
                                </div>
                                <div class="form-group">
                                    <label>Kategori</label>
                                    <input type="text" required class="form-control @error ('category') is-invalid @enderror" id="category" name="category" autocomplete="off">
                                </div>
                                <div class="form-group">
                                    <label>Deskripsi</label>
                                    <textarea name="desc" id="desc" cols="30" rows="10" autocomplete="off"></textarea>
                                </div>

                                <div class="form-group">
                                    <label>PDF</label>
                                    <input type="file" name="file">
                                </div>

                                <div class="form-group">
                                    <label>Estimasi</label>
                                    <input type="text" required class="form-control @error ('est') is-invalid @enderror" id="est" name="est" autocomplete="off">
                                </div>
                                <div class="form-group">
                                    <label>Harga</label>
                                    <input type="text" required class="form-control @error ('price') is-invalid @enderror" id="price" name="price" autocomplete="off">
                                </div>

                                <div class="form-group">
                                    <label>Image</label>
                                    <input type="file" name="file">
                                </div>

                                <div class="form-group">
                                    <label>Stock</label>
                                    <input type="text" required class="form-control @error ('packs') is-invalid @enderror" id="packs" name="packs" autocomplete="off">
                                </div>
                                <button type="submit" class="btn btn-info" id="btn_submit" 
                style="background-color: #eefa69; border:none; color:black">Submit</button> 
                            </form>
                        </section>
                    </div>
                </div>
            </div>
        </div>
    </div>
0 likes
8 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Add this to your form: enctype='multipart/form-data'

<form action="{{url('/store')}}" method="POST" enctype="multipart/form-data">

And also check your storage/logs files for any errors.

Snapey's avatar

you have some fields that don't display validation errors

Nicho's avatar
Level 1

@Snapey Ok, the error is the max size that i validate in the controller. So i remove the max size and it got me a new error

The error is : Call to a member function getClientOriginalExtension() on null

johnDoe220's avatar

I think you have to print the errors first to get the result

@if($errors->any())
<div class="alert alert-danger">
<ul>
                    @foreach($errors->all() as $error)
						<li>{{$error}}</li>			
                    @endforeach
</ul>
</div>
@endif

And then first check the value that reaches the method

public function store(Request $request)
    {
		dd($request->all());
	}
Nicho's avatar
Level 1

@johnDoe220 Thank you for the error code there friend. I tried it and the error caused by the max size that i declare on the controller. After i remove the max size validation on the controller, i got another error

The error is : Call to a member function getClientOriginalExtension() on null

Nicho's avatar
Level 1

@Snapey Yes, and the problem has been resolved thank you so much sir for the help. I really appreciate it :D

johnDoe220's avatar

for save data into public folder,first run

php artisan storage:link

and

public function store(Request $request)
    {
		$post = new Post();
		$post->image = $request->image->store('directory name to save file');
	}

blade file

<form action="{{route('posts.store')}}" method="POST" enctype="multipart/form-data">
		@csrf
	<input type="file" name="image">
</form>

Please or to participate in this conversation.