Http request 500 internal server error on Axios put - php

I have an issue submitting my updated data to database.
It accepts all input but when I'm trying to get it saved it gets the error.
This is my route:
Route::resource('Sales', 'SalesController');
How I put my data:
axios.put('/Sales/' + id)
My update method:
public function update(Request $req, $id){
$sales = Sales::find($id);
$sales = new Sales;
$sales->itemQty = $req['itemQty'];
$sales->itemID = $req['itemID'];
$sales->save();
}
My Editing form:
<div class="card-body" v-for="(sale, index) in sales" :key="sale.id">
<form action="/Sales" method="post">
<div>
<input type="hidden" name="_token" :value="csrf">
<div class="form-group">
<div class="form-inline">
<div class="mr-3 mt-2">Item ID: </div>
<input class="form-control mt-2 col" name="itemID[]" maxlength="12" v-model="sale.itemID">
</div>
<div class="form-inline">
<div class="mr-2 mt-2">Quantity: </div>
<input type="number" min="1" class="form-control mt-2 col" placeholder="Quantity" name="itemQty[]" v-model="sale.itemQty">
</div>
</div>
</div>
<div class="updatesale" style="margin:0 auto; text-align: center;">
<button class="btn btn-success mt-2" type="button" name="button" v-on:click="updateSalesRec(sale.id)">Update</button>
</div>
</form>
</div>
What is wrong with it?

You don't need to create another instance of the Sales object after finding it:
public function update(Request $req, $id){
$sales = Sales::find($id);
$sales->itemQty = $req['itemQty'];
$sales->itemID = $req['itemID'];
$sales->save();
}
I don't think your intention is to have your data submitted in array. So remove [] from the input field names:
<div class="card-body" v-for="(sale, index) in sales" :key="sale.id">
<form action="/Sales" method="post">
<div>
<input type="hidden" name="_token" :value="csrf">
<div class="form-group">
<div class="form-inline">
<div class="mr-3 mt-2">Item ID: </div>
<input class="form-control mt-2 col" name="itemID" maxlength="12" v-model="sale.itemID">
</div>
<div class="form-inline">
<div class="mr-2 mt-2">Quantity: </div>
<input type="number" min="1" class="form-control mt-2 col" placeholder="Quantity" name="itemQty" v-model="sale.itemQty">
</div>
</div>
</div>
<div class="updatesale" style="margin:0 auto; text-align: center;">
<button class="btn btn-success mt-2" type="button" name="button" v-on:click="updateSalesRec(sale.id)">Update</button>
</div>
</form>
</div>
Hope this is useful.

Related

I can't get data using id

I can't get data with id it returns this: %7Bid%7D.
I want to get the id and edit the data.
Controller:
public function edit($id){
$slider = DB::table('header_sliders')->find($id);
return view('posts.edit',['header'=>$slider]);
}
View:
<div class="container">
<div class="row">
<div class="col-md-12">
<form action="{{url('admin/edit')}}" method="POST" >
{{csrf_field()}
<div class="form-group">
<label for="exampleInputEmail1">Mətn</label>
<input type="text" name="text" class="form-control" aria-describedby="emailHelp" value="{{$header->text}}">
<small id="emailHelp" class="form-text text-muted"></small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Şəkil</label>
<input type="file" name="imgName" class="form-control" value="{{$header->imgName}}">
</div>
<div class="form-check">
</div>
<button type="submit" class="btn btn-primary">Dəyiş</button>
<a href="{{url('admin/edit/{id}')}}" class="edit" data-toggle="modal"><i
class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
Create a Route like this
Route::get('/admin/edit/{id}','UserController#edit');
and pass the id with your URL like this
YourURL..../admin/edit/11
This will solve your problem

One form is working while other form is not working in laravel 5.4

I've UserController in which I've two options -
1) For Updating Profile
2) For Updating Password
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\User;
use Auth;
use Hash;
class UserController extends Controller
{
public function profile(){
return view('profile', array('user' => Auth::user()));
}
public function update_avatar(Request $request){
if(isset($request->avatar) && $request->avatar->getClientOriginalName()){
$ext = $request->avatar->getClientOriginalExtension();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->avatar->storeAs('public/avatar',$file);
}
else
{
$user = Auth::user();
if(!$user->avatar)
$file = '';
else
$file = $user->avatar;
}
$user = Auth::user();
$user->avatar = $file;
$user->name = $request->name;
$user->email = $request->email;
$user->mb_number = $request->mb_number;
$user->home_town = $request->home_town;
$user->save();
return view('profile', array('user' => Auth::user()));
}
public function update_password(Request $request){
$user = Auth::user();
if(Hash::check(Input::get('old_password'), $user['password']) && Input::get('new_password') == Input::get('confirm_new_password')){
$user->password = bcrypt(Input::get('new_password'));
$user->save();
}
return view('profile', array('user' => Auth::user()));
}
}
In my view blade, I've two forms -
update_avatar for updating profile like name, phone number and avatar.
update_password for updating password.
</div>
<div class="widget-user-image">
<img class="img-circle" src="{{ asset('public/storage/avatar/'.$user->avatar) }}" alt="User Avatar">
</div>
<div class="box-footer">
<div class="row">
<div class="col-sm-4 border-right">
<div class="description-block">
<h5 class="description-header">{{ $user->email }}</h5>
<span class="description-text">Email</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
<div class="col-sm-4 border-right">
<div class="description-block">
<h5 class="description-header">{{ $user->name }}</h5>
<span class="description-text">{{ $user->home_town }}</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
<div class="col-sm-4">
<div class="description-block">
<h5 class="description-header">{{ $user->mb_number }}</h5>
<span class="description-text">Phone No.</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</div>
<!--
<div class="box-footer no-padding">
<ul class="nav nav-stacked">
<li>Projects <span class="pull-right badge bg-blue">31</span></li>
<li>Tasks <span class="pull-right badge bg-aqua">5</span></li>
<li>Completed Projects <span class="pull-right badge bg-green">12</span></li>
<li>Followers <span class="pull-right badge bg-red">842</span></li>
</ul>
</div>
-->
</div>
</div>
<section class="content">
<div class="container-fluid">
<form action="/profile" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Title" value="{{$user->name}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" id="email" placeholder="Description" value="{{$user->email}}" readonly>
</div>
<div class="form-group">
<label for="mb_number">Mobile No.</label>
<input type="text" name="mb_number" class="form-control" id="mb_number" placeholder="Schedule" value="{{$user->mb_number}}">
</div>
<div class="form-group">
<label for="home_town">Home Town</label>
<input type="text" name="home_town" class="form-control" id="home_town" placeholder="Deadline" value="{{$user->home_town}}">
</div>
<div class="form-group">
<label>Update Profile Image</label>
<input type="file" name="avatar">
#if($user->avatar)
<img src="{{ asset('public/storage/avatar/'.$user->avatar) }}" style="width:150px;">
#endif
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}"
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</section>
<section class="content">
<div class="container-fluid">
<form action="/profile" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="old_password">Old Password</label>
<input type="password" name="old_password" class="form-control" id="old_password" placeholder="Old Password">
</div>
<div class="form-group">
<label for="new_password">New Password</label>
<input type="password" name="new_password" class="form-control" id="new_password" placeholder="New Password">
</div>
<div class="form-group">
<label for="confirm_new_password">Confirm New Password </label>
<input type="password" name="confirm_new_password" class="form-control" id="confirm_new_password" placeholder="Confirm New Password">
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}"
<button type="submit" class="btn btn-primary">Update Password</button>
</div>
</div>
</section>
update_password function is working fine but update_avatar function is not working neither it's showing any error. I've tried dd($user) but still not giving output to dd.
Check this out i updated form a bit with indentation and proper closing, also please redirect profile avatar to different URL so that it can access different method :-
I would suggest you to use https://laravelcollective.com/docs/5.4/html , by using form collective it would be much more easier to implement more complex form structure.
In Route,
Route::post('/profile_avatar',"UserController#update_avatar");
Route::post('/profile_password',"UserController#update_password");
In Blade,
<section class="content">
<div class="container-fluid">
<form action="/profile_avatar" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="name">
Name
</label>
<input class="form-control" id="name" name="name" placeholder="Title" type="text" value="{{$user->name}}">
</input>
</div>
<div class="form-group">
<label for="email">
Email
</label>
<input class="form-control" id="email" name="email" placeholder="Description" readonly="" type="text" value="{{$user->email}}">
</input>
</div>
<div class="form-group">
<label for="mb_number">
Mobile No.
</label>
<input class="form-control" id="mb_number" name="mb_number" placeholder="Schedule" type="text" value="{{$user->mb_number}}">
</input>
</div>
<div class="form-group">
<label for="home_town">
Home Town
</label>
<input class="form-control" id="home_town" name="home_town" placeholder="Deadline" type="text" value="{{$user->home_town}}">
</input>
</div>
<div class="form-group">
<label>
Update Profile Image
</label>
<input name="avatar" type="file">
#if($user->avatar)
<img src="{{ asset('public/storage/avatar/'.$user->avatar) }}" style="width:150px;">
#endif
</img>
</input>
</div>
<input <a="" class="btn btn-info" href="" name="_token" type="submit" value="{{ csrf_token() }}"/>
</div>
<button class="btn btn-primary" type="submit">
Update
</button>
</form>
</div>
</section>
<section class="content">
<div class="container-fluid">
<form action="/profile_password" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="old_password">
Old Password
</label>
<input class="form-control" id="old_password" name="old_password" placeholder="Old Password" type="password">
</input>
</div>
<div class="form-group">
<label for="new_password">
New Password
</label>
<input class="form-control" id="new_password" name="new_password" placeholder="New Password" type="password">
</input>
</div>
<div class="form-group">
<label for="confirm_new_password">
Confirm New Password
</label>
<input class="form-control" id="confirm_new_password" name="confirm_new_password" placeholder="Confirm New Password" type="password">
</input>
</div>
<input <a="" class="btn btn-info" href="" name="_token" type="submit" value="{{ csrf_token() }}"/>
</div>
<button class="btn btn-primary" type="submit">
Update Password
</button>
</form>
</div>
</section>

$_POST method turns empty

Firstable, hello everyone. I have issue about the $_POST method, when I write this code block;
if (isset($_POST['menuduzenle'])) {
$menu_id=$_POST['menu_id']; //It didn't turn with value in mysql
$ayarguncelle=mysql_query("update menuler set menu_ad='".$_POST['menu_ad']."',menu_link='".$_POST['menu_link']."'where menu_id='$menu_id'");
if(mysql_affected_rows())
{
header("Location:../menuleriduzenle.php?guncelleme=basarili&menu_id=$menu_id");
}else{
header("Location:../menuleriduzenle.php?guncelleme=basarisiz&menu_id=$menu_id");}
}
menu_id turns emty, what I need to do for solve this?
and here it's html code
form
<form action="network/islem.php" method="POST">
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Adı: </label>
<input class="form-control" name="menu_ad" value="<?php echo $menucek['menu_ad']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Link: </label>
<input class="form-control" name="menu_link" value="<?php echo $menucek['menu_link']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-6">
<input class="btn btn-primary col-md-3" type="submit" name="menuduzenle" value="Edit Menu">
</div>
</form>
and
<td><button class="btn btn-primary col-md-12">Edit</button></td>
You not send any one input with name 'menu_id' on your from.
<form action="network/islem.php" method="POST">
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Adı: </label>
<input class="form-control" name="menu_ad" value="<?php echo $menucek['menu_ad']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-9">
<label>Menu Link: </label>
<input class="form-control" name="menu_link" value="<?php echo $menucek['menu_link']; ?>" />
</div>
</div>
<div class="col-md-12">
<div class="form-group col-md-6">
<input class="btn btn-primary col-md-3" type="submit" name="menuduzenle" value="Edit Menu">
</div>
<input type="hidden" name="menu_id" value="<?php echo $_GET['menu_id'] ?>" />
</form>
You input name "menu_ad" would be "menu_id" or fault other input with name "menu_id" on you sample.

Clear data in modal when clicking close in Laravel

I have a modal (Bootstrap) for creating a new product. When I click the "close" button, which creates a new product, the old data still does not clear from the modal.
My code:
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal" data-backdrop="static" data-keyboard="false" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create new product</h4>
</div>
<div class="modal-body">
<form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
{{ csrf_field() }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" id="" name="product_name">
</div>
<div class="form-group">
<label for="">Product Type</label>
<input type="text" class="form-control" id="" name="product_type_id">
</div>
<div class="form-group">
<label for="">Price</label>
<input type="text" class="form-control" id="" name="price">
</div>
<div class="form-group">
<label for="">Status</label>
<select class="form-control input-sm m-bot15" id="" name="status">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
</div>
<div class="form-group">
<label for="img">Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Choose <input type="file" id="imgInp">
</span>
</span>
<input type="text" class="form-control" name="product_image" readonly>
</div>
<img id='img-upload' class="image_responsive" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" id="" name="description"></textarea>
</div>
<div class="form-group">
<label for="">Note</label>
<textarea class="form-control" id="" name="addition_information"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Save" class="btn btn-success">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
How can I clear the data when closing the modal?
You can add event listener to your close button by adding an id to your close button for example id=close-btn:
document.getElementById("close-btn").addEventListener("click", function(){
document.getElementById("frmProduct").reset();
});
You can use hidden.bs.modal event:
This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).
The snippet:
$('#product').on('hidden.bs.modal', function(e) {
$(this).find('form').trigger('reset');
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#product">
Launch modal
</button>
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal" data-backdrop="static" data-keyboard="false" >
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Create new product</h4>
</div>
<div class="modal-body">
<form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
{{ csrf_field() }}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="">Product Name</label>
<input type="text" class="form-control" id="" name="product_name">
</div>
<div class="form-group">
<label for="">Product Type</label>
<input type="text" class="form-control" id="" name="product_type_id">
</div>
<div class="form-group">
<label for="">Price</label>
<input type="text" class="form-control" id="" name="price">
</div>
<div class="form-group">
<label for="">Status</label>
<select class="form-control input-sm m-bot15" id="" name="status">
<option value="0">Inactive</option>
<option value="1">Active</option>
</select>
</div>
<div class="form-group">
<label for="img">Image</label>
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-default btn-file">
Choose <input type="file" id="imgInp">
</span>
</span>
<input type="text" class="form-control" name="product_image" readonly>
</div>
<img id='img-upload' class="image_responsive" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="">Description</label>
<textarea class="form-control" id="" name="description"></textarea>
</div>
<div class="form-group">
<label for="">Note</label>
<textarea class="form-control" id="" name="addition_information"></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<input type="submit" value="Save" class="btn btn-success">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>

Php laravel using post method by controller

am try to send value form from by post method to controller
here is my view,and how can i use post method to send
<form class="form-horizontal" role="form">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" value="Save Changes" type="button">
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
here is controller method like
public function postSaveedit($meaning){
}
using route by controller
You should read up on Requests in Laravel: https://laravel.com/docs/5.2/requests#accessing-the-request
You need to pass that to your controller
public function postSaveedit(Request $request) {
$input = $request->input();
$foo = $input['foo'];
$bar = $input['bar'];
$baz = $input['baz'];
}
In Laravel 5.2 you can use request() helper method to solve your problem...
This is how you can do it...
Routes file should look like this (be sure that this route should be of post type)
Route::post('/myurl', 'Controllername#postSaveEdit')->name('postSaveEdit');
Form file should look like this, also please specify the input field names in the form so that you can grab them in the the controller by their specified names (like - title, meaning - see below code)...
<form class="form-horizontal" action="{{ route('postSaveEdit') }}" method="POST" role="form">
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" name="title" value='{{ $words->first()->title }}' type="text">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" name="meaning" value="{{ $words->first()->meaning }}" type="text">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<button class="btn btn-primary" type="submit">Save Changes</button>
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
and controller should be like this...
public function postSaveEdit() {
// The inputs variable contains all your form's inputs in the form of array...
$inputs = request()->all();
/*
$inputs = array(
'title' => 'title_value',
'meaning' => 'meaning_value'
)
*/
// Wheareas you can also get them by using 'get' method on request method like this
$title = request()->get('title');
$meaning = request()->get('meaning');
}
here u go
Form
you have to add method to your form + names to your inputs
<form class="form-horizontal" role="form" method="POST">
<!-- Add csrf token -->
{!! csrf_field() !!}
<div class="form-group">
<label class="col-lg-3 control-label">Title:</label>
<div class="col-lg-8">
<input class="form-control" value='{{ $words->first()->title }}' type="text" name="input1">
</div>
</div>
<div class="form-group">
<label class="col-lg-3 control-label">Meaning:</label>
<div class="col-lg-8">
<input class="form-control" value="{{ $words->first()->meaning }}" type="text" name="input2">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-8">
<input class="btn btn-primary" type="submit" value="Save Changes"/>
<span></span>
<input class="btn btn-default" value="Cancel" type="reset">
</div>
</div>
</form>
controller
Use Word; // at the top of the class
public function postSaveedit(Request $request) {
$word= new Word; // if you are creating a new record
$word= Word::find(1);// if you are updating a record
$word->title = $request->input('input1');
$word->meaning= $request->input('input2');
$word->save();
return view('home.blade.php');
}
Routes file
Route::get('/myurl', 'Controllername#postSaveedit');
:)

Categories