Laravel update single column - laravel

Hello am working in laravel crud and all are working fine except update. I need to update a single column of my database.The column i need to update is soldStatus.
Controller class.
public function update(Request $request, $product_id)
{
$request->validate([
'product_sales_status'=>'required'
]);
$prod = Products::FindOrFail('product_id');
$prod->product_id = $request->input('product_id');
$prod->product_sales_status = $request->input('product_sales_status');
$prod->save();
return redirect()->back();
}
Modal body with update form
<div class="modal-body">
<form action="{{route('products.update', $productt-
>product_id)}}" method="POST">
#csrf
<p> Item will be registerd as Sold?</p>
<input type="hidden" name="product_sales_status" value="Yes">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<input type="hidden" name="product_id" value="{{$productt->product_id}}">
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
and my route file is like below
Route::post('products_list/{product_id}','ProductsController#update')->name('products.update');
Thanks

try this:
public function update(Request $request, $product_id)
{
$request->validate([
'product_sales_status'=>'required'
]);
$prod = Products::find($product_id);
$prod->product_sales_status = $request->input('product_sales_status');
$prod->save();
return redirect()->back();
}
I think you wanna update product_sales_status column. However, you can change the column name in $prod->product_sales_status

Related

Store data in PostgreSQL using Laravel and Vue.js

I am new to Vue.js and would like to know more about it, so I tried this video. I followed all code but cannot store data in the database and I get this error:
TodoController.php
public function store(Request $request)
{
$todo = Todo::create($request->all());
$todo->create();
return $todo;
}
Post.php
protected $fillable = ['title', 'completed'];
TodoComponent.vue
<form #submit.prevent="saveData">
<div class="input-group mb-3">
<input v-model="form.title" type="text" class="form-control form-control-lg" aria-label="Recipient's username" aria-describedby="button-addon2">
<div class="input-group-append">
<button class="btn btn-success" type="submit" id="button-addon2">Add List </button>
</div>
</div>
</form>
Vue method
methods:{
saveData(){
let data = new FormData();
data.append('title', this.form.title)
axios.post('/api/todo', data)
}
},

laravel 6 : Call to a member function store() on null

I am trying to attach a file to a blog type post. For this i have an file field and 2 buttons, one saves the fields to the database and the other uploads the file. standalone the file upload works as intended. However in the form i get Call to a member function store() on null. I have changed the menthod from put to post, but that doesnt seem have any effect.
Below my post forms and the function in the controllers.
The Form:
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">New/Edit Blog</div>
<div class="card-body">
#if($data)
<form … action = "{{Route ('post.update', $data->_id)}}" method="post", enctype = 'multipart/data'>
#csrf
#method('POST')
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title" value = "{{$data->title}}" >
</div>
<div class="form-group">
<label for="shorttext">Shorttext:</label>
<input type="text" class="form-control" name="shorttext" value = "{{$data->shorttext}}" >
</div>
<div>
<input id="x" type="hidden" name="content" value ="{{$data->content}}">
<trix-editor input="x" ></trix-editor>
</div>
<div class="form-group">
<label for="created_by">Created By:</label>
<input type="text" class="form-control" name="created_by" value = "{{$data->created_by}}" readonly>
</div>
<input type="file" name="attachment" enctype="multipart/form-data"/>
<p align="center">
<input type="submit" btn btn-primary class="btn btn-primary" id="save" name="action" value="save">
<input type="submit" btn btn-primary class="btn btn-primary" id="upload" name="action" value="upload">
The functions in the controller:
Store:( this one is the function that determines which button is pressed)
public function store(Request $request, $_id = false, $attachment = false){
//check which submit was clicked on
if($request->action == 'upload'){
//
$this->upload($request);
return redirect()->route('home');
} elseif($request->action == 'save') {
//echo 'save pressed';
//run function save all form fields
$this->update($request, $_id);
return redirect()->route('home');
} else {echo "error";}
}
Upload function:
function upload(Request $request){
$path = $request->file('attachment');
// $original = $request->file('attachment')->getClientOriginalName();
$path->store('/public');
Update function:
public function update (Request $request, $_id){
//$this->upload($request);
/*
$path = $request->file('attachment');
$path->storeas('/public','123'); */
$data = post::findOrFail($_id);
$data->title = $request->title;
$data->content = $request->content;
$data->shorttext = $request->shorttext;
$data->created_by = $request->created_by;
$data->text3 = $request->text3;
$data->attachment = $request->attachment;
$data->save();
if($data){
return redirect()->route('home');
}else{
return back();
}
}
The route is:
Route::post('/post/update/{_id}', 'PostController#store')->name('post.update');
As mentioned, the save function in the complete form works. In a standalone form ( and a standalone controller) the upload works (and i can, if i wish, manipulate the filename), but how do i bring the 2 together? At first i thought it was because the update form had a PUT method, but changing everything to post doesnt seem to have any effect and i still get the Null error.
For completeness the standalone solution:
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Te7aHoudini\LaravelTrix\Pipes\AttachmentInput;
class UploadController extends Controller
{
//
function upload(Request $req){
$path = $req->file('attachment');
$original = $req->file('attachment')->getClientOriginalName();
$path->storeas('/public',$original);
echo $original;
}
}
The standalone form:
<html>
<head>
<title>Upload</title>
</head>
</html>
<form action="upload" method="POST" enctype="multipart/form-data">
<input type="file" name="attachment" />
#csrf
<button type="submit">file upload</button>
</form>
In your form element Change enctype = 'multipart/data' to enctype="multipart/form-data"
<form … action = "{{ Route ('post.update', $data->_id) }}" method="post", enctype="multipart/form-data">
Hope this solves your problem.

Array to string conversion using laravel

I am trying to send a multi user-id into the database when I select users from the checkbox then I click to submit so I face error Array to string conversion how can I resolve this issue? please help me thanks.
please see error
https://flareapp.io/share/17DKWRPv
controller
public function adduseraction(REQUEST $request)
{
$useradd=$request->get('userid');
$checkid=$request->get('multiusersid');
$user=Users_permissions::create([
'user_id'=>$useradd,
'user_Access_id'=> $checkid
]);
$user->save();
}
html view
<div class="card card-success">
<div class="card-header">
<h3 class="card-title">Users Permission </h3>
</div>
<br>
<form action="{{route('adduseraction')}}" method="post">
{{ csrf_field() }}
<div class="col-sm-4">
<select name="userid" class="form-control">
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->name}}</option>
#endforeach
</select>
</div>
<div class="card-body">
<!-- Minimal style -->
<div class="row">
#foreach($users as $user)
<div class="col-sm-2">
<div class="form-check">
<input type="checkbox" name="multiusersid[]" value="{{$user->id}}" class="form-check-input" >
<h5 style="position:relative;left:10px;">{{$user->name}}</h5>
</div>
<!-- checkbox -->
</div>
#endforeach
</div>
<!-- /.card-body -->
</div>
<div class="card-footer">
<button type="submit" name="btnsubmit" class="btn btn-primary col-md-2
center">Submit</button>
</div>
</form>
</div>
<!-- /.content-wrapper -->
Route
Route::post('adduseraction','AdminController#adduseraction')->name('adduseraction');
** current status **
{"_token":"4Z3ISznqKFXTMcpBKK5tUgemteqxuJjQpKF8F0Ma","userid":"6","multiusersid":["2","5","7"],"btnsubmit":null}
use implode($checkid, ',');
public function adduseraction(REQUEST $request)
{
$useradd=$request->get('userid');
$checkid=$request->get('multiusersid');
$user=Users_permissions::create([
'user_id'=>$useradd,
'user_Access_id'=> implode($checkid, ',');
]);
}
Change in your Users_permissions model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Users_permissions extends Model
{
protected $table = 'userspermissions';
protected $fillable = [
'user_id','user_Access_id'
];
}
Here is your solution
public function adduseraction(REQUEST $request)
{
$useradd=$request->get('userid');
$checkid=implode(",", $request->get('multiusersid'));
Users_permissions::create([
'user_id'=>$useradd,
'user_Access_id'=> $checkid
]);
}
It is expecting a string and you are passing an array of ids. You may want to change the database to json or do json_ecode(checkid). Which will stringify your array. then you can store. However, remember you will need to convert it back with typecasting or manually doing it.
example:
public function adduseraction(REQUEST $request)
{
$useradd=$request->get('userid');
$checkid=$request->get('multiusersid');
$user=Users_permissions::create([
'user_id'=>$useradd,
'user_Access_id'=> json_encode($checkid)
]);
// $user->save(); // yes obviously not needed
}

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 update specific row using modal Laravel 5.8

I am trying to update a specific row using modal. However I don't have any idea how to pass the value of the row id to the route parameter.
Here's the update form.
<form action="{{route('subcategory.update', 'idhere')}}" method="POST">
#method('PATCH')
#csrf
<div class="modal-body">
<label for="editname">New sub-category name:</label>
<input type="text" name="editname" id="editname" class="form-control">
<input type="text" name="editid" id="editid" value="" hidden>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button class="btn btn-primary" type="submit">Save changes</button>
</div>
</form>
Edit button
<button class="btn btn-secondary btn-sm" data-myid="{{$item->id}}"
data-mytitle ="{{$item->name}}"
data-target="#editsub" data-toggle="modal">Edit</button>
And here's the controller
public function update(Request $request, Subcategory $subcategory)
{
// return $request;
Subcategory::where('id',$request->editid)->update([
'name' => $request->editname
]);
return back();
}
When you pass an id here and if your model name is subcategory.php you will get the db record in your controller.
<form action="{{route('subcategory.update', $subcategory )}}" method="POST">
Or you can do following too
<form action="{{route('subcategory.update', $subcategory->id )}}" method="POST">
Your route should have {subcategory} in it for eager loading. After that you can do following in your code.
public function update(Request $request, Subcategory $subcategory)
{
//variable $subcategory has the db record
$subcategory->update(['name' => $request->only('editname')]);
return back();
}
If I understand it correctly, you can store the item id in
<input type="text" name="editid" id="editid" value="{$item->id}" hidden>
And since you are using POST request, you can easily get the item id from the request.
Like the one in your post:
$request->input('editid');
Please try first to pass id in input box hidden field

Resources