I got error this Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in - laravel

I am trying to update my record but unfortunately record is not updating i getting error please help me thanks.
Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, integer given, called in /home/zubair/htdocs/hourlog/cms/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 869
https://flareapp.io/share/q5YBBDmX
Controller
public function issuesUpdate(Request $request,Project $project)
{
$project_id =$project->id;
foreach($request->date as $key =>$value){
$issue = new Issue();
$issue->date = $request->date[$key];
$issue->issue = $request->issue[$key];
$issue->project_id = $project_id ;
$issue->save();
}
return redirect()->route('project');
}
html view
#foreach($issues as $key=>$details)
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input required type="date" value="{{$details->date}}" name="date[{{$key}}][]" class="form-control"
aria-describedby="emailHelp" >
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<input required type="text" value="{{$details->issue}}" name="issue[{{$key}}][]" class="form-control"
aria-describedby="emailHelp" placeholder="task...">
</div>
</div>
<div class="col-md-1 mt12">
<button type="button" class="btn btn-danger waves-effect
waves-light btn-sm delete">
<i
class="fa fa-times">
</i>
</button>
</div>
</div>
#endforeach
Route
Route::post('/projects/{project}/issues/update',
"ProjectController#issuesUpdate")->name('project.issues.update');

maybe this will help you try it
public function issuesUpdate(Request $request,Project $project)
{
$project_id =$project->id;
$dates = $request->get('date');
foreach($dates as $key =>$value){
$issue = new Issue();
$issue->date = $request->get('date');
$issue->issue = $request->get('issue');
$issue->project_id = $project_id ;
$issue->save();
}
return redirect()->route('project');
}

Related

How to use laravel redirect back with compact

I am trying to pass value from the price page via input and redirect back to the price page with a value that has been calculated. I am using two functions for this; one is for get price()-PagesController#pricing and the other one is for post feeCal()-PagesController#feeCal.
public function pricing(Request $request)
{
return response()->view('pages.price');
}
public function feeCal(Request $request)
{
$value = $request->input('price');
if ($value > 500 or $value > 20000) {
$fee = $value * 0.015;
}
// dd($fee);
return redirect()->back()->compact('fee');
}
<form action="{{ route('page.vfee')}}" method="POST">
#csrf
<div class="row">
<input type="number" name="price" class="form-control rounded mb-3" placeholder="Type the price here">
</div>
<div class="row">
<input type="submit" class="btn btn-danger w-100
rounded" value="GET TOTAL FEE">
</div>
</form>
<div class="card-footer">
<div class="text-muted">Total fee</div>
<div class="text-danger">
{{ $fee ?? '' }}
</div>
</div>
When you try to input a value it returns a black value but when you dump the fee variable in the controller function you get a result.
Try one of following
return redirect()->back()->with(compact('fee'));
or
return redirect()->back()->with('fee', $fee);

multiple field validation using livewire ? name.0.required is working but name.*.required is not working. Please provide me solution

I want to validate all field. But It is validating only first field. Append field is not validating
multiple fields validation using livewire? name.0.required is working but name.*.required is not working.
<?php
namespace App\Http\Livewire;
use Livewire\Component;
class Test extends Component
{
public $name;
public $inputs = [];
public $i = 1;
public function add($i)
{
$i = $i + 1;
$this->i = $i;
array_push($this->inputs ,$i);
}
public function remove($i)
{
unset($this->inputs[$i]);
}
public function store()
{
$validatedDate = $this->validate([
'name.0' => 'required',
'name.*' => 'required',
],
[
'name.0.required' => 'name field is required',
'name.*.required' => 'name field is required',
]
);
session()->flash('message', 'Name Has Been Created Successfully.');
}
public function render()
{
return view('livewire.test');
}
}
<div>
<form class="offset-md-3">
#if (session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
<div class="add-input">
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Name" wire:model="name.0">
#error('name.0') <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>
<div class="col-md-2">
<button class="btn text-white btn-info btn-sm" wire:click.prevent="add({{$i}})">Add</button>
</div>
</div>
</div>
#foreach($inputs as $key => $value)
<div class=" add-input">
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input type="text" class="form-control" placeholder="Enter Name" wire:model="name.{{ $value }}">
#error('name.'.$value) <span class="text-danger error">{{ $message }}</span>#enderror
</div>
</div>
<div class="col-md-2">
<button class="btn btn-danger btn-sm" wire:click.prevent="remove({{$key}})">remove</button>
</div>
</div>
</div>
#endforeach
<div class="row">
<div class="col-md-12">
<button type="button" wire:click.prevent="store()" class="btn btn-success btn-sm">Save</button>
</div>
</div>
</form>
</div>
This is my full code use to create multi field using livewire. I am not able to validate appended field so I need help to solve this problem. This validate first field name.0 other append field name.* does not validate.
You should use $key instead $value.
Like this:
wire:model="name.{{ $key }}"

How to update multiple record using Laravel?

I want to update multiple record related to project and I have already made logic for store data in controller; it's perfectly adding multiple record but I have no idea how to do so.
I want to also update multiple record that already exist in database.
https://ibb.co/sjNKMzJ
Model Issue
public function project()
{
return $this->hasOne('App\Project','project_id',id)
}
Model Project
public function issue()
{
return $this->hasMany('App\Issue','id','project_id')
}
return response
// http://localhost/hourlog/cms/public/projects/43/issues/update
{
"_token": "S6typbw0hywPqaUdxCTqWeJNieyl3VieQhCHqDZ7",
"date": [
"2020-07-06",
"2020-07-22",
"2020-07-11"
],
"issue": [
"edit issue",
"delete issue on web",
"update issue on cms"
]
}
Controller
public function issuesUpdate(Request $request, Project $project)
{
$issues =Issue::where('project_id',$project->id)->get();
//for add new record
foreach($request->date as $key2 => $val){
$issue = new Issue;
$issue->date = $val;
$issue->issue = $request->issue[$key2];
$issue->project_id = $project->id;
$issue->save();
}
return redirect()->route('project');
}
html view
<form action="{{ route('project.issues.update',[$project->id])}}" method="POST">
#csrf
<div class="portlet">
<div class="portlet-heading bg-light-theme">
<h3 class="portlet-title">
<span class="ti-user mr-2">
</span>Add Issues
</h3>
<div class="portlet-widgets">
<span class="divider">
</span>
<button type="submit" class="btn btn-white waves-effect btn-rounded">
<span class="btn-label">
<i class="fa fa-save">
</i>
</span> Save
</button>
</div>
<div class="clearfix">
</div>
</div>
<div id="bg-inverse" class="panel-collapse collapse show" style="">
<div class="portlet-body">
<div class="card-box">
<div class="row">
<div class="col-md-12 mt20">
<div class="addMore">
<div class="addmore_cont">
<div class="addMore_btn">
<div class="mt12 pull-right">
<button type="button" title="Add More" class="btn btn-success waves-effect
waves-light btn-sm add_more" data-key="">
<i class="fa fa-plus">
</i>
</button>
</div>
<div class="row addmore_issues">
<div class="col-md-12">
#if(count($issues))
#foreach($issues as $key => $details)
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input required type="date" value="{{$details->date}}"
name="date[]" class="form-control"
aria-describedby="emailHelp" >
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<input required type="text" value="{{$details->issue}}"
name="issue[]" class="form-control"
aria-describedby="emailHelp" placeholder="Issue...">
</div>
</div>
<div class="col-md-1 mt12">
<button type="button" class="btn btn-danger waves-effect waves-
light btn-sm delete">
<i
class="fa fa-times">
</i>
</button>
</div>
</div>
#endforeach
#else
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input required type="date" value="" name="date[]" class="form-control"
aria-describedby="emailHelp" >
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<input required type="text" value="" name="issue[]" class="form-control"
aria-describedby="emailHelp" placeholder="Issue...">
</div>
</div>
</div>
#endif
</div>
</div>
<!--end row-->
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
Route
Route::post('/projects/{project}/issues/update', "ProjectController#issuesUpdate")-
>name('project.issues.update');
First change your data format from
"date": [
"2020-07-06",
"2020-07-22",
"2020-07-11"
],
"issue": [
"edit issue",
"delete issue on web",
"update issue on cms"
]
To
[
["date"=>"2020-07-06","issue"=>"edit issue"],
["date"=>"2020-07-22","issue"=>"delete issue on web"],
["date"=>"2020-07-11","issue"=>"update issue on cms"]
]
And you can make it by using like below html format
<input required type="date" value="{{$details->date}}"
name="issues[{{$key}}][date]" class="form-control"
aria-describedby="emailHelp" >
Inside Controller
public function issuesUpdate(Request $request, Project $project)
{
$issues = $request->issues;
$insertData = [];
foreach($issues as $key=>$issue){
$issue["project_id"] = $project->id;
$insertData[] = $issue;
}
Issue::where('project_id',$project->id)->delete();// to not insert duplicate issues for the same project
Issue::insert($insertData);
return redirect()->route('project');
}

Laravel - How to Pass Parameter to return redirect

In my Laravel-5.8, I have:
public function manager_employee_goal_list($id)
{
$goal = AppraisalGoal::findOrFail($id);
$goaldetails = AppraisalGoalDetail::where('appraisal_goal_id', $id)->get();
return view('appraisal.appraisal_goals.manager_employee_goal_list')
->with('goal', $goal)
->with('goaldetails', $goaldetails);
}
public function manager_employee_goal_approve(Request $request, $id)
{
$goal = AppraisalGoal::find($id);
$goal->is_approved = 3;
$goal->line_manager_comment = $request->line_manager_comment;
$goal->save();
Session::flash('success', 'Goal is approved');
return redirect()->route('appraisal.appraisal_goals.manager_employee_goal_list');
}
view : manager_employee_goal_list
<a class="btn btn-xs btn-info" data-toggle="modal" data-target="#approve{{ $goal->id }}" data-original-title="Approve">
Approve
</a>
<div class="modal fade" id="approve{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{route('appraisal.appraisal_goals.manager_employee_goal_approve',['id'=>$goal->id])}}" method="post">
{{ csrf_field() }}
<div class="modal-header">
Approve Goal
</div>
<div class="col-md-12">
<div class="form-group">
<label class="control-label">Comment</label>
<textarea rows="2" name="line_manager_comment" class="form-control" placeholder="Enter Comment here" value="{{old('line_manager_comment')}}" required data-validation-required-message="This field is required"></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-success btn-ok">Approve Goal</button>
</div>
</form>
</div>
</div>
</div>
route/web.php
Route::get('appraisal_goals/manager_employee_goal_list/{id?}', 'Appraisal\AppraisalGoalsController#manager_employee_goal_list')->name('appraisal.appraisal_goals.manager_employee_goal_list');
Route::post('appraisal_goals/manager_employee_goal_approve/{id?}', 'Appraisal\AppraisalGoalsController#manager_employee_goal_approve')->name('appraisal.appraisal_goals.manager_employee_goal_approve');
When I submit on the modal form, it saves but couldn't redirect to:
return redirect()->route('appraisal.appraisal_goals.manager_employee_goal_list');
It displays this error
Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::manager_employee_goal_list(), 0 passed and exactly 1 expected
How do I resolve it?
Thanks
manager_employee_goal_list method expects an ID, all you have to do is to pass that ID, like so:
return redirect()->route(
'appraisal.appraisal_goals.manager_employee_goal_list',
['id' => $id]
);
Also I suggest you to either remove question mark after id in your defined route, or update arguments from your method by making them nullable
public function manager_employee_goal_list($id = null)
and of course you have to adjust code too

Error 500 in Laravel ajax

I got this error which I tried to fix with no success
in console
xhr.send( options.hasContent && options.data || null );//error
js
var data_id;
$(document).ready(function() {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
}
});
$(".edit_cost").click(function() {
data_id = $(this).closest('div.portlet').attr('data-id');
});
$(".submit_cost").on('click',function(e) {
e.preventDefault();
var type = $(".cost_form").find('input[name="type"]').val();
var cost = $(".cost_form").find('input[name="cost"]').val();
var revenue = $(".cost_form").find('input[name="revenue"]').val();
var profit = $(".cost_form").find('input[name="profit"]').val();
var data = $("form.cost_form").serialize();
alert(data);
$.ajax({
url: "/partA-edit-cost",
type: "POST",
data: {
// 'types': type,
//'cost': cost,
// 'revenue': revenue,
// 'profit': profit,
// 'data_id': data_id
// 'data' : data
},
error: function(data) {
console.log(data);
},
success: function(log) {
console.log(log);
}
})
});
});
Routes
Route::post('/partA-edit-cost', 'CostController#editCost');
html
#foreach($costs as $widget)
<li data-row="1" data-col="1" data-sizex="3" data-sizey="3">
<div class="portlet portlet-sortable light bordered ui-widget-content ui-resizable" data-id="{{$widget->id }}" data-find="{{$widget->id.$widget->name }}" data-name="{{ $widget->name }}">
<div class="portlet-title ui-sortable-handle">
<div class="caption font-green-sharp">
<i class="fa fa-money"></i>
<span class="caption-subject bold uppercase">{{ $widget->name }}</span>
</div>
<div class="actions">
<a href="javascript:;" class="btn btn-circle btn-default btn-sm remove">
<i class="fa fa-trash-o"></i> Remove </a>
<div class="btn-group">
<button type="button" class="btn btn-primary edit_cost" data-toggle="modal" data-target="#cost"><i class="fa fa-edit" data-button ="{{$widget->id}}"></i>Edit</button>
</div>
<a class="btn btn-circle btn-icon-only btn-default fullscreen" href="javascript:;"></a>
</div>
</div>
<div class="portlet-body">
<div>{!! html_entity_decode($widget->api) !!}</div>
</div>
</div>
</li>
#endforeach
<div class="modal fade bs-example-modal-sm cost" id="cost" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel">
<div class="modal-dialog modal-sm" id="cost">
<div class="modal-content">
<div class="col-md-12" style="width:500px;">
<div class="portlet box white">
<div class="portlet-title">
<div class="caption">
<i class="fa fa-gift"></i>Edit Cost
</div>
</div>
<div class="portlet-body form">
<!-- BEGIN FORM-->
<form action="/partA-edit-cost" class="cost_form" method="POST">
<div class="form-actions top">
<button type="submit" class="btn green submit_cost">Submit</button>
<button type="button" class="btn default">Cancel</button>
</div>
<div class="form-body">
<div class="form-group">
<label class="control-label">Type</label>
<input type="text" name="type" class="form-control type" placeholder="Enter Cost type">
</div>
<div class="form-group">
<label class="control-label">Cost</label>
<input type="text" name ="cost" class="form-control" placeholder="Enter Cost">
</div>
<div class="form-group">
<label class="control-label">Revenue</label>
<input type="text" name="revenue" class="form-control" placeholder="Enter Revenue">
</div>
<div class="form-group">
<label class="control-label">Profit</label>
<input type="text" name="profit" class="form-control" placeholder="Enter Profit">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Models\Cost;
class CostController extends Controller
{
public function editCost(Request $request)
{
$type = $request->get('type');
//$id = $request->get('id');
$cost = COST::where('id', 3);
$cost->type = "type";
$cost->save();
return \Response::json(['id' => $id,'type' => $type],200);
}
}
Disable temporarily stuff inside the editCost method will remove the error
public function editCost(Request $request) {
echo "ok";
}//returns on "ok " to the console;
I think there is any error at $cost = COST::where('id', 3);, try changing it to
$cost = Cost::where('id', 3)->first();
$cost = COST::where('id', 3); results query scope not the Model, therefore you should add first() method, if no result matches your condition it will return null which can also result in the internal error issue since, you are calling $cost->type on the next line.
I hope this helps.
In your code the variable $id is not defined in your return statement ( you have commented it out )
return \Response::json(['id' => $id,'type' => $type],200);
I think the code should look like:
$type = $request->get('type');
//$id = $request->get('id');
$cost = Cost::where('id', 3)->first();
$cost->type = "type";
$cost->save();
return \Response::json(['id' => 3,'type' => $type],200);
If that works you can use the dynamic $id and $type variables

Resources