Laravel Error: Missing required parameters - php

Please help me out. I'm having the missing required parameter's error. This is the form script:
<div class="container">
<div class="row">
<!--first column-->
<div class="col-md-6 col-sm-6 ">
<!-- Default form contact -->
<form class="text-center border border-light p-5" action="{{route('updating', $update->id)}}" enctype="multipart/form-data" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}" >
#method('PATCH')
<div class="well">
<p class="h4 mb-4">Edit Updates</p>
</div>
<!-- Name -->
<input type="text" name="title" id="defaultContactFormName" class="form-control mb-2" value=" {{ old('title') ?? $update->title }} ">
<!-- Message -->
<div class="form-group">
<textarea class="form-control rounded-0" name="body" id="exampleFormControlTextarea2" rows="5" value="{{ old('title') ?? $update->body }}"></textarea>
</div>
<!-- Send button -->
<button class="btn btn-success btn-block" type="submit">Publish</button>
</form>
</div>
</div>
</div>
And this is my controller and edit function:
public function update(Request $request, $title)
{
$this->validate($request, [
'title'=>'required',
'body'=>'required'
]);
//Publishing Updates
$update = Update::findOrFail($title);
$update->title = $request->input('title');
$update->body = $request->input('body');
$update->save();
return redirect('admin.updates')->with('success', 'Saved');
}
And this is my route:
Route::patch('/updates/{title}/update', 'UpdatesController#update')->name('updating');
I keep getting the error and i don't know where its coming from.

Related

Laravel 8 safe (think problem datetime-local input field to mysql)

When I click save on my form, the page reloads. Which is correct until then. Unfortunately, it does not save the data for me in the database. So I tried to find the error with dd($request->all()). Unfortunately, I haven't found a real error as far as the correct data is loaded on the form. However, they never end up in the database. I currently only have one guess that it is due to the wrong format of DateTime_local input. But can't say for sure.
I don't get any error messages or anything like that.
Here is a small snippet of my code:
app/Http/Controllers/TasksController
public function store(Request $request)
{
// validate all required Data
$request->validate([
'title' => 'required',
'user' => 'required',
'labels' => 'required',
'work' => 'required',
'start_work' => 'required',
'problems' => 'required',
'stop_work' => 'required',
]);
$input = $request->all();
Task::create($input);
return back()->with('success', 'Successfully saved.');
}
app/Models/Task.php
class Task extends Model
{
use HasFactory;
public $fillable = ['title', 'user', 'labels', 'work', 'start_work', 'problems', 'stop_work'];
}
Migration
public function up()
{
Schema::create('task', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('user');
$table->string('label');
$table->string('work');
$table->string('problems');
$table->dateTime('start_work');
$table->dateTime('stop_work');
$table->timestamps();
});
}
View snippet
#extends('layouts.app')
#section('content')
<!-- Success message -->
#if(Session::has('success'))
<div class="alert alert-success">
{{Session::get('success')}}
</div>
#endif
<form method="post" action="{{ route('screate') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<section class="person">
<div class="container-fluid">
<div class="card card-default">
<div class="card-header">
<h3 class="card-title">Daten</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="title">title</label>
<input type="text" class="form-control {{ $errors->has('title') ? 'error' : '' }}" id="title" name="title" required>
</div>
<!-- /.form-group -->
</div>
<div class="col-md-4">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control {{ $errors->has('username') ? 'error' : '' }}" id="username" name="username">
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="labels">Labels</label>
<select id="labels" name="label" class="select2 select2-hidden-accessible {{ $errors->has('label') ? 'error' : '' }}" multiple="" name="label[]" style="width: 100%;" data-select2-id="5" tabindex="-1" aria-hidden="true">
#foreach($labels as $label)
<option value="{{ $label->id }}">{{ $label->name }}</option>
#endforeach
</select>
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="work">work</label>
<input type="text" class="form-control {{ $errors->has('work') ? 'error' : '' }}" id="work" name="work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="start_work">Start-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="start_work" name="start_work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="problems">Problems</label>
<input type="text" class="form-control {{ $errors->has('problems') ? 'error' : '' }}" id="problems" name="problems">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="stop_work">Stop-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="stop_work" name="stop_work">
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div><!-- /.container-fluid -->
</section>
<section class="options">
<div class="row">
<div class="col-md-2 centered">
<button class="btn btn-danger" href="{{ route('home') }}">
<i class="fas fa-times">
</i>
Abbrechen
</button>
</div>
<div class="col-md-2">
<button class="btn btn-success" type="submit" name="submit">
<i class="fas fa-plus">
</i>
Speichern
</button>
</div>
</div>
</section>
</form>
#endsection
I hope you can help me with this small problem.
edit:
Here is my dd output:
dd() output

Laravel - Refresh only a section instead of refresh the whole page on submit form

I create a contact form on the home page of the website.
This is the code:
<section id="contact" class="light-grey-background m-top-30">
<div class="container">
<header>
<h2 class="title-contact text-center">
<b>CONTATTACI</b>
</h2>
</header>
#if (count($errors) > 0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">x</button>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form id="formEmail" class="m-top-40" method="post" action="{{ url('/send') }}">
{{ csrf_field() }}
<div class="form-row">
<div class="col-sm-12">
<div class="form-group">
<input id="oggetto" name="oggetto" type="text" placeholder="Oggetto" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input id="nome" name="nome" type="text" placeholder="Nome" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input id="cognome" name="cognome" type="text" placeholder="Cognome" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input id="email" name="email" type="email" placeholder="Email" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<textarea id="messaggio" rows="6" placeholder="Scrivi qui il tuo messaggio..." name="messaggio" class="form-control"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="send" class="btn btn-primary contact m-bottom-70 text-center" value="Send" />
</div>
</div>
</div>
</form>
</div>
</section>
This is the HomeController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
function index()
{
return view('home');
}
function send(Request $request)
{
$this->validate($request, [
'oggetto' => 'required',
'nome' => 'required',
'cognome' => 'required',
'email' => 'required|email',
'messaggio' => 'required'
]);
}
}
And this is the route for the home page:
Route::get('/', 'HomeController#index');
Route::post('/send', 'HomeController#send');
The problem is that when you click on the send button the page is completely updated and to know the status of the form you have to scroll the page. Is there a way to avoid the problem and update only the section that contains this?

Upload photo in project and db - Laravel

I need some help with upload. I want to insert a product into the db. The product has 3 pictures. I want the picture to be uploaded into a specific folder in the project, and the path to be entered into the db.
The folder where I want to upload the photos is: /public/css/img
My Db looks like this: I will put an example added manually in db.
id | title |price|category_id| images1 | images2| | images3| etc.
1 |Sofa |324.0 5 |/css/img/1.jpg |/css/img/2.jpg |/css/img/3.jpg
This is my view addProductModal.blade.php -> Is a modal with form.
<div class="modal fade" id="modalFormaddproduct" role="dialog">
<div class="modal-dialog" id="route">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Inchide</span>
</button>
<h4 class="modal-title" id="myModalLabel">Adauga Subcategorie</h4>
</div>
<!-- Modal Body -->
<div class="modal-body" style="text-align: center;">
<p class="statusMsg"></p>
<form role="form" action="{{route('addproduct')}}" method="post">
{{csrf_field()}}
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Nume</label>
<input type="text" class="form-control text-center" name="name" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Pret</label>
<input type="text" class="form-control text-center" name="price" placeholder="">
</div>
</div>
</div>
<!-- /.row -->
<div class="col-sm-6">
<div class="form-group">
<label>Subcategoria:</label>
<select style="text-align-last:center" class="form-control text-center" name="category_id">
#foreach($categories as $category)
#foreach($category->subcategories as $subcategory)
<option value="{{$subcategory->id}}">{{$subcategory->category}}</option>
#endforeach
#endforeach
</select>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Descriere</label>
<input type="text" class="form-control text-center" name="description" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Marime</label>
<input type="text" class="form-control text-center" name="size" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Material</label>
<input type="text" class="form-control text-center" name="material" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Cantitate</label>
<input type="text" class="form-control text-center" name="quantity" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Recomandat:</label>
<select style="text-align-last:center" class="form-control text-center" name="hot">
<option value="0">Nerecomandat</option>
<option value="1">Recomandat</option>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 1:</label>
<input type="file" name="file1" id="file1">
<input type="submit" value="Upload1" name="submit1">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 2:</label>
<input type="file" name="file2" id="file2">
<input type="submit" value="Upload2" name="submit2">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 3:</label>
<input type="file" name="file3" id="file3">
<input type="submit" value="Upload3" name="submit3">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" style="background: gainsboro; border-radius: 8px" class="btn btn-default" data-dismiss="modal">Inchide</button>
<button type="submit" style="background: #10D47D; border-radius: 8px" class="btn btn-primary">Adauga</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Route is Route::post('/products/add', 'AdminController#addproduct')->name('addproduct');
Controller: AdminController.php
public function addproduct(Request $request)
{
$product = new Product();
$product->title = $request->name;
$product->price = $request->price;
$product->category_id = $request->category_id;
$product->description = $request->description;
$product->size = $request->size;
$product->material = $request->material;
$product->quantity = $request->quantity;
$product->hot = $request->hot;
$product->images1 = $request->file1;
$product->images2 = $request->file2;
$product->images3 = $request->file3;
if (Input::hasFile('file1','file2','file3')) {
echo 'Uploaded';
$file = Input::file('file1','file2','file3');
$file->move('uploads', $file->getClientOriginalName());
echo '';
}
$product->save();
return redirect(route('adminproducts'))->with('success', 'The Product was added');
}
You should add into your form:
enctype="multipart/form-data"
to make sure you can upload a file like so:
<form role="form" action="{{route('addproduct')}}" method="post" enctype="multipart/form-data">
I do not think there is any reason not to use some maintained library but write all the functionality yourself. I would recommend you to take a look on: https://github.com/spatie/laravel-medialibrary
I found the solution.
public function addproduct(Request $request)
{
$product = new Product();
$product->title = $request->name;
$product->price = $request->price;
$product->category_id = $request->category_id;
$product->description = $request->description;
$product->size = $request->size;
$product->material = $request->material;
$product->quantity = $request->quantity;
$product->hot = $request->hot;
$file1 = Input::file('file1');
$file2 = Input::file('file2');
$file3 = Input::file('file3');
$filename1 = $file1->getClientOriginalName();
$filename2 = $file2->getClientOriginalName();
$filename3 = $file3->getClientOriginalName();
$file1 = $file1->move(public_path().'/img', $filename1);
$file2 = $file2->move(public_path().'/img', $filename2);
$file3 = $file3->move(public_path().'/img', $filename3);
$product->images1 = '/img/'.$filename1;
$product->images2 = '/img/'.$filename2;
$product->images3 = '/img/'.$filename3;
$product->save();
return redirect(route('adminproducts'))->with('success', 'Produsul a fost creat');
}

How to update a database table in laravel 5.2

i created one project in laravel 5.2. In that "myform.blade.php" page , created form for register the user. after registration it will show the current user in table format("index.blade.php"). There i given two dynamic button like the drop inside the table. One for Edit and other for Edit/View. When i click on delete button it will delete the corresponding row from the database by taken Primary key(employeeID) as reference id, it's working properly. if i click on Edit/View button it will redirect to "edit.blade.php". There i created same form as in myform.blade.php. If we want to edit the details we can edit from there. I can able to fetch the data from database to the form that i created in the "edit.blade.php". But i don't know how to update the data from their without inserting the same data again(It is not possible, because it will create Integrity constraint violation by trying to insert duplicate primary key. Can any one please tell me how to do updation. Replies are appreciable.
"myform.blade.php" is
#extends('app')
#section('content')
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">Employee Form</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">Employee Form</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('myform/myform/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-4 control-label">Employee ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="employeeID" value="{{ old('employeeID') }}" placeholder="Enter employee ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">E_number</label>
<div class="col-md-6">
<input type="text" class="form-control" name="employeeNo" value="{{ old('employeeNo') }}" placeholder="Enter employee number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="Cname" value="{{ old('Cname') }}" placeholder="Enter Contact Name">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">DOB</label>
<div class="col-md-6">
<input type="date" class="form-control" name="dob" value="{{ old('dob') }}" placeholder="Enter date of birth">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Phone</label>
<div class="col-md-6">
<input type="text" class="form-control" name="phoneNumber" value="{{ old('phoneNumber') }}" placeholder="Enter Mobile Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Address</label>
<div class="col-md-6">
<input type="text" class="form-control" name="address" value="{{ old('address') }}" placeholder="Enter Address">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning">
Save
</button>
</div>
</div>
view Data
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
"index.blade.php" is
#extends('app')
#section('content')
<div class="templatemo-content-wrapper" xmlns="http://www.w3.org/1999/html">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">user information</li>
</ol>
<div class="templatemo-content">
<h1>View/Edit user information</h1>
<div>
<div>
<div>
<table id="example" class="table table-striped table-hover table-bordered" bgcolor="#fff8dc">
<thead>
<tr>
<th>Employee ID</th>
<th>Employee No</th>
<th>Contact Name</th>
<th>Date of birth</th>
<th>Mobile number</th>
<th>address</th>
</tr>
</thead>
<tbody>
{{--{{ UserController::getIndex() }}--}}
#foreach($employer as $emp)
<tr>
<td>{{ $emp->employeeID }}</td>
<td>{{ $emp->employeeNo }}</td>
<td>{{ $emp->Cname }}</td>
<td>{{ $emp->dob }}</td>
<td>{{ $emp->phoneNumber }}</td>
<td>{{ $emp->address }}</td>
<td>
{{--#if ( in_array($nam->isActive, array('Yes','No')) )--}}
<div class="btn-group">
<button type="button" class="btn btn-info">Action</button>
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" role="menu">
{{--#if ($nam->isActive == 'Yes')--}}
<li data-toggle="modal" data-target="#acceptModal" data-bookingid="{{ $emp->employeeID }}">View/ Edit
</li>
{{--#endif--}}
<li>Delete</li>
</ul>
</div>
{{--#endif--}}
</td>
</tr>
#endforeach
</tbody>
</table>
{{$employer->links()}}
</div>
</div>
</div>
</div>
</div>
{{-- <input type="submit" id="add" name="add" value="Edit" class="button">--}}
</br>
<h4>Create a new Employee</h4>
{{--<form class="templatemo-preferences-form" role="form" method="POST" action="{{ action('UserController#save') }}">--}}
{{--<input type="hidden" name="_token" value="{{ csrf_token() }}">--}}
<form role="form" method="POST" action="{{ url('myform/index') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="row">
<div class="col-md-6 margin-bottom-15">
<input type="text" class="form-control" name="employeeID" value="{{ old('employeeID') }}" placeholder="Enter employee ID">
</div>
<div class="row templatemo-form-buttons">
<div class="submit-button">
<button type="submit" class="btn btn-primary">New</button>
</div>
</div>
</div>
</form>
{{--</form>--}}
<script type="text/javascript">
$(document).ready(function() {
$('#example').dataTable();
} );
</script>
#endsection
"edit.blade.php" is
#extends('app')
#section('content')
<div class="templatemo-content-wrapper">
<div class="container">
<ol class="breadcrumb">
<li><font color="green">Home</font></li>
<li class="active">Employee Form</li>
</ol>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-success">
<div class="panel-heading">Employee Form</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('myform/myform/') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($user as $use)
<div class="form-group">
<label class="col-md-4 control-label">Employee ID</label>
<div class="col-md-6">
<input type="text" class="form-control" name="employeeID" value="{{ $use->employeeID }}" placeholder="Enter employee ID">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">E_number</label>
<div class="col-md-6">
<input type="text" class="form-control" name="employeeNo" value="{{ $use->employeeNo}}" placeholder="Enter employee number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-6">
<input type="text" class="form-control" name="Cname" value="{{ $use->Cname }}" placeholder="Enter Contact Name">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">DOB</label>
<div class="col-md-6">
<input type="date" class="form-control" name="dob" value="{{ $use->dob }}" placeholder="Enter date of birth">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Contact Phone</label>
<div class="col-md-6">
<input type="text" class="form-control" name="phoneNumber" value="{{ $use->phoneNumber }}" placeholder="Enter Mobile Number">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Address</label>
<div class="col-md-6">
<input type="text" class="form-control" name="address" value="{{ $use->address }}" placeholder="Enter Address">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-warning"><a href="{{ url('myform/update/'.$use->employeeID) }}">
Update</a>
</button>
</div>
</div>
{{-- <input type="button" id="add" name="add" value="View data" class="button">--}}
#endforeach
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
"myformController.php" is
<?php
namespace App\Http\Controllers;
use App\myform;
use Mail;
use Illuminate\Support\Facades\DB;
use Faker\Provider\DateTime;
use App\User;
use App\Http\Requests\createUserRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Input;
use Symfony\Component\HttpFoundation\Request;
class myformController extends Controller
{
public $type = 'myform';
public function getIndex()
{
// $user = DB::table('user')->get();
$employer = DB::table('employee')->simplePaginate(5);
return view('myform.index')->with('employer',$employer);
}
public function formInsert()
{
$postform = Input::all();
//insert data into mysql table
$data = array('employeeID'=> $postform['employeeID'],
'employeeNo'=> $postform['employeeNo'],
'Cname'=> $postform['Cname'],
'dob'=> $postform['dob'],
'phoneNumber'=> $postform['phoneNumber'],
'address'=> $postform['address'],
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('employee')->Insert($data);
//echo "Record Added Successfully!";
$employer = DB::table('employee')->simplePaginate(10);
return view('myform.index')->with('employer',$employer);
}
public function delete($id)
{
DB::table('employee')->where('employeeID', '=', $id)->delete();
$employer = DB::table('employee')->simplePaginate(10);
return view('myform.index')->with('employer', $employer);
}
public function formIDinsert()
{
$postform = Input::all();
//insert data into mysql table
$data = array('employeeID'=> $postform['employeeID'],
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('employee')->Insert($data);
//echo "Record Added Successfully!";
$employer = DB::table('employee')->simplePaginate(10);
return view('myform.index')->with('employer',$employer);
}
public function edit($id)
{
try {
//Find the user object from model if it exists
$user=DB::table('employee')->where('employeeID', '=', $id)->get();
//$user = User::findOrFail($id);
//Redirect to edit user form with the user info found above.
return view('myform.edit')->with ('user', $user);
//return view('myform.edit')->with('user', myform::find($id));
} catch (ModelNotFoundException $err) {
//redirect to your error page
}
}
// Update user
public function update(Request $request, $id)
{
try{
//Find the user object from model if it exists
$user= myform::findOrFail($id);
DB::table('employee')
->where('employeeID', $id)
->update(['employeeNo' =>$request['employeeNo'],
'Cname'=>$request['Cname'],
'phoneNumber'=>$request['phoneNumber'],
'address'=>$request['address']
]);
//Set user object attributes
//the $request index should match your form field ids!!!!!
//you can exclude any field you want.
// $user->employeeNo = $request['employeeNo'];
// $user->Cname = $request['Cname'];
// $user->phoneNumber = $request['phoneNumber'];
// $user->address = $request['address'];
//Save/update user.
$user->save();
return view('myform.index')->with('user', $user);
//redirect to somewhere
}
catch(ModelNotFoundException $err){
//Show error page
}
}
}
model "myform.php" is
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class myform extends Model
{
protected $table = 'employee';
//protected $primaryKey = 'employeeID';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'employeeID',
'employeeNo',
'Cname',
'dob',
'phoneNumber',
'address',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Routes.php is
Route::any('myform', function()
{
return view('myform/myform');
});
Route::any('myform/myform', 'myformController#formInsert');
Route::any('myform/index', 'myformController#getIndex');
//to show edit form and fetch passed user id info from db
Route::get('myform/edit/{id}', 'myformController#edit');
//to get the edited info and save it to db
Route::get('myform/update/{id}', 'UserController#update');
Route::any('myform/index', 'myformController#formIDinsert');
Route::any('myform/delete/{id}', 'myformController#delete');
You wanna change this in your route:
Route::get('myform/update/{id}', 'UserController#update');
to
//Because the data in your form is transferred/submitted by post request
Route::post('myform/update/{id}', 'UserController#update');
then change your update function to this
// Update user
public function update(Request $request, $id)
{
try{
//Find the user object from model if it exists
$user= myform::findOrFail($id);
//$request contain your post data sent from your edit from
//$user is an object which contains the column names of your table
//Set user object attributes
$user->employeeNo = $request['employeeNo'];
$user->Cname = $request['Cname'];
$user->dob = $request['dob'];
$user->phoneNumber = $request['phoneNumber'];
$user->address = $request['address'];
// Save/update user.
// This will will update your the row in ur db.
$user->save();
return view('myform.index')->with('user', $user);
}
catch(ModelNotFoundException $err){
//Show error page
}
}
If you have any question or need clarification you are most welcome to ask :)
Update in your edit view
change this
<form class="form-horizontal" role="form" method="POST" action="{{ url('myform/myform/') }}">
to
<form class="form-horizontal" role="form" method="POST" action="{{ url('myform/update/').$user->employeeID}}">
change $user->employeeID to whatever is your primary key.
Also change this
<button type="submit" class="btn btn-warning"><a href="{{ url('myform/update/'.$use->employeeID) }}">
Update</a>
</button>
to this
<button type="submit" class="btn btn-warning">Update</button>
Aso you didn't fix your route as I mentioned earlier in this answer.

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