insert data through checkbox in laravel - laravel

I am trying to insert the checkbox data in database in which data are coming dynamically from database,
Here is the form :
<form action="{{ $action }}" method="post" enctype="multipart/form-data">
#csrf()
#if(!empty($method)) #method($method) #endif
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
#foreach($users as $k=>$u)
<tr>
<td><input type="checkbox" name="user_id[]" value="{{ $u->id }}"></td>
<td>{{ $u->name }}</td>
<td>{{ trim($evtitle) }}</td>
<td>{{ trim($evteacher) }}</td>
</tr>
#endforeach
</table>
<input type="text" name="question_id[]" value="{{ trim($evid) }}">
<input type="submit" class="btn btn-lg btn-success" value="Share" name="btn_btn_add_product">
</form>
Here is the controller :
$rs = ShareQuestion::create([
'user_id' => $request->input('user_id'),
'question_id' => $request->input('question_id')
]);
if($rs)
{
$message = array('flag'=>'alert-success', 'message'=>'Successfully');
return redirect()->route('auth.question.index')->with(['message'=>$message]);
}
$message = array('flag'=>'alert-danger', 'message'=>'Please try again');
return redirect()->route('auth.question.share')->with(['message'=>$message]);
Please help me out,

I think here is the issue as your checkbox is not checked so data is not passed to the controller function.
so change from(if you want to send all user_id)
<td><input type="checkbox" name="user_id[]" value="{{ $u->id }}"></td>
to
<td><input type="checkbox" name="user_id[]" checked value="{{ $u->id }}"></td>
or you can pass the condition for a checked attribute by
{{ Form::checkbox('user_id[]',$u->id,true, array('class'=>'some_class')) }}

$request->user_id and $request->question_id, array of values you have to use json_encode() method
$rs = ShareQuestion::create([
'user_id' => json_encode($request->user_id),
'question_id' => json_encode($request->question_id)
]);

Related

how to update an array in laravel

i have an array of data in a form that i want to update.when i update only the last column updated while the inputs that i updated do change.for example, we have a column size, price, and stock. the size include small,medium, and large and their respective prices and stock number. when i update the price of small size,it doesnt update but rather it updates the medium size.same for the large size also.i havent understood why only a specfic column is updating yet i have added a foreach to loop all the columns when updating.here is my update function.
public function editattributes(Request $request,$id)
{
$merchadisedata=Merchadise::select('id','merch_name','merch_code','merch_image')->find($id);
if ($request->isMethod('post')){
$data=$request->all();
foreach($data['attrid'] as $key=>$attr){
if(!empty($attr)){
Productattribute::where(['id'=>$data['attrid'][$key]])
->update([
'productattr_price'=>$data['productattr_price'][$key],
'productattr_stock'=>$data['productattr_stock'][$key],
]);
}
$message='Product attributes have been updated successfully';
Session::flash('success_message',$message);
return redirect()->back();
}
}
return view('backend.merchadise.editproductattributes')->with(compact('merchadisedata'));
}
on dd($data);die(); i get the inputs i have filled
here is the form in the blade file that am submitting
<form method="post" action="{{ url('admin/edit-attributes/'.$merchadisedata->id) }}">
{{csrf_field()}}
<table id="products" class="table table-striped table-bordered nowrap" style="width:100%;">
<thead>
<tr>
<th>Attribute Size</th>
<th>Attribute Stock</th>
<th>Attribute Price</th>
<th>Attribute Sku</th>
</tr>
</thead>
<tbody>
#foreach ( $merchadisedata->merchadiseattributes as $attribute)
<input type="text" name="attrid[]" value="{{ $attribute->id }}" style="display: none"/>
<tr>
<td>{{ $attribute->productattr_size}}</td>
<td>
<input type="number" name="productattr_stock[]" value="{{ $attribute->productattr_stock }}" required=""/>
</td>
<td>
<input type="number" name="productattr_price[]" value="{{ $attribute->productattr_price }}" required=""/>
</td>
<td>{{ $attribute->productattr_sku }}</td>
</tr>
#endforeach
</tbody>
</table>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-sm btn-block">Submit</button>
</div>
</form>
where might i be going wrong in my code

How do I match users permission with all the permissions in the database

I'm trying to get the user role with permissions and compare them with all permissions that are in the database, if they match then a checkbox should be checked... I'm not sure if I'm doing it correctly
RoleController :
public function edit($id)
{
$role =Role::where('id', $id)->with('permissions')->first();
$permissions = Permission::all();
return view('admin.roles.edit', compact('role', 'permissions'));
}
edit.blade.php :
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">TYPE</th>
<th scope="col">CREATE</th>
<th scope="col">VIEW</th>
<th scope="col">UPDATE</th>
<th scope="col">DELETE</th>
</tr>
</thead>
<tbody>
#foreach($permissions as $permission)
<tr>
<td>
<div class="custom-control custom-checkbox ml-3">
<input type="checkbox" class="custom-control-input" id="{{ $permission->name }}" value="{{ ($permission->name) }}" {{ ($permission->name == "$role->permissions->name") ? "checked" : "" }}>
<label class="custom-control-label" for="{{ $permission->name }}">{{ $permission->display_name }}</label>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
I'm not getting any errors but I cant make the checkbox to be checked if they match... I'm using Laratrust 5.2
You can do it like this,
#foreach($permissions as $permission)
#php
$attribute = ($permission->id === $role->permissions->id)
? 'checked="checked"'
: '';
#endphp
<label for="permission-{{ $permission->id }}">
<input type="checkbox" {{ $attribute }} class="custom-control-input" id="permission-{{ $permission->id }}" value="{{ ($permission->id) }}">
<span>{{ ($permission->name) }}</span>
</label>
#endforeach
$permissions and $role->permissions should share the id field with same integer value where applicable.
I've added some extra html as it's considered as a good practice and changed your id & value attributes so it becomes more precise & standard.

How to get Laravel Blade value as an Array data using Post Method?

I Have a Blade Form that will execute Post Method, this is my blade
#foreach ($dataku as $row => $order)
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-md-12 text-center">
<b>{{ $order->delivery_order_no }}</b>
<input type="hidden" name="order[{{ $row }}][do_id]" value="{{ $order->id }}">
<input type="hidden" name="order[{{ $row }}][so_id]" value="{{ $order->sales_order->id }}">
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive m-t-40" style="clear:both;">
<table class="table table-hover" style="font-size: 9pt;">
<thead>
<tr><th class="text-center">No</th>
<th class="text-center">SKUID</th>
<th class="text-center">Item Name</th>
<th class="text-center">UOM</th>
<th class="text-center">Qty So</th>
<th class="text-center">Qty Do</th>
<th class="text-center">Qty Confirm</th>
<th class="text-center">Qty Minus</th>
<th class="text-center">Remark Confirm</th>
</tr>
</thead>
<tbody>
#foreach ($order->delivery_order_details as $do =>$detOrder )
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $detOrder->skuid }}</td>
<td>{{ $detOrder->sales_order_detail->item_name }}</td>
<td>{{ $detOrder->uom->name }}</td>
<td>{{ $detOrder->sales_order_detail->qty }}</td>
<td>{{ $detOrder->qty_do }}</td>
<td>
<input type="hidden" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][skuid]]" value="{{ $detOrder->skuid}}">
<input type="number" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][qty_do]]" value="{{ $detOrder->qty_do }}">
</td>
<td>
<input type="number" min="0" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][qty_minus]]" value="0">
</td>
<td>
<input type="text" placeholder="Remark Confirm" class="form-control" name="order[{{ $row }}][detail[][{{ $do }}][remarks]]">
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
#endforeach
I Just want to get the data from the blade as an array ,... and then this is my controller
public function update(Request $request)
{
return $request->all();
}
I Get The Data Like This
is the return value from my blade $request->all(); correct? looks like something wrong ???
#sta provided an answer but I would make it more detailed:
When you build more complex structure (nested) in a html form input, every next key or list has to be surrounded by [].
So if you do just a nested keys it will be something[key1][key2][key3].
And if you do an array and a nested keys it will be something[key1][key2][][key3], where [] means that [key2] will be an array (and every array element will have a key3 key with provided value in input value attribute.
That's why this input name can't work:
order[0][detail[][0][skuid]]
but this one will work:
name="order[0][detail][0][qty_minus]

Uploading image can't saved at directory and having error "htmlspecialchars() expects parameter 1 to be string, array given "

i have a view input like that :
<form class="form-group" action="/user6" method="post" enctype="multipart/form-data">
<table class="table table-striped">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th style="width: 60px">Tidak Baik</th>
<th style="width: 60px">Baik</th>
</tr>
<div class="form-group">
<tr>
<td>1.</td>
<td><input class="form-control" style="border:none" type="text" name="question1" value="Kondisi, kebersihan, pelumasan bearing" readonly></td>
{{-- <td><input class="form-control" type="text" placeholder=".input-lg"></td> --}}
<td>
<label><input type="radio" name="answer1" value="tidak baik" checked></label>
</td>
<td>
<label><input type="radio" name="answer1" value="baik"></label>
</td>
</tr>
<tr>
<td></td>
<td> <input class="form-control" style="border:none" type="file" name="image" value="" readonly> </td>
</tr>
</div>
<tr>
<td></td>
<td> <div class="form-group">
<label>Catatan</label>
<textarea class="form-control" name="catatan" rows="3" placeholder="Enter ..." required></textarea>
</div>
</tr>
<input type="hidden" name="alat_id" value="7">
<input type="hidden" name="status" value="3 Bulanan">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</tbody>
</table>
<div class="box-footer">
<button type="submit" class="btn btn-primary" name="name" value="post">Submit</button>
</div>
</form>
and to save this i have controller :
public function store6(Request $request)
{
if($request->hasfile('image'))
{
foreach($request->file('image') as $file)
{
$name=$file->getClientOriginalName();
$file->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$user = new pemeliharaan;
$id = Auth::user()->id;
$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan']));
$user->catatan = $request->catatan;
$user->status = $request->status;
$user->save();
//dd($user);
return redirect('user/show6')->with('success', 'Data Telah Terinput');
}
before i add "enctype="multipart/form-data" , my view dont have error BUT cant display and saved image at directory . i want saved image to directory but cant saved .
i create a folder named 'images' at public . after i add enctype="multipart/form-data" . this view having error "htmlspecialchars() expects parameter 1 to be string, array given "
its my view after input :
<table class="table table-condensed">
<tbody><tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
{{-- <th style="width: 40px">Label</th> --}}
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['answer1'] }}
</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['image'] }}</td>
<td>
{{ $pemeliharaan->pertanyaan['image'] }}
</td>
</tr>
<td><img src="{{ url('images/'.$pemeliharaan->pertanyaan['image'])}}"></td>
</tbody></table>
Something in your table is array, it's not a string so when you put it inside {{}}, it will show error.
You should check
$pemeliharaan->pertanyaan['question1']
$pemeliharaan->pertanyaan['answer1']
$pemeliharaan->pertanyaan['image']
by using dd() function.
Deal with one problem at a time. At present you are storing the image but not saving the path to the file, which you saved in $data. You are not using $data anywhere.
Then you have a problem rendering the new model in the view. This is a totally different issue and is caused by having non-string data passed to the htmlspecialchars function which is what {{ }} does.

laravel, checkbox wont pass form

Why won't checkbox pass into Input? With other input types (text,password,select) it works fine.
form
<form method="POST" enctype="multipart/form-data" id="form">
#foreach ($query as $article)
<tr>
<td>{{ $article->title }}</td>
<td>{{ $article->slider }}</td>
<td><i class="fa fa-edit"></i> Edit</td>
<td><input type="checkbox" class="group-checkable" name="article[]" value="{{ $article->id }}"></td>
</tr>
<input name="test" value="tedt" hidden>
#endforeach
</form>
and controller
if (Request::isMethod('post'))
{
dd(Input::get('article'));
}

Resources