One page CRUD form operation (Laravel6) - php

I have a table in my database with contact details. On my form, I have a Dropdown which displays the names of people, and which, once I click on the name, fills in the fields with the data (ajax).
The problem happens when I want to perform operations on the form data.
For example, I click on "M Bram Manu" (id = 1) in the Dropdown. My Ajax will fill out my form with the details of this contact.
But when I want to update, delete or add a new contact, it doesn't work properly.
So, if I click on "delete", it not delete the contact I choose but the last contact of the dropdown list. Why???
So I think my link retrieves the last ID from my dropdown, so I decided to put an hidden Input with the value of the contact ID that appears in the form. But I don't know how to retrieve this value to define it as the ID to send to the controller.
Dropdown bar and link button :
<form class="col-md-9">
<label class="col-md-2">Rechercher : </label>
<select class="form-control select2 col-md-7" id="selInscrit" name="selInscrit" onchange="selectID()">
<option value="0" selected="selected"></option>
#foreach($inscrit as $inscrits)
<option value="{{$inscrits->INS_ID}}">{{$inscrits->INS_CIVILITE}} {{$inscrits->INS_NOM}} {{$inscrits->INS_PREN}} {{$inscrits->INS_NUM_RUE}} {{$inscrits->INS_Rue}} </option>
#endforeach
</select>
</form>
<div class="btn-group btn-group-sm col-md-3" role="group">
<form action="" method="GET">
<button type="submit" class="btn btn-secondary btn-sm">Edit</button>
</form>
<button type="submit" form="registerForm" formmethod="POST" formaction="{{ route('inscrits.store') }}" class="btn btn-secondary btn-sm">Save</button>
<button type="submit" form="registerForm" formmethod="POST" formaction="{{ route('inscrits.update') }}" class="btn btn-secondary btn-sm">Update</button>
<form action="{{ route('inscrits.destroy') }}" method="POST">
<input id="INS_ID" name="INS_ID" value="" type="hidden">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-secondary btn-sm">Delete </button>
</form>
</div>
Ajax for dropdown :
<script>
$('#selInscrit').change(function() {
var id = $(this).val();
var url = '{{ route("inscrits.show", ":id") }}';
url = url.replace(':id', id);
$.ajax({
url: url,
type: 'get',
dataType: 'json',
success: function(response) {
if (response != null) {
$('#INS_ID').val(response.INS_ID);
$('#INS_CIVILITE').val(response.INS_CIVILITE);
$('#INS_NOM').val(response.INS_NOM);
$('#INS_PREN').val(response.INS_PREN);
$('#INS_NAISS').val(response.INS_NAISS);
$('#INS_AGE').val(response.INS_AGE);
$('#INS_NUM_RUE').val(response.INS_NUM_RUE);
$('#INS_Rue').val(response.INS_Rue);
$('#INS_TEL1').val(response.INS_TEL1);
$('#INS_OBS').val(response.INS_OBS);
$('#INS_DATE').val(response.INS_DATE);
$('#INS_TEL2').val(response.INS_TEL2);
}
}
});
});
Route :
// INSCRITS
/ route for index
Route::get('/inscrits', 'InscritController#index')->name('inscrits.index');
// route for dropdown bar
Route::get('/inscrits/show/{id}', 'InscritController#show')->name('inscrits.show');
// route for update
// Route::match(['put', 'patch'], '/inscrits/update','InscritController#update')->name('inscrits.update');
Route::post('/inscrits/update','InscritController#update')->name('inscrits.update');
// route for store
Route::post('/inscrits/store', 'InscritController#store')->name('inscrits.store');
//route for delete
Route::delete('/inscrits/destroy', 'InscritController#destroy')->name('inscrits.destroy');
Form :
<form id="registerForm">
#csrf
<div class="form-group row">
<div class="col-lg-1">
<label for="INS_CIVILITE">Civilité</label>
<select class="form-control form-control-sm" id="INS_CIVILITE" name="INS_CIVILITE">
<option value="" selected="selected"></option>
<option value="Mme">Mme</option>
<option value="Mlle">Mlle</option>
<option value="M.">M.</option>
</select>
</div>
<div class="col-lg-4">
<label for="INS_NOM">Nom</label>
<input class="form-control form-control-sm" id="INS_NOM" name="INS_NOM" value="" type="text">
</div>
<div class="col-lg-3">
<label for="INS_PREN">Prénom</label>
<input class="form-control form-control-sm" id="INS_PREN" name="INS_PREN" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_NAISS">Année Naiss</label>
<input class="form-control form-control-sm" id="INS_NAISS" name="INS_NAISS" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_AGE">Age</label>
<input class="form-control form-control-sm" id="INS_AGE" name="INS_AGE" value="" type="text">
</div>
</div>
<div class="form-group row">
<div class="col-lg-1">
<label for="INS_NUM_RUE"># Rue</label>
<input class="form-control form-control-sm" id="INS_NUM_RUE" name="INS_NUM_RUE" value="">
</div>
<div class="col-lg-9">
<label for="INS_Rue">Libellé voie</label>
<select class="form-control form-control-sm" id="INS_Rue" name="INS_Rue">
#foreach($rue as $rues)
<option value="{{$rues->RUE_NUM}}">{{$rues->RUE_NOM}} ({{$rues->RUE_Type}})</option>
#endforeach
</select>
</div>
<div class="col-lg-2">
<label for="INS_TEL1">Téléphone 1</label>
<input class="form-control form-control-sm" id="INS_TEL1" name="INS_TEL1" value="" type="text">
</div>
</div>
<div class="form-group row">
<div class="col-lg-8">
<label for="INS_OBS">Observation</label>
<input class="form-control form-control-sm" id="INS_OBS" name="INS_OBS" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_DATE">Date d'inscription</label>
<input class="form-control form-control-sm" id="INS_DATE" name="INS_DATE" value="" type="text">
</div>
<div class="col-lg-2">
<label for="INS_TEL2">Téléphone 2</label>
<input class="form-control form-control-sm" id="INS_TEL2" name="INS_TEL2" value="" type="text">
</div>
</div>
<input id="INS_LIEU" name="INS_LIEU" value="WEB" type="hidden">
<input id="INS_EID" name="INS_EID" value="" type="hidden">
</form>
The controller :
public function index()
{
$inscrit = Inscrit::all();
return view('index', compact('inscrit'));
}
public function store(Request $request)
{
$storeData = $request->validate([
'INS_CIVILITE' => 'max:15',
'INS_NOM' => 'max:50',
'INS_PREN' => 'max:50',
'INS_NUM_RUE' => 'max:8',
'INS_TEL1' => 'max:10',
'INS_TEL2' => 'max:10',
'INS_AGE' => 'numeric',
'INS_OBS' => 'max:255',
'INS_Rue' => 'max:255',
'INS_DATE' => 'max:255',
'INS_NAISS' => 'max:255',
]);
$inscrit = Inscrit::create($storeData);
return redirect('/inscrits')->with('completed', 'Nouvel inscrit !');
}
public function show($id = 0)
{
$data = Inscrit::where('INS_ID', $id)->first();
return response()->json($data);
}
public function edit($id)
{
$inscrit = Inscrit::findOrFail($id);
return view('index', compact('inscrit'));
}
public function update(Request $request, $id)
{
$updateData = $request->validate([
'INS_CIVILITE' => 'max:15',
'INS_NOM' => 'max:50',
'INS_PREN' => 'max:50',
'INS_NUM_RUE' => 'max:8',
'INS_TEL1' => 'max:10',
'INS_TEL2' => 'max:10',
'INS_AGE' => 'numeric',
'INS_OBS' => 'max:255',
'INS_Rue' => 'max:255',
'INS_DATE' => 'max:255',
'INS_NAISS' => 'max:255',
]);
$id = request()->input('INS_EID');
Inscrit::where('INS_ID', $id)->update($updateData);
return redirect('/inscrits')->with('completed', 'inscrit mis à jour');
}
public function destroy($id)
{
$id = request()->input('INS_ID');
$inscrit = Inscrit::where('INS_ID', $id)->delete();
return redirect('/inscrits')->with('completed', 'inscrit supprimé');
}
The form
Database

Something like :
public function edit ($id) {
$inscritContent = Inscrit::where('id', $id)->firstOrFail();
return view('admin.inscrit.edit.form', [
'content' => $iscritContent
]);
}
public function update($id, Request $req) {
$inscrit = Inscrit::findOrFail($id);
$inscrit->update($req->all());
return redirect(route('admin.inscrit.index'));
}
admin.inscrit.edit.form and admin.inscrit.index are blade template

Related

Update data in laravel, when there a data that is primary key

i want to edit data, and the id is primary key.
but when i submit, the database does not change.
here the code
controller :
public function edit($id)
{
$karyawan = Karyawan :: find ($id);
$jabatan = Jabatan::all();
return view ('/karyawan.editData', compact(('karyawan'),('jabatan')));
}
public function update(Request $request, $id)
{
$request->validate([
'id' => 'required',
'nama'=> 'required',
'umur'=>'required',
'alamat'=> 'required',
'nomor'=> 'required'
]);
$karyawan =Karyawan::find($id);
$karyawan->jabatan_id = $request->jabatan;
$karyawan->nama = $request->nama;
// $karyawan->id = $request->id;
$karyawan->umur = $request->umur;
$karyawan->alamat = $request->alamat;
$karyawan->nomor = $request->nomor;
$karyawan->save();
return redirect ('/karyawan');
}
and the editData :
#extends ('layout')
#section ('content')
<h1>Edit Data Karyawan </h1>
<form action="/karyawan/{{ $karyawan->Id }}" method="post">
{{-- <form action = "/karyawan" method="post"> --}}
#csrf
<div class="form-group">
<label for = "title">ID Jabatan</label>
<select name="jabatan" class="form-control">
#foreach ($jabatan as $baris)
<option value = "{{ $baris->id}}">{{ $baris->nm_jabatan }}</option>
#endforeach
</select>
<div class = "form-group">
<label for="title"> nama</label>
<input type="text" class="form-control" name="nama">
</div>
<div class = "a">
<label for="title"> ID </label>
<input type="number" class="form-control" name="id" value = "{{ old('id') ? old('id'): $karyawan->id }}">
</div>
<div class = "form-group">
<label for="title"> umur</label>
<input type="text" class="form-control" name="umur">
</div>
<div class = "form-group">
<label for="title"> alamat</label>
<input type="text" class="form-control" name="alamat">
</div>
<div class = "form-group">
<label for="title"> nomor</label>
<input type="text" class="form-control" name="nomor">
</div>
<a href="/karyawan">
<button type = "button" class="btn btn-warning">kembali</button>
</a>
<button type ="submit" class = "btn btn-primary">Simpan</button>
</form>
#endsection
how i can update the data? from these code
i expect to get some explanation, and show me how to fix my code. so i can submit the update data on my database.
Try this in your controller
public function update(Request $request, $id)
{
$validated_data = $request->validate([
'name' => 'required|max:255', (add all the form here)
]);
YourModel::where('id', $id)->update($validated_data);
return redirect('admin/problem')->with('success', 'Data berhasil diubah');
}
Tips* dump all the request with dd($request->all) so you can see all of your input form data

My Laravel Website is redirecting all post request to index page

Having Problem with POST Request
All the get routes are working fine.
Post route when submiting any form it gets updated in DB and then redirected to homepage but where I go back to previous link. I see that I am having all the success and errors on that page before getting redirected to homepage.
My POST Route:
Route::post('/update-faq', 'FaqController#update')->name('admin.faq.update');
My Controller Function:
public function index(){
$all_faqs = Faq::all();
return view('backend.pages.faqs')->with(['all_faqs' => $all_faqs]);
}
public function update(Request $request){
$this->validate($request,[
'title' => 'required|string',
'description' => 'required|string',
'status' => 'nullable|string|max:191',
]);
Faq::find($request->id)->update([
'title' => $request->title,
'description' => $request->description,
'status' => $request->status,
'is_open' => !empty($request->is_open) ? 'on' : '',
]);
return redirect()->back()->with(['msg' => 'Faq Updated...','type' => 'success']);
}
My View:
<div class="modal fade" id="faq_item_edit_modal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">{{ 'Edit Faq Item' }}</h5>
<button type="button" class="close" data-dismiss="modal"><span>×</span></button>
</div>
<form action="{{ route('admin.faq.update') }}" id="faq_edit_modal_form" enctype="multipart/form-data"
method="post">
<div class="modal-body">
#csrf
<input type="hidden" name="id" id="faq_id" value="">
<div class="form-group">
<label for="edit_title">{{ 'Title' }}</label>
<input type="text" class="form-control" id="edit_title" name="title"
placeholder="{{ 'Title' }}">
</div>
<div class="form-group">
<label for="edit_is_open">{{ 'Is Open' }}</label>
<label class="switch">
<input type="checkbox" name="is_open" id="edit_is_open">
<span class="slider"></span>
</label>
</div>
<div class="form-group">
<label for="edit_description">{{ 'Description' }}</label>
<textarea name="description" id="edit_description" cols="30" rows="10"
class="form-control max-height-150" placeholder="{{ 'Description' }}"></textarea>
</div>
<div class="form-group">
<label for="edit_status">{{ 'Status' }}</label>
<select name="status" id="edit_status" class="form-control">
<option value="publish">{{ 'Publish' }}</option>
<option value="draft">{{ 'Draft' }}</option>
</select>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'Close' }}</button>
<button type="submit" class="btn btn-primary">{{ 'Save Changes' }}</button>
</div>
</form>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$(document).on('click', '.faq_edit_btn', function() {
var el = $(this);
var id = el.data('id');
var title = el.data('title');
var form = $('#faq_edit_modal_form');
form.find('#faq_id').val(id);
form.find('#edit_title').val(title);
form.find('#edit_description').val(el.data('description'));
form.find('#edit_status option[value="' + el.data('status') + '"]').attr('selected', true);
if (el.data('is_open') != '') {
form.find('#edit_is_open').attr('checked', true);
} else {
form.find('#edit_is_open').attr('checked', false);
}
});
});
</script>

Not getting error but data was not stored in database in laravel 8

Hello everyone i'm actually stuck for hours because of this, im new to laravel and idk whats wrong with my code and there is no actual message error, please help me
This my store function
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'jenis_id' => 'required',
'detjenis_id' => 'required',
'email' => 'required',
'message' => 'required',
]);
$asep = $request->name;
dd($asep);
$order = new DataOrder();
$order->name = $request->name;
$order->jenis_id = $request->jenis_id;
$order->detjenis_id = $request->detjenis_id;
$order->email = $request->number;
$order->message = $request->message;
$order->save();
//return back()->with('alert-success', '<script> window.onload = swal("Sukses!", "Data telah terkirim!", "success")</script>');
// if ($order->save()) {
// return redirect(route('home'))->with('alert-success', '<script> window.onload = swal("Sukses!", "Data telah terkirim!", "success")</script>');
// } else {
// return redirect(route('home'))->with('alert-success', '<script> window.onload = swal("Oops !" , "Data gagal terkirim!!" , "error")</script>');
// }
}
This my blade code
<form action="{{route('order.store')}}" method="POST">
{{csrf_field()}}
<div class="tm-section-wrap bg-white">
<section id="talk" class="row tm-section">
<div class="col-xl-6 mb-5">
<div class="tm-contact-form-wrap">
<div class="tm-contact-form">
<div class="form-group">
<input type="text" id="name" name="name"
class="form-control rounded-0 border-top-0 border-right-0 border-left-0"
placeholder="Nama anda..." required="" />
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Kebutuhan</label>
<select name="jenis_id" class="form-control" id="jenis_id">
<option value="" disabled selected>Pilih dibawah ini:</option>
#foreach ($jenis as $j)
<option value="{{$j->id}}">{{$j->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">Detail</label>
<select name="detjenis_id" class="form-control" id="detjenis_id">
</select>
</div>
<div class="form-group">
<input type="number" id="number" name="number"
class="form-control rounded-0 border-top-0 border-right-0 border-left-0"
placeholder="Nomor HP" required="" />
</div>
<div class="form-group">
<textarea rows="4" id="message" name="message"
class="form-control rounded-0 border-top-0 border-right-0 border-left-0"
placeholder="Message..." required=""></textarea>
</div>
<div class="form-group mb-0">
<button type="submit" class="btn rounded-0 d-block ml-auto tm-btn-primary">
SEND
</button>
</div>
</div>
</div>
</div>
</section>
</div>
and my route setup
Route::get('/testcrud', 'HomeController#testcrud')->name('testcrud');
Route::resource('SAS/order', 'DataOrderController');
Thank you if there anyone can help me with this problem, i'm really appreciate that.
As I can see, There is email field is missing in your form and you are using :
'email' => 'required'
Thats why data is not saving in database. For displaying validation error you can check here
are you sure that you already put all the field name,jenis_id,detjenis_id,email,message
in the fillable array in the model?
or if you don't want to bother doing that? you can just create a empty
protected $guarded variable on model.
And are you sure about the field? because i see you put number on email field.

Unable to upload a image ; laravel 6.0

I'm a new to Laravel and using Laravel6.0. While doing validation I'm getting a error.
The picture must be an image. The picture must be a file. The picture must be a file of type: jpeg, png, jpg, gif.
I'm searching a solution on google, but cannot find correct an answer.
I added dd(request) in my rules() ,but I got the same error.
This is my ContentController, ContentRequest and create.blade.php.
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-body">
<form action="{{route('content.store')}}" method="post" enctype="multipart/form-data" >
#csrf
<div class="form-group">
<label for="exampleFormControlInput1">タイトル</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="旅のタイトル" name="title" value="{{old('title')}}" >
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">大陸名</label>
<select class="form-control" id="exampleFormControlSelect1" name="continent" value="{{old('continent')}}">
<option>アジア</option>
<option>北アメリカ</option>
<option>中南米</option>
<option>ヨーロッパ</option>
<option>アフリカ</option>
<option>オセアニア</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">国名</label>
<input type="text" class="form-control" id="exampleFormControlInput1" placeholder="日本" name="country" value="{{old('country')}}">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">滞在期間</label>
<select class="form-control" id="exampleFormControlSelect1" name="span" placeholder="滞在期間を選択" >
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
 <option>12</option>
 <option>13</option>
 <option>14</option>
 <option>15</option>
 <option>16</option>
 <option>17</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlSelect1">費用</label>
<select class="form-control" id="exampleFormControlSelect1" name="costs" placeholder="金額を選択" value='{{old('costs')}}'>
<option>10000</option>
<option>30000</option>
<option>50000</option>
<option>70000</option>
<option>100000</option>
<option>150000</option>
<option>200000</option>
<option>250000</option>
<option>300000</option>
</select>
</div>
<div class="form-group">
<label for="exampleFormControlFile1">写真</label>
<input type="file" class="form-control-file" id="exampleFormControlFile1" name="picture" value="{{old('picture')}}">
<div class="input-group-append">
<button type="submit" class="btn btn-outline-secondary reset"><i class="fas fa-times fa-fw"></i>取消</button>
</div>
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">コンテンツ</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="5" name="content" >{{old('content')}}</textarea>
</div>
<input type="hidden" name="user_id" value="{{Auth::id()}}">
<button type="submit" class="btn btn-primary btn-lg btn-block">投稿</button>
</form>
</div>
</div>
#endsection
ContentRequest
public function rules()
{
return [
'user_id'=>'required',
'title' => 'required|string|max:255',
'continent' => 'required|string',
'picture'=>'required|image|max:1000',
'country' => 'required|string',
'costs'=>'required|numeric',
'span'=>'required|numeric',
'content'=>'required|string|max:250',
];
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContentRequest;
use App\Content;
class ContentController extends Controller
public function store(ContentRequest $request)
{
if($request->validated()){
$content =new Content;
$filename = $request->file('image')->store('public/image');
$content->picture = basename($filename);
$content->user_id = $request->user_id;
$content->content = $request->content;
$content->title = $request->title;
$content->span = $request->span;
$content->continent = $request->continent;
$content->country = $request-> country;
$content->costs = $request->costs;
$content->save();
}
return redirect('/');
}
You have to use like this
'picture' => 'required|image|mimes:jpeg,jpg,png|max:1000',
try this
public function rules()
{
return [
'user_id'=>'required',
'title' => 'required|string|max:255',
'continent' => 'required|string',
'picture'=>'required|mimes:jpeg,png,jpg,gif|max:1000',
'country' => 'required|string',
'costs'=>'required|numeric',
'span'=>'required|numeric',
'content'=>'required|string|max:250',
];
}
and in the store function
if($request->validated()){
$content =new Content;
$filename = $request->file('image')->store('public/image');
$content->picture = basename($filename);
$content->user_id = $request->user_id;
$content->content = $request->content;
$content->title = $request->title;
$content->span = $request->span;
$content->continent = $request->continent;
$content->country = $request-> country;
$content->costs = $request->costs;
$content->save();
}

upload more files to a folder with laravel

I try to upload multiple files to an specific folder using laravel. I was able to do this with just one file. But after I tried to upload multiple files it didn't work. First of all I tried to make an foreach. But it didn't work I tried to find solutions on the internet but unfortunately I didn't find one. Can anybody help me?
public function store(Request $request) {
// dd(request()->all());
$this->validate(request(), [
'name' => 'required|unique:lessons|max:20',
'type' => 'required',
'description' => 'required',
'price' => 'required',
'main_picture' => 'required',
'pictures' => 'required'
]);
$input = $request->all();
$images= array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('lesson images',$name);
$images[]=$name;
}
}
$lesson = Lesson::create( [
'name' =>$input['name'],
'type' => $input['type'],
'description' =>$input['description'],
'price' => $input['price'],
'main_picture' =>$input['main_picture'],
'pictures'=> implode("|",$images),
]);
$image_name = $request->file('main_picture')->getClientOriginalName();
$id = $lesson->id;
if($request->hasFile('main_picture')) {
$file = $request->file('main_picture');
$file->move('lesson images', $id . "-main-" . $image_name);
}
return redirect('/lessenoverzicht');
}
HTML:
<form method="post" enctype="multipart/form-data" action="/lessenoverzicht/toevoegen">
{{ csrf_field() }}
<div class="form-group">
<label for="name"><span class='required'>*</span>Naam les:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Voer naam les in..">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1"><span class='required'>*</span>Type</label>
<select name='type' class="form-control" id="type">
<option value="" disabled selected>Select your option</option>
<option value='les'>Les</option>
<option value='workshop'>Workshop</option>
</select>
</div>
<div class="form-group">
<label for="description"><span class='required'>*</span>Omschrijving:</label>
<textarea class="form-control" id="description" name="description" rows="10" placeholder="Voer omschrijving in..."></textarea>
</div>
<label for="description"><span class='required'>*</span>Prijs:</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">€</span>
</div>
<input type="number" value="0.00" min="0" step="1.00" class="form-control" id='price' name='price'>
</div>
<div class="form-group">
<label for="main_picture"><span class='required'>*</span>Main Image:</label>
<input type="file" class='form-control' id='main_picture' name="main_picture">
</div>
<div class="form-group">
<label for="picture">Afbeelding(en) omschrijving:</label>
<input type="file" class='form-control' id='picture' name="pictures" multiple>
</div>
<div class="form-group">
#include('layouts.errors')
</div>
<button type="submit" class="btn btn-primary">Toevoegen</button>
</form>
Can you try the below change in your code? I have only added Array type name property name="pictures[]"
<input type="file" class='form-control' id='picture' name="pictures[]" multiple>
Sample Code:
<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">
and this for multiple selection:
<input required type="file" class="form-control" name="images[]" placeholder="address" multiple>
PHP logic
public function store(request $request) {
$input=$request->all();
$images=array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('image',$name);
$images[]=$name;
}
}
/*Insert your data*/
Detail::insert( [
'images'=> implode("|",$images),
'description' =>$input['description'],
//you can put other insertion here
]);
return redirect('redirecting page');
}

Categories