How to fetch and update data in Laravel - php

So, I have two fields. In the first field I want to add old data and in the second field I want to add new data, and below those fields is a button which when clicked will fetch the data from Mongo Table and update the data inside.
I have tried implementing various logic but still no luck. Any sort of help would be appreciated.
This is my controller code:
public function updatedockets($dockets){
if (!Session::has('user_id')){
return Redirect::to('/login');
}
$cart = \App\Models\Cart::where('tracking_no',$dockets)->get()->toArray();
foreach ($cart as $value) {
$cart = \App\Models\Cart::where('tracking_no')->update(['tracking_no' => $dockets]);
}
}
This is my view code:
<div role="tabpanel" class="tab-pane" id="docket-replace">
<form method="post" id="frmDocket" action="/admin/viewdockets">
{!! csrf_field() !!}
<div>
<div class="form-group">
<textarea id="txtreploaceDockets" name="replacedockets" class="form-control" rows="15" placeholder="Enter old docket(s) here. Don't put more than 1000 docket numbers, all dockets should be comma(,) or new line separated"></textarea>
</div>
</div>
</form>
<form method="post" id="frmDocket" action="/admin/viewdockets">
{!! csrf_field() !!}
<div>
<div class="form-group">
<textarea id="txtreplaceDockets" name="replacedockets" class="form-control" rows="15" placeholder="Enter new docket(s) here. Don't put more than 1000 docket numbers, all dockets should be comma(,) or new line separated"></textarea>
</div>
<div class="form-group">
<div class="text-right pr-sm-15 pb-sm-15">
<button type="button" class="btn btn-primary btn-xs" id="btnReplaceDocketData" onclick="return confirm('Are you sure that you have entered the right docket number?')"> Replace Docket </button>
</div>
</div>
</div>
</form>
</div>

As you're not using $value, maybe below code helps you:
$cart = \App\Models\Cart::where('tracking_no', $dockets)
->update(['tracking_no' => $dockets]);

Sample:
$course->statuses()->updateOrCreate(
['status_id' => $id],
['status' => $request->get('status'), 'status_type' => Course::class]
);`

Related

Update not working in Resource Controller for Laravel 8

Previously, I was trying for a few hours to get my "destroy()" function working. Which would sound ridiculous to most of you but simply putting "method("DELETE")" in the form made it work, which I still don't why.
Now, I'm stuck with the update() function. Once I click update, the page just reloads and does nothing. It doesn't update the info in the database nor does it redirect me back to the index. Here's the code:
Route (web.php):
Route::resource('books', BookController::class);
Form (Edit.blade.php):
#section('content')
<div class="row">
<div class="col-lg-12">
<form action="{{ route('books.update', $book->id) }}" method="POST">
#csrf
#method('PUT')
<div class="mb-3 mt-3">
<label for="exampleFormControlInput1" class="form-label">Book Title</label>
<input type="text" name="title" class="form-control" value="{{ $book->title }}">
</div>
<div class="mb-3">
<label for="exampleFormControlTextarea1" class="form-label">Book Author</label>
<input type="text" name="author" class="form-control" value="{{ $book->author }}" placeholder="Author Name..">
</div>
<div class="btn-group" role="group">
<button type="submit" class="btn btn-block btn-danger">Update</button>
<button type="button" class="btn btn-success">Go Back</button>
</div>
</form>
</div>
</div>
#endsection
Controller (BookController):
public function update(Request $request, Book $book)
{
$validated = $request->validate([
'title' => 'required|max:255|unique:books',
'author' => 'required|max:255'
]);
Book::where('id',$book->id)->update($validated);
return redirect()->route('books.index')->with('message','Book Updated!');
}
What am I doing wrong here? Some help would be appreciated.
I think the problem is in your unique validation. When you don't change the title in the form and hit updated, the unique validation fails. It fails because the title already exists in the database. We have to skip unique validation for the resource that is being updated.
$validated = $request->validate([
'title' => 'required|max:255|unique:books,title,'.$book->id, // <-- change this
'author' => 'required|max:255'
]);
$book->update( $request->only(['title', 'author']) );

update URL as input value

I have a search box and when I enter the code in that search box it should show the required data on the web page. but I'm unable to fetch data by using the search box. because I am unable to send that code to my controller which is entered in the search box.
As well as when I am submitting the code in the search box the browser URL should replace that code (It is like an edit function)
So, can someone help me to get it done? The following code shows what I have done.
Thank You, everyone!
{!! Form::open(['action' => ['Account\CodeController#codeResult', 'code'=> 'code'], 'method' => 'get' , 'class'=>"col12"]) !!}
<div class="row mb-5">
<div class="col-4 mx-auto">
<div class="text-input-with-icon">
<span class="fa fa-search form-control-feedback float-right"></span>
<input type="text" name="search" value="" id="barcode" placeholder="Search By code">
</div>
</div>
<div class="col-2 mx-auto">
<button type="submit" name="code" value="name" class="btn btn-reset">SEARCH
</button>
</div>
</div>
{!! Form::close() !!}

laravel foreach input radiobutton displayed incorrectly

I'm trying to display the options for the questions. i have the problem that when im doing it this way:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
i can check all radio buttons at the same time but if i put there a name for the radiobutton i can check only one of the options for the whole questions.. i think there's something strange with the foreach loop..
Info: the table "questions" has the following rows:
id, category_id, question_text, correct_answer, option_text (this is a json-field that is casted to an array)
Code from Controller:
public function store(Request $request, Category $category){
if($request->categoryTest == $category->name){
$questions = $category->question()->inRandomOrder()->get();
return view('user.test', compact('questions', 'category'));
}
}
do you have an idea how to fix this? thank you!
Try this:
#section('content')
<div class="card">
<div class="card-header">Quiz: {{$category->name}}:</div>
<div class="card-body">
<form action="#" method="post" class="form-group">
#csrf
#foreach($questions as $key => $question)
#php
$q = 1+$key
#endphp
<div class="form-group">
<label for="question">Question {{1+$key}}:</label>
<p class="card-text">{{$question->question_text}}</p>
#foreach($question->option_text as $key => $option)
<input name="radio-{{$q}}" type="radio">{{$option}}
#endforeach
</div>
#endforeach
<div class="form-group">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
This seems to be a normal behavior of the radio buttons, only one is market at time. Have you tried to use checkbox instead?
Also, if you want to send it to backend, you'll need to set a name attribute. And here is the magic: instead of using a singular name, put it as array in order to get an array on your controller.
<input type="checkbox" name="question[]" class="btn btn-primary">

How to edit one column in a row of the database in laravel

How to edit one column in a row of the database in laravel
I can't update one column of row has multiple columns by laravel
My edit :
public function edit($id)
{
$addremark=bookappoitment::findOrFail($id);
return view('admin.ManageTime.addremarks', compact('addremark'));
}
My update:
public function update(Request $request, $id)
{
$request->validate([
'Remarks'=>'required'
]);
$data=bookappoitment::find($id);
$data->Remarks = $request->get('Remarks');
$data->save();
return view('/home');
}
link to update:
Update
form:
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Remarks :</label>
<textarea class="form-control" rows="10" name="Remarks" placeholder="Remarks">{{ $addremark->Remarks }}</textarea>
</div>
<a class="btn btn-success btn-mini deleteRecord " href="{{route('BookAppoint.update',$addremark->id)}}">Update</a>
</div>
</div>
</div>
You can update 1 (or more, just add to the array) column through the DB class.
DB::table('yourTable')->where('id', $id)->update(['remarks' => $request->input('Remarks')]);
You can also do it for a model like so:
bookappoitment::where('id', $id)->update(['remarks' => $request->input('Remarks')]);
It's in the Laravel documentation here.
<form class="" action="index.html" method="{{ route('BookAppoint.update', $addremark->id) }}">
#method('PATCH')
#csrf
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Remarks">Remarks :</label>
<textarea name="remarks" rows="10" placeholder="Remarks">{{$addremarks->Remarks}}</textarea>
</div>
</div>
<input type="submit" name="submit" value="Update">
</div>
</div>
</form>

Laravel resource store method is being redirected to destroy method

I have a resource route
Route::resource('climb-excluded','CexcludedController',['only'=>['store','update','destroy']]);
And my code in view to save data
<div class="col-lg-4">
<form class="form" method="POST" action="{{ route('climb-excluded.store') }}">
{{ csrf_field() }}
<div class="card">
<div class="card-head style-primary">
<header>Add item</header>
</div>
<div class="card-body floating-label">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name">
<label for="name">Name</label>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<button type="submit"
class="btn btn-block btn-success ink-reaction">
Add
</button>
</div>
</div>
</div>
</div>
A button to destroy data:
{!! Form::open( array('route'=>array('climb-excluded.destroy', $excluded->id),
'method'=>'DELETE')) !!}
<button type="submit"
class="btn ink-reaction btn-floating-action btn-sm btn-danger "
rel="tooltip"
title="Delete">
<i class="fa fa-trash-o" aria-hidden="true"></i>
</button
{!! Form::close() !!}
Store method form controller:
public function store(Request $request)
{
$this->validate($request,[
'name' => 'required|max:255'
]);
$excluded = new Cexcluded;
$excluded -> name = $request->name;
$excluded->save();
//redirect to
Session::flash('success','New item sucessfully added !');
return back()->withInput(['tabs'=>'second4']);
}
Destroy method form controller:
public function destroy($id)
{
$trekExcluded = Cexcluded::find($id);
$trekExcluded->tours()->detach();
$trekExcluded ->delete();
Session::flash('success','Item sucessfully deleted !');
return back()->withInput(['tabs'=>'second4']);
}
The trouble/bug that I'm facing is I can insert first row into table successfully. But when I go for the second one, the store method is somehow redirected to destroy method and deletes the first inserted row also. While I've clearly declared store method in action attribute of the form.
FYI: Both routes exists in same view/page. Destroy method in col-md-8with foreach loop while store method in col-md-4
Its quite obvious, that your form don't have a unique name or id, so that's why the second method is redirected to destroy method. Do something like this:
cex-store-1
cex-destroy-1

Categories