Php laravel using post method by controller - php

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');
:)

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

Trying to get property 'serino' of non-object

I don't want to use "id" to edit the product, I want to use the "serial no" column. But I get the following error:
Trying to get property 'serino' of non-object
Product.php
public function edit($serino)
{
$product= DB::select('select * from products where serino=?',[$serino]);
return view("products/edit",compact("product"));
}
index.blade.php
<a href="{{route('products.edit',$product->serino)}}">
<button class="btn btn-info btn-sm"><i class="fas fa-edit"></i> Edit</button>
</a>
edit.blade.php
<form class="form-horizontal" action="{{route('products.update',$product->serino)}}" method="post">
{!! csrf_field() !!}
{!! method_field('put') !!}
<div class="form-group">
<div class="col-sm-12">
<input type="text" class="form-control" value="{{$product->name}}" name="name" placeholder="Product Name">
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="text" class="form-control" value="{{$product->category}}" name="category" placeholder="Product Category">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-12">
<button type="submit" class="btn btn-success">Save</button>
</div>
</div>
</form>
Good Work..
Your query will return you collection/array of result, no just one Product.
You can use sometihing like this:
$product = DB::table('products')->where('serino', $serino)->first();

Http request 500 internal server error on Axios put

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.

Post form data in laravel

I have just created a new project in larvel and I am trying to submit form in db but it is not saving data in db.
here is my form
<form id="" method="post" class="form-horizontal" action="{{ route('updateadminprofile')}}"enctype="multipart/form-data" >
#if (Session::has('success'))
<div class="alert alert-success" role="alert" style="font-size: 18px;">
<strong>Success: </strong>
{{ Session::get('success') }}
</div>
#endif
<div class="form-group">
<label class="col-sm-4 control-label" for="userName"> Name</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="name" name="name" placeholder="name}" value="name" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="userName"> Description</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="description" name="description" placeholder="Description}" value="description" />
</div>
</div>
<button style="margin-left: 30%" type="submit" class="btn btn-primary" name="signup" value="sumbmit" >Save</button>
My route is
Route::group(['namespace' => 'PrivatePages'], function () {
Route::any('/updateadminprofile', ['as' => 'updateadminprofile',
'uses' => 'ProductController#UpdateAdminProfile']);
});
here is my contoller function
public function UpdateAdminProfile(CreateProductRequest $request)
{
$saveproduct = new Product();
$saveproduct->name = $request->name;
$saveproduct->description = $request->description;
$saveproduct->save();
}
it is not saving record in db. and when i try to submit form it gives me below text,
The page has expired due to inactivity.
Please refresh and try again.
when i added csrf in form it even not going to route specified in the action of form
Try this, you are probably missing the CSRF field in your form. Also keep the Flash message out of your form.
#if (Session::has('success'))
<div class="alert alert-success" role="alert" style="font-size: 18px;">
<strong>Success: </strong>
{{ Session::get('success') }}
</div>
#endif
<form id="form" method="post" class="form-horizontal" action="/updateadminprofile">
{{ csrf_field() }}
<div class="form-group">
<label class="col-sm-4 control-label" for="name"> Name</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="name" name="name" placeholder="name" value="name" />
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="description"> Description</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="description" name="description" placeholder="Description}" value="description" />
</div>
</div>
<button style="margin-left: 30%" type="submit" class="btn btn-primary" name="signup" value="submit">Save</button>
</form>
Edit: cleaned up some minor HTML errors and specified the route directly instead of a blade function. Could you try this?
I solved the issue by just adding following namespaces in the CreateProductRequest and ProductController
In Create CreateProductRequest updated following lines.
use Illuminate\Support\Facades\Request;
class CreateProductRequest extends Request{}
In Product Controller i use following namespace
.
use App\Http\Requests\CreateProductRequest;
Thanks All for your time and help

Php laravel TokenMismatchException request

am try to send request by post form from but error TokenMismatchException
here is my controller code
public function postSaveedit(Request $request) {
$var1 = $request->input('title'); //name on the form
$var2 = $request->input('meaning'); //name on the form
$words = User::where("title", $var1)->update(array("meaning" => $var2));
return view('dict.watch', compact('words'));
}
here is view code.
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<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="title">
</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="meaning">
</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="submit">
<span></span>
</div>
</div>
</form>
thnak you for your help
From https://laravel.com/docs/master/routing:
Laravel automatically generates a CSRF "token" for each active user
session managed by the application. This token is used to verify that
the authenticated user is the one actually making the requests to the
application.
Anytime you define a HTML form in your application, you should include
a hidden CSRF token field in the form so that the CSRF protection
middleware will be able to validate the request. To generate a hidden
input field _token containing the CSRF token, you may use the
csrf_field helper function
Just add this line inside your form
<form class="form-horizontal" role="form" method="POST" action="{{ URL::to('index/saveedit') }}">
<input type="hidden" name="_token" value="{{ csrf_token(); }}">
<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="title">
</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="meaning">
</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="submit">
<span></span>
</div>
</div>
</form>

Categories