Undefined variable in action route with parameter for laravel - laravel

This is my view, and I want to pass r{{$i}}_selected to controller, but it gives me an error. Undefined variable: enterprise . How can I solve this?
<form name="frm-example" id="frm-example"
action="{{ route('pengawas_alokasi.store', $enterprise ) }}"
method="post">
{{ csrf_field() }}
<div class="table-responsive text-center">
<table class="table table-borderless table-striped table-hover" id="myTable">
<thead class="thead-dark">
<tr>
<th></th>
<th class="text-center">#</th>
<th class="text-center">Nama Perusahaan</th>
</tr>
</thead>
<?php $i=1;?> #foreach($enterprises as $enterprise)
<tr>
{{--
<td></td> --}}
<td>
<input type="checkbox" name="r{{$i}}_selected" value="{{$enterprise->id}}">
</td>
<td>{{$i}}</td>
{{-- <td>
<input type="hidden" name="nomor{{$i}}" value="{{$i}}" size="3">{{$i}}</td> --}}
<td>{{$enterprise->nama_perusahaan}}</td>
{{-- <td>
<input type="hidden" name="nama_perusahaan{{$i}}" value="{{$enterprise->nama_perusahaan}}" size="3">{{$enterprise->nama_perusahaan}}</td> --}}
</tr>
<?php $i=$i+1;?> #endforeach
</table>
</div>
<div class="form-group">
<input type="submit" name="" class="btn btn-primary" value="Submit">
</div>
</form>
This is the controller that responsible to the view above
public function insert($id)
{
$enterprises = Enterprise::get();
return view('pengawas.pengawas-insert-alokasi', compact('enterprises'));
}
This is the controller that should store the value that I want to retrieve in my view form
public function store_enterprise(Request $request){
//I'm not doing it yet
dd($request);
}

because you are sending "enterprises" from your controller and you are trying to send "enterprise" in your form's action

you can use hidden variable in your form instead of use URL variable :
<input type="hidden" name="enterprise" value="{{$enterprise}}">
and in your controller :
public function insert(Request $request)
{
//get enterprise value here
$enterprise = $request->enterprise;
}

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 to redirect back to previous page after post request in laravel?

I have a system where user enters the date range from and to and gets all required data between that date range. Data retrieved from the database shows in the table containing checkbox to each row. When user Check the check box and submit the data it updates to the database, but I'm have to redirect to the same table after updating the data in database. It says the GET method is not supported for this route. Supported methods: POST. Need Help.
Here is my Date range fill view.
#extends('master')
#section('content')
<div class="input-group" style="margin:50px">
<table>
<form action="/payment_list_table" method="POST">
#csrf
<div class="form-outline">
<tr>
<th>
<label>From</label>
</th>
<th>
<input type="date" name= 'from' class="form-control" placeholder="Doc Received Date" required="" />
</th>
<th>
<label>To</label>
</th>
<th>
<input type="date" name= 'to' class="form-control" placeholder="Doc Received Date" required="" />
</th>
</tr>
<th>
<button type="submit" class="btn btn-success">
<i class="fas fa-search"></i>
</button>
</th>
</div>
</form>
</table>
</div>
#endsection
Here is my Search Table View
#extends('master')
#section('content')
<div class='container' style="margin-top:50px">
<div class="row">
<div class="input-group" style="margin:20px">
<form>
<table style="float:right">
<th>
<div class="form-outline">
<input type="search" id="form1" class="form-control" placeholder="Search"/>
</th>
</form>
</div>
<form method="POST">
#csrf
<button type="submit" formaction="#" name="submit" class="checklistpo">PO</button>
<button type="submit" formaction="#" name="submit" class="ajax.po">AO</button>
<div class="table-responsive">
<table class="table custom-table">
<thead>
<tr>
<th scope="col">
<label class="control control--checkbox">
<input type="checkbox" class="js-check-all"/>
<div class="control__indicator"></div>
</label>
</th>
<th scope="col" >S.N</th>
<th scope="col">LC NO</th>
<th scope="col">Applicant</th>
<th scope="col">Doc Value</th>
<th scope="col">Doc Received date</th>
<th scope="col">LC Type</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
<?php $number = 1;?>
#foreach($datas as $items)
<tr>
<td><input type="checkbox" name="checkboxlist[]" value="{{$items->id}}" /></td>
<td>{{$number}}</td>
<td>{{$items->lc_no}}</td>
<td>{{$items->applicant}}</td>
<td>{{$items->doc_value}}</td>
<td>{{$items->rec_date}}</td>
<td>{{$items->sight_usance}}</td>
</tr>
<?php $number++; ?>
#endforeach
</tbody>
</table>
</div>
</form>
</div>
</div>
#endsection
Here is my Controller
function po(Request $req){
$validate=Validator::make($req->all(), [
"checkboxlist" => "required|array"
]);
foreach($req->checkboxlist as $list){
$data = Doc::find($list);
$data->payment_comment = "PO";
$data->Payment_status = "Prepared";
$data->save();
};
return Redirect::back();
if($validate->fails()){
return redirect()->back()->with("errors",$validate->errors());
}
}
And My Route
Route::post('po', [PaymentController::class, 'po']);
Your code needs a bit of formatting & make more understandable variables but anyways. You need to check if validation fails before foreach. return redirect()->back(); is the right way to go back.
function po(Request $req)
{
$validate = Validator::make($req->all(), [
"checkboxlist" => "required|array"
]);
if ($validate->fails()) {
return redirect()->back()->with("errors", $validate->errors());
}
foreach ($req->checkboxlist as $list) {
$data = Doc::find($list);
$data->payment_comment = "PO";
$data->Payment_status = "Prepared";
$data->save();
}
return redirect()->back();
}

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.

Form submit button in table without function

Can anyone explains me, why the submit buttons no and yes in the following code not trigger?
Without the table, it works.
<table class="table">
<thead>
<tr>
<th scope="col">Group</th>
<th scope="col">Yes No</th>
</tr>
</thead>
#foreach($invitations as $invitation)
{!! Form::open(array('route'=>'store.groupentry')) !!}
<tbody>
<tr>
<td>
Do you want to enter group {{$invitation->group_name}}?
</td>
<td>
<input type="hidden" name="idgroup" value="{{ $invitation->idgroup }} "/>
<input type="hidden" name="groupname" value="{{ $invitation->group_name }} "/>
<button type="submit" class="btn btn-default" name = "submitbutton" value = "save">Yes</button>
<button type="submit" class="btn btn-default" name = "submitbutton" value = "nosave">No</button>
{{ csrf_field() }}
</td>
</tr>
</tbody>
{!! Form::close() !!}
#endforeach
</table>
Maybe not valid html generation. You generate many forms wrap many tbody in one table. I think you need create one form, and before submit set hidden values with js

variable showing null value in view laravel

i am trying to sent data to view from function in controller but the variable view is not showing any result
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit')->with('row', $catg);
}
and my view is
#if(isset($row))
<form action="{{action('ProductController#edit')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="col-sm-12">
<h1 style="text-align:center;">Edit Items</h1>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Category</th>
<th>Item</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<input type="hidden" name="item_id" value="{{ $row->item_id}}">
<td><input class="form-control" name="item_name" value="{{$row->item_name}}" /></td>
<td><input class="form-control" name="item_price" value="{{$row->item_price}}" /></td>
<td><input class="btn btn-primary btn-block btn-flat" type="submit" value="Edit"></td>
</tr>
</tbody>
</table>
</div>
</form>
#endif
please help thanks
It seems you query returning null, that's the problem (there is no such ID in a database).
If you want to check for null, use is_null:
#if(is_null($var))
Or maybe you want to use empty:
#if(!empty($row))
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit')->with('row', $catg);
}
practically should be:
public function goedit($id)
{
$catg = Category::findOrFail($id);
return view('product.gocatedit')->with('row', $catg);
}
and then it seems that you aren't using default primary key, which is "id", so in your model add
public class Category{
...................
protected $primaryKey = "cat_id";
.....................
}
If you use ->with('something', 'data'), then the data passed will be in the session('something') and it's purpose is to 'flash' messages.
You need this (view data is supposed to be passed as an array, and as a second parameter of the view() function):
public function goedit($id)
{
$catg = Category::where('cat_id',$id)->first();
return view('product.gocatedit', ['row' => $catg]);
}

Resources