How to send multiple variable from blade to controller - php

<input type="date" class="form-control" name="from" value="{{ date('Y-m-d') }}">
<input type="date" class="form-control" name="to" value="{{ date('Y-m-d') }}">
i want to send 'from' and 'to' to controller from blade. how to write that in the tag. the necessary code is mentioned above. Thanks for your help in advance.

<form method="post" action="{{ URL("/totalsales/") }}">
<input type="datetime-local" class="form-control" name="from" >
<input type="datetime-local" class="form-control" name="to" >
<input type="submit" class = "btn btn-primary">
this is the method to send data in controller
your route should be post .

Use form:
<form method="post" action="{{ url("/totalsales/{{ }}") }}">
<input type="date" class="form-control" name="from" value="{{ date('Y-m-d') }}">
<input type="date" class="form-control" name="to" value="{{ date('Y-m-d') }}">
<input type="submit" value="Send">
</form>

Related

Laravel: Too few arguments to function 0 passed and 1 expected

I am currently working on a 1 page crud for a laravel project. I had a lot of trouble to begin with, but now the last error i have is
Too few arguments to function 0 passed and 1 expected
this happens on loading the main page of the crud. this file gives the error:
edit.blade.php
<form method="post" action="{{ route('admin.user.update', $userEdit->id) }}">
#method('PATCH')
#csrf
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" name="name" value="{{ $userEdit->name }}" />
</div>
<div class="form-group">
<label for="email">email </label>
<input type="email" class="form-control" name="email" value="{{ $userEdit->email }}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
controller index:
public function index($id)
{
$users = User::all();
$userEdit = User::find($id);
return view('admin.user.index', compact('users', 'userEdit'));
}
yes i need $users and $userEdit, but i cant reach $userEdit on the first load of the page. normally there is also no $id in index() but i added it to try to fix it, this did not work
You need to pass the parameter in as an array with a key specifying the route's parameter name. See https://laravel.com/docs/9.x/routing#generating-urls-to-named-routes
<form method="post" action="{{ route('admin.user.update', ['id' => $userEdit->id]) }}">
you have a error in your form:
<form method="post" action="{{ route('admin.user.update', $userEdit->id) }}">
#csrf
#method('PUT')
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" name="name" value="{{ $userEdit->name }}" />
</div>
<div class="form-group">
<label for="email">email </label>
<input type="email" class="form-control" name="email" value="{{ $userEdit->email }}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
method not it´s PATCH it´s PUT and firts send your CSRF

Value Old() On Laravel Blade, Seems Not To Be Working

I want to retrieve the old value input that user has entered on form, so he/she won't need to re-enter all the form inputs again.
In order to do that, I have added these:
<input type="text" class="form-control" name="uid" value="{{ (!empty($member)) ? $member->mbr_national_code : old('uid') }}">
<input type="text" class="form-control" name="mobile" value="{{ (!empty($member)) ? $member->mbr_mobile : old('mobile') }}">
<input type="text" class="form-control" name="phone" value="{{ (!empty($member)) ? $member->mbr_phone : old('phone') }}">
<input type="text" class="form-control" name="post_code" value="{{ (!empty($member)) ? $member->mbr_post_code : old('post_code') }}">
<input type="text" class="form-control" name="email" value="{{ (!empty($member)) ? $member->user->usr_email : old('email') }}">
<textarea id="" class="form-control" cols="30" rows="3" name="address" placeholder="Street, block, etc">{{ (!empty($member)) ? $member->mbr_address : old('address') }}</textarea>
So I tried retrieving old requests by saying:
old('input_name')
But it does not seem to be working, and when the form refreshes, no data added from the last request.
So how to fix this? I would really appreciate any idea or suggestion from you guys...
Thanks.
Try passing second param to old method
<input type="text" class="form-control" name="uid" value="{{old('uid',$member->mbr_national_code??null) }}">
Full code
<input type="text" class="form-control" name="uid" value="{{old('uid',$member->mbr_national_code??null) }}">
<input type="text" class="form-control" name="mobile" value="{{ old('mobile',$member->mbr_mobile??null) }}">
<input type="text" class="form-control" name="phone" value="{{ old('phone',$member->mbr_phone??null) }}">
<input type="text" class="form-control" name="post_code" value="{{ old('post_code',$member->mbr_post_code??null) }}">
<input type="text" class="form-control" name="email" value="{{old('email',$member->user->usr_email??null) }}">
<textarea id="" class="form-control" cols="30" rows="3" name="address" placeholder="Street, block, etc">{{ old('address',$member->mbr_address??null) }}</textarea>
try this,
it is much simpler
<input type="text" class="form-control" name="mobile" value="{{ old('mobile') ?? $member->mbr_mobile }}">

put method not supported in laravel

I tried to update a post using edit route but when I send the form and use the update function give me an error
my code is
<form action="/posts{{$posts->id}}" method="POST">
#method('PUT')
#csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">
You have to use like this
<form action="{{url('')}}/posts/{{$post->id}}" method="POST">
#csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">
And in your route use like this
Route::post('/posts/{id}', ...)
You are missing a / in your action
action="/posts/{{ $posts->id }}"
You could do the following :
<form action="{{ route('route.name', $post->id) }}" method="POST">
#csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}</textarea>
<input type="submit" class="btn btn-primary" value="edit">
And for the route :
Route::post('/posts/{id}', 'Controller#function')->name('route.name');
I put a hidden method that I found on laravel documents and worked fine
<form action="/posts/{{$post->id}}" method="POST">
#csrf
<label for="">title</label>
<input type="text" name="title" class="form-control" >
<label for="">body</label>
<textarea type="text" name="body" class="form-control">{{$post->body}}.
</textarea>
<input type="submit" class="btn btn-primary" value="edit">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

Laravel update not working or not routing

I made an update form in laravel project to update some entries in my database but when i press submit button nothing is happening.i don't know why.tried everything.
here is the form:
<form action="{{ route('loans.update', $loan->id)}}" method="patch">
<p class="al-left">
{{csrf_field()}}
<label for="date">Date:</label>
<input type="date" name="date" value="{{$loan->data}}" id="date">
</p>
<p class="al-left">
<label for="name">Name:</label>
<input type="text" name="name" value="{{$loan->nume}}" id="name">
</p>
<p class="al-left">
<label for="period">Period(months):</label>
<input type="number" name="period" value="{{$loan->durata}}" id="period">
</p>
<p class="al-left">
<label for="month">Month Rate(euro):</label>
<input type="number" name="month" value="{{$loan->valoare_rata_luna}}" id="month">
</p>
<p class="al-left">
<label for="amount">Amount:</label>
<input type="number" name="amount" value="{{$loan->valoare_totala}}" id="amount">
</p>
<input type="submit" class="btn btn-sm btn-primary" name="submit" value="Edit">
<br>
<br>
</form>
here is the delete function in controller:
public function update(Request $request, $id){
$id = Auth::id();
$loan =loan::find($id);
$loan->cod_user=$id;
$loan->nume = $request->name;
$loan->data=$request->date;
$loan->durata=$request->period;
$loan->valoare_rata_luna=$request->month;
$loan->valoare_totala=$request->amount;
$loan->save();
return view("loans")->with('loans', $loans);
}
the routes:
Route::resource('/finance/loans','loanController');
To say that form action is in loans.blade.php
Form only supports GET and POST method. You have spoof the 'PATCH` method like:
<form action="{{ route('loans.update', $loan->id)}}" method="POST"> //**Here method = POST
<p class="al-left">
{{ csrf_field() }}
{{ method_field('PATCH') }} //**Here give PATCH by spoofing method
<label for="date">Date:</label>
<input type="date" name="date" value="{{$loan->data}}" id="date">
</p>
..
..
..
</form>
Hope it's helpful.
Route web.php
Route::resource('loans', 'LoansController');
<form action="{{ route('loans.update', $loan->id)}}" method="POST">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<input ... />
<input ... />
<input ... />
<input ... />
<input ... />
</form>

How to make two rows insert with one button Laravel using ajax

i want to make two inserts in same table.
The table is based on this fields (locale, project_id(foreign key), title, caption).
And the controller looks like here:
public function storeTranslation(Request $request)
{
$projecttranslation = new ProjectTranslation();
$projecttranslation->locale = $request->input("locale");
$projecttranslation->project_id = $request->input("project");
$projecttranslation->title = $request->input("title");
$projecttranslation->caption = $request->input("caption");
$projecttranslation->save();
}
The form for the moment looks like here:
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="locale" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
<form enctype="multipart/form-data" id="myFormTraduccion2" name="myFormTraduccion2"> <!--SECOND FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="title">Language:</label>
<input type="text" id="locale" name="locale" value="es" disabled class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit2" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!--SECOND FORM TO TRANSLATE END HERE -->
</div>
</div>
And the ajax look like this:
$("#createtranslatesubmit").click(function(){
$("#myFormTraduccion").submit();
});
$("#myFormTraduccion").submit(function(e){
e.preventDefault();
$.ajax({
url:'/admin/projects/postUploadTranslation',
type:'POST',
data:$('#myFormTraduccion').serializeArray(),
success: function(){
$("#form2").fadeOut(1000);
$("#form3").fadeIn(2000);
}
});
});
This create only with the first form, the first translation.
I think i should change the view code to this (Two same inputs for each field of database):
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="locale" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale" name="locale" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="project" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="title" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="caption" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
</div>
</div>
That's correct? The problem to "store" the data, i think will be a foreach in controller.
And finally, i don't have any idea, how to pass the data in the ajax, with a formdata maybe?
Thanks a lot, any help will be appreciated!
When you submit a form, you send to the server the data of that specific form. Your approach of using multiple forms doesn't work here, because you want to send all data with only 1 specific form submit.
So you have to only create 1 form and separate the different translations with a numeric reference.
Your HTML (note the -0 and -1 used to separate id and name of each input element) :
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale-0" name="locale-0" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project-0" name="project-0" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title-0" name="title-0" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption-0" name="caption-0" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale-1" name="locale-1" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project-1" name="project-1" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title-1" name="title-1" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption-1" name="caption-1" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form>
The controller:
public function storeTranslation(Request $request)
{
$projecttranslation0 = new ProjectTranslation();
$projecttranslation0->locale = $request->input("locale-0");
$projecttranslation0->project_id = $request->input("project-0");
$projecttranslation0->title = $request->input("title-0");
$projecttranslation0->caption = $request->input("caption-0");
$projecttranslation0->save();
$projecttranslation1 = new ProjectTranslation();
$projecttranslation1->locale = $request->input("locale-1");
$projecttranslation1->project_id = $request->input("project-1");
$projecttranslation1->title = $request->input("title-1");
$projecttranslation1->caption = $request->input("caption-1");
$projecttranslation1->save();
}
Of course, it can be easily generalized for N multiple transations and not only 2.
try this out:
<div id="form2" style="display:none;" class="col-md-6">
<div class="col-md-">
<h3>Crear nueva traduccion</h3>
<form enctype="multipart/form-data" id="myFormTraduccion" name="myFormTraduccion"><!--FIRST FORM TO TRANSLATE -->
<input type="hidden" name="_token" value="{{ Session::token() }}">
<div class="form-group">
<label name="Language">Language:</label>
<input type="text" id="locale" name="ProjectTranslation[0][locale]" value="en" class="form-control form-control-sm">
<label name="Project">Project id:</label>
<input type="number" id="project" name="ProjectTranslation[0][project]" class="form-control form-control-sm">
<label name="Title">Title:</label>
<input type="text" id="title" name="ProjectTranslation[0][title]" class="form-control form-control-sm">
<label name="Caption">Caption:</label>
<input type="text" id="caption" name="ProjectTranslation[0][caption]" class="form-control form-control-sm">
<label name="title">Language:</label>
<input type="text" id="locale" name="ProjectTranslation[1][locale]" value="es" class="form-control form-control-sm">
<label name="order">Project id:</label>
<input type="number" id="project" name="ProjectTranslation[1][project]" class="form-control form-control-sm">
<label name="public">Title:</label>
<input type="text" id="title" name="ProjectTranslation[1][title]" class="form-control form-control-sm">
<label name="caption">Caption:</label>
<input type="text" id="caption" name="ProjectTranslation[1][caption]" class="form-control form-control-sm"><br>
<input type="submit" value="Crear Traduccion" id="createtranslatesubmit" class="btn btn-danger btn-md">
<br><br><br>
</div>
</form> <!-- FIRST FORM TO TRANSLATE END HERE -->
</div>
So you will get array of ProjectTranslation at controller side
Now at controller side
public function storeTranslation(Request $request)
{
$form_data = $request->get('ProjectTranslation');
foreach($form_data as $form){
$projecttranslation = ProjectTranslation::create($form);
$projecttranslation->save();
}
}

Categories