I want to display data in textfield. when search data have - laravel

I want to display data in textfield . Not all data. When search by name and it have data then went to display data in textfield. And search form and want to display form is same form.
<form action="postAuth" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="text" class="form-control" name="productname" placeholder="Search Product"> <span class="input-group-btn">
<button type="submit" class="btn btn-default" name="search">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<div class="form-group">
<label for="ProductName" >Product Name :</label>
<input type="text" class="form-control" name="ProductName">
</div>
</form>
route
Route::post("postAuth", ['as' => 'search' , 'uses'=> 'ProductController#postAuth']);
That's my controller
public function postAuth(Request $request)
{
//check submit
$update = $request->get('update',false);
if($update){
return $this->update($request);
}
$productname = $request->input('productname');
$product = DB::table('products')
->where('product_name','LIKE','%'.$productname.'%')
->get();
if($product->count() > 0)
return redirect()->to('/update')->withDetails($product)->withQuery($productname);
else
$request->session()->flash('alert-danger','No Data Found!');
return redirect()->to('/update');
}
can anyone help me please

Here what you exactly want : Just for example
<div class="form-group">
{{Form::label('name', trans('admin.venue.fields.name'),['class' => 'col-md-4 control-label'])}}
<div class="col-md-6">
{{Form::text('name',old('name', isset($venue) ? $venue->name : ''),['class' => 'form-control'])}}
</div>
</div>
Model function example :
public function save(Request $request) {
try {
$this->validate($request, Venue::rules(), Venue::messages());
$venue = Venue::saveOrUpdate($request);
if($venue !== false) {
if($request->get('continue', false)) {
return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
} else {
return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
}
} else {
return back()->with('error', "Unable to save venue")->withInput();
}
} catch (\Exception $ex) {
return back()->with('error', "Unable to save venue")->withInput();
}
}
Hope it is useful.

use session() in the value field of ProductName input tag and with some logic, for example
view file:
#if(session()->has('data'))
#if(count(session('data')))
#foreach(session('data') as $data)
<input type="text" class="form-control" name="ProductName" value="{{ $data->prodcut_name }}">
#endforeach
#else
<input type="text" class="form-control" name="ProductName" value="No Data Available">
#endif
#else
<input type="text" class="form-control" name="ProductName">
#endif
and in Controller:
public function postAuth(Request $request) {
$productname = $request->input(productname);
$product_search = Products::where('product_name', $productname)->get();
if($product_search) {
return redirect()->back()->with('data', $product_search);
}
}

Related

laravel routing, view not found

when am adding, it records saves to database, but when return to view is says ($store_id is undefined)
Blade:
<form action="{{ route("machine-slots.create", $machineSlotId) }}" method="POST">
#csrf
<input type="hidden" name="store_id" value="{{ $store_id }}">
<div class="modal-body ct-modal-form">
<div class="form-group">
<label for="slot_id" class=" form-control-label">Slot ID</label>
<input type="number" id="slot" name="slot_id"
placeholder="Enter Slot ID" class="form-control">
</div>
<div class="form-group ct-form-dropdown">
<label for="assign-item" class="form-control-label">Assign Item</label>
<select class="form-control" name="product_id">
<option value="" disabled selected>Select Items</option>
#forelse ($products as $i => $product)
<option value={{$product->id}}>{{$product->name}}</option>
#empty
#endforelse
</select>
</div>
<div class="form-group">
<label for="quantity" class=" form-control-label">Quantity</label>
<input type="number" id="quantity" name="current_count"
placeholder="Enter Quantity" class="form-control"
onkeyup="this.value = this.value.toUpperCase();">
</div>
<div class="form-group">
<label for="max-quantity" class=" form-control-label">Max Quantity</label>
<input type="number" id="max-quantity" name="max_count"
placeholder="Enter Max Quantity" class="form-control"
onkeyup="this.value = this.value.toUpperCase();">
</div>
</div>
<div class="modal-footer">
<input type="hidden" value="{{ $machineSlotId }}" name="machine_address_id">
<button type="submit" class="btn btn-primary">Add</button>
</div>
</form>
Controller:
public function store(Request $request, $machineSlotId)
{
//get all machineslot data
$machineSlots = $this->getMachineSlotData1($machineSlotId);
$products = $this->getProductsData();
$store_id = Machine::where([Machine::COLUMN_MACHINE_ADDRESS_ID => $machineSlotId])->first(['store_id'])->store_id;
if($request->input('current_count') > $request->input('max_count'))
{
//display text
Session::flash('warning', "Quantity cannot be higher than max Quantity. Please Try again");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
else
{
//create new record
MachineSlot::create($request->all());
//display text
Session::flash('success', "Table has been updated.");
//display return
//return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
return redirect()->route('machine-slots.index')->with(compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
}
Web:
`
Route::post('/machine-slots/{id}', [App\Http\Controllers\UI\MachineSlotController::class, 'store'])->name('machine-slots.create');
Error
Heres the Update:
Blade
no Changes
Controller: Add on Index
public function index()
{
//check user Data
if(Auth::user() != null)
{
//get user ID
$userID = User::findOrFail(auth()->user()->id)->first(['id'])->id;
//check store is existing, based of passed ID
$isStoreExisting = Store::where('user_id', $userID)->first(['id'])->id;
//check machine is Existing, based on passed ID
$isMachineExisting = Machine::where('store_id', $isStoreExisting)->first(['machine_address_id'])->machine_address_id;
//get machine data
$machineSlot = MachineSlot::where('machine_address_id', $isMachineExisting)->first();
$machineSlots = MachineSlot::where('machine_address_id', $isMachineExisting)->paginate(5);
//fix paginate error
return view('machine-slots',compact('machineSlot', 'machineSlots'));
}
}
public function store(Request $request, $machineSlotId)
{
//get all machineslot data
$machineSlots = $this->getMachineSlotData1($machineSlotId);
$products = $this->getProductsData();
$store_id = Machine::where([Machine::COLUMN_MACHINE_ADDRESS_ID => $machineSlotId])->first(['store_id'])->store_id;
if($request->input('current_count') > $request->input('max_count'))
{
//display text
Session::flash('warning', "Quantity cannot be higher than max Quantity. Please Try again");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
else
{
//create new record
MachineSlot::create($request->all());
//display text
Session::flash('success', "Table has been updated.");
//display return
return view('machine-slots', compact('machineSlotId', 'machineSlots', 'products', 'store_id'));
}
}
Error: Data duplicates every time i refresh the page

Get product id to store attachments accordingly

I currently have the add attachment button for each product on the product list page. After clicking the button, will proceed to add attachment form. How do I get the current product ID from the product table in order to store the attachment data into the attachment table?
Route
Route::post('/store/{product}', 'AttachmentController#store')->name('attachment.store');
Product Model
public function attachment()
{
return $this->hasMany(Attachment::class, 'product_id', 'id');
}
Attachment Model
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
Controller
public function create()
{
return view('document.create', ['prod' => Product::select('id', 'name')->get()]);
}
public function store(Request $request, Product $product) {
$data['product_id'] = $product->id;
$data = $request->validate([
'file' => 'required',
'file.*' => 'mimes:csv,xlsx,pdf,docx',
]);
$attachmentData = [];
if($request->hasFile('file'))
{
foreach($request->file('file') as $file) {
$path = public_path('storage/attachments/'.$request->product_id);
$fileName = time().'-'.$file->getClientOriginalName();
$file->move($path, $fileName);
$attachmentData[] = $fileName;
}
$data['file'] = json_encode($attachmentData);
}
$attachment = Attachment::create($data);
return redirect()->route('product.index')->with('success','Attachment added successfully');
}
Blade View
<form method="POST" action="{{route('attachment.store')}}" enctype="multipart/form-data">
#csrf
<h3><b>Add Attachment</b></h3>
<input type="submit" class="btn btn-primary mr-2" value="Save">
<div class="row">
<h4 class="card-title">General</h4>
<input type="text" name="product_id" value="{{ $product->id ?? '' }}" hidden>
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" required>
</div>
<div class="form-group">
<label>Attachment </label>
<div class="input-group-append">
<label for="attachment" class="btn btn-info">Upload</label>
<input id="attachment" type="file" name="file[]" multiple required>
</div>
</div>
</div>
</form>
You have to use form action like below
<form method="POST" action="{{route('attachment.store',['product'=>$product->id])}}" enctype="multipart/form-data">

Laravel - How to update checkbox field value

Using Laravel-5.8, I have been able to save the data into the database including the check box field.
protected $fillable = [
'appraisal_name',
'is_current',
'appraisal_start',
'appraisal_end',
];
public function rules()
{
return [
'appraisal_name' => 'required|min:5|max:100',
'appraisal_start' => 'required',
'appraisal_end' => 'required|after_or_equal:appraisal_start',
'is_current' => 'nullable|boolean',
];
}
public function create()
{
return view('appraisal.appraisal_identities.create');
}
public function store(StoreAppraisalIdentityRequest $request)
{
$identity = AppraisalIdentity::create([
'appraisal_name' => $request->appraisal_name,
'appraisal_start' => $appraisalStart,
'appraisal_end' => $appraisalEnd,
'is_current' => $request->has('is_current'),
]);
Session::flash('success', 'Appraisal Initialization is created successfully');
return redirect()->route('appraisal.appraisal_identities.index');
}
public function edit($id)
{
abort_unless(\Gate::allows('appraisal_identity_edit'), 403);
$identity = AppraisalIdentity::where('id', $id)->first();
return view('appraisal.appraisal_identities.edit')->with('identity', $identity);
}
public function update(UpdateAppraisalIdentityRequest $request, $id)
{
abort_unless(\Gate::allows('appraisal_identity_edit'), 403);
$appraisalStart = Carbon::parse($request->appraisal_start);
$appraisalEnd = Carbon::parse($request->appraisal_end);
$submissionStart = Carbon::parse($request->submission_start);
$submissionEnd = Carbon::parse($request->submission_end);
$identity = AppraisalIdentity::find($id);
$identity->appraisal_name = $request->appraisal_name;
$identity->appraisal_start = $appraisalStart;
$identity->appraisal_end = $appraisalEnd;
$identity->is_current = $request->has('is_current');
$identity->save();
Session::flash('success', 'Appraisal Initialization is updated successfully');
return redirect()->route('appraisal.appraisal_identities.index');
}
create.blade
<form action="{{route('appraisal.appraisal_identities.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Is Current Appraisal?</label>
<div class="col-md-9">
<input type="checkbox" class="form-control" name="is_current" value="{{old('is_current')}}">
</div>
</div>
</div>
</div>
</div>
<div>
<!--<input class="btn btn-primary" type="submit" value="{{ trans('global.save') }}">-->
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('appraisal.appraisal_identities.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
edit.blade
<form action="{{route('appraisal.appraisal_identities.update', ['id'=>$identity->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Is Current Appraisal?</label>
<div class="col-md-9">
<input type="checkbox" class="form-control" name="is_current" value="{{old('is_current')}}">
</div>
</div>
</div>
</div>
</div>
<div>
<!--<input class="btn btn-primary" type="submit" value="{{ trans('global.save') }}">-->
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('appraisal.appraisal_identities.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
I have these issues while trying to update the data?
The edit checkbox field (is_current) did not pick the value from the database when loaded. It remains unchecked.
is_current is either 0 or 1. The goal is that, there can only be one is_current field that is set to 1 in the table. From the checkbox, when is_current is checked to 1, it should set any other is_current that is 1 to 0 in the table.
How do I resolve these issues?
Thank you.
In edit blade update your checkbox code as this:
<input type="checkbox" class="form-control" name="is_current" #if($identity->is_current == 1) checked #endif value="{{old('is_current')}}">
In your update function change this:
public function update(UpdateAppraisalIdentityRequest $request, $id)
{
abort_unless(\Gate::allows('appraisal_identity_edit'), 403);
$appraisalStart = Carbon::parse($request->appraisal_start);
$appraisalEnd = Carbon::parse($request->appraisal_end);
$submissionStart = Carbon::parse($request->submission_start);
$submissionEnd = Carbon::parse($request->submission_end);
$identity = AppraisalIdentity::find($id);
$identity->appraisal_name = $request->appraisal_name;
$identity->appraisal_start = $appraisalStart;
$identity->appraisal_end = $appraisalEnd;
$identity->is_current = $request->has('is_current');
$identity->save();
// this line update all column to 0 and leave $id field
AppraisalIdentity::where('id', '!=', $id)->update(['is_current' => 0]);
Session::flash('success', 'Appraisal Initialization is updated successfully');
return redirect()->route('appraisal.appraisal_identities.index');
}
In your store function change this:
public function store(StoreAppraisalIdentityRequest $request)
{
$identity = AppraisalIdentity::create([
'appraisal_name' => $request->appraisal_name,
'appraisal_start' => $appraisalStart,
'appraisal_end' => $appraisalEnd,
'is_current' => $request->has('is_current'),
]);
$id = $identity->id;
// this line update all column to 0 and leave $id field
AppraisalIdentity::where('id', '!=', $id)->update(['is_current' => 0]);
Session::flash('success', 'Appraisal Initialization is created successfully');
return redirect()->route('appraisal.appraisal_identities.index');
}
try this in the edit blade
<
is_current == 1 ? checked : value="{{old('is_current')}}">

How to Manage Accounts in the Laravel 5 FrameWork - MVC - with this code?

How to Manage Accounts in the Laravel 5 FrameWork - MVC - with this code? I got it all for a default presentation but i still get an Undefined Variable request with this code - please your answer will be appreciated:
UserController:
public function account(&$data, $request){
User::get_user(self::$data,$request);
self::$data['title'] = self::$data['title'] . 'Edit Account';
return view('forms.account', self::$data);
}
public function postAccount(AccountRequest $request){
User::edit_user($request);
return redirect('');
}
AccountRequest:
public function rules()
{
return [
'name' => 'required|min:2|max:70',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6|max:10|confirmed',
];
}
Model:
static public function get_user(&$data,$request){
$sql = "SELECT * FROM users WHERE id = ". Session::get('user_id');
$data['users'] = DB::select($sql);
}
static public function edit_user(&$data,$request) {
$id = $data['id'];
$sql = "SELECT * FROM users WHERE id = ".$id;
$getVal = DB::select($sql);
if($data['name'] || $data['password'] || $data['email']){
if($data['name']){
DB::update("UPDATE users SET name = ? WHERE id = ?",[$data['name'],$id]);
session(['user_name' => $data['name']]);
}
if($data['password']){
DB::update("UPDATE users SET password = ? WHERE id = ?",[bcrypt($data['password']),$id]);
}
if($data['email']){
DB::update("UPDATE users SET email = ? WHERE id = ?",[$data['email'],$id]);
}
}
Session::flash('sm',$request['name'] . '`s Has Been Updated');
}
Web:
Route::get('user/account', 'UserController#account');
Route::post('user/account', 'UserController#postAccount');
HTML:
#extends('master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<h1>Edit Your Account -</h1>
</div>
<div class="row" style="margin-left:30%;">
<div class="col-md-6">
<form action="" method="post">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{ $user['id'] }}">
<div class="form-group">
<label for="name"></label>
<input value="{{ $user['name'] }}" type="text" name="name"
class="form-control" id="name" placeholder="Name">
</div>
<div class="form-group">
<label for="email"></label>
<input value="{{ $user['email'] }}" type="text" name="email"
class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<label for="editpassword"></label>
<input type="password" name="password" class="form-control"
id="editpassword" placeholder="Edit Password">
</div>
<div class="form-group">
<label for="editpasswordconf"></label>
<input type="password" name="password_confirmation" class="form-
control" id="editpasswordconf" placeholder="Confirm New Password">
</div>
<div class="form-group text-center">
<input type="submit" name="submit" value="Update Details" class="btn
btn-primary">
</div>
</form>
</div>
</div>
</div>
#endsection
Your AccountController should look like this:
public function edit($request, Account $account){
return view('forms.account', [
'account' => $account,
'title' => 'Edit Account'
]);
}
public function update(AccountRequest $request, Account $account){
$account->update($request->all());
Session::flash('sm', $account->name . ' Has Been Updated');
return redirect()->back();
}
AccountRequest:
public function rules()
{
return [
'name' => 'required|min:2|max:70',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6|max:10|confirmed',
];
}
That is about as much code as you need for this process... please read the documentation for Eloquent https://laravel.com/docs/5.4/eloquent

Laravel - How to handle errors on PUT form?

I am working with laravel 5.2 and want to validate some data in an edit form. I goal should be to display the errors and keep the wrong data in the input fields.
My issue is that the input is validated by ContentRequest and the FormRequest returns
$this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
which is fine so far. Next step the edit action in the controller is called and all parameters are overwritten.
What I have currently done:
ContentController:
public function edit($id)
{
$content = Content::find($id);
return view('contents.edit', ['content' => $content]);
}
public function update(ContentRequest $request, $id)
{
$content = Content::find($id);
foreach (array_keys(array_except($this->fields, ['content'])) as $field) {
$content->$field = $request->get($field);
}
$content->save();
return redirect(URL::route('manage.contents.edit', array('content' => $content->id)))
->withSuccess("Changes saved.");
}
ContentRequest:
class ContentRequest extends Request
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required|min:3',
'body' => 'required|min:3'
];
}
}
How can I fix this? The form looks like this:
<form action="{!! URL::route('manage.contents.update', array('content' => $content->slug)) !!}"
id="site-form" class="form-horizontal" method="POST">
{!! method_field('PUT') !!}
{!! csrf_field() !!}
<div class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="title" id="title" placeholder="Title"
value="{{ $content->title }}">
#if ($errors->has('title'))
<span class="help-block">
<strong>{{ $errors->first('title') }}</strong>
</span>
#endif
</div>
</div>
</form>
Try something like the following:
<input
type="text"
class="form-control"
name="title"
id="title"
placeholder="Title"
value="{{ old('title', $content->title) }}" />
Note the value attribute. Also check the documentation and find Retrieving Old Data.

Resources