How to update a single value with laravel - laravel

Hello i want to update a single value on another table using laravel. This is the code i have done until now but doesnt seem to work:
$amount = Product::findorFail($request->products[$i]);
$total_value = $request->amount[$i] + $amount->amount;
$amount->update(['amount', $total_value]);
dd($total_value);
with dd i see that the result is correct but the update function is not, the query im trying to make is
update table set amount=x where id=y

You have multiple choices. The shortes are:
$amount->update(['amount'=> $amount->amount + request->amount[$i]]);
or
Product::findorFail($request->products[$i])->increment('amount', $request->amount[$i]);

you could change your code like below
$amount = Product::findorFail($request->products[$i]);
$total_value = $request->amount[$i] + $amount->amount;
$amount->amount=$total_value;
$amount->update();
or as mentioned in comments you could use eloquent increment function

Related

Codeigniter updating data with OR condition in ID

Hello I am a beginner in using Codeigniter. What I want is to update a data in Codeigniter with an OR condition to the ID.
Here is my MySQL query that I want to make happen:
Update message_received SET is_read = 1 WHERE msg_id = 3 OR parent_id = 3
I tried something like this:
$this->query->update_array('message_received', array('msg_id' => $_POST['read_my_message'],'parent_id' => $_POST['read_my_message']) , array('is_read'=>1));
You can use or_where to apply OR condition in CodeIgniter, I've written a possible solution for your question(Reference).
See if it helps you.
$msg = $this->input->post('read_my_message'); // get the post data
$this->db->set('is_read', 1); // set the value of column
$this->db->where('msg_id', $msg); // first condition
$this->db->or_where('parent_id', $msg); // second contion
$this->db->update('message_received'); // table name
// Produces:
/* UPDATE `message_received` SET `is_read` = 1 WHERE `msg_id` = '$msg' OR `parent_id` = '$msg' */
You can apply OR. The references https://www.codeigniter.com/userguide3/database/query_builder.html

Wrong total pagination with select distinct in Laravel 6

I use Laravel 6.12, I have this request :
$queryJob = DB::table('jobs as j')->join('job_translations as jt', 'j.id', 'jt.job_id')
->whereNull('j.deleted_at')
->whereNull('jt.deleted_at')
->select('j.id', 'j.short_name', 'j.status', DB::raw("case when j.short_name = '{$request->short_name}' then 0 else 1 end"))
->distinct();
$jobs = $queryJob->paginate($qtyItemsPerPage);
The results displays an error for the total :
The total = 3, but as you can see the data contains only 2 elements.
I read here that when using a distinct, I must be clear on which column the total must be calculated: distinct() with pagination() in laravel 5.2 not working
So I modified my query like that:
$jobs = $queryJob->paginate($qtyItemsPerPage, ['j.*']);
But without success, the total is still wrong.
Hoping that I don't misunderstand your DB and relations structure and purpose of your query perhaps this will avoid using distinct or groupBy altogether?
$shortname = $request->input('short_name');
$queryJob = Job::with('job_translations')->select('id','short_name',
'status', DB::raw("case when short_name = '" . $shortname . "'
then 0 else 1 end")
->paginate($qtyItemsPerPage);
Pagination can be easily manually added with skip and take in case you need to use groupBy
$queryJob->skip(($page - 1) * $qtyItemsPerPage)->take($qtyItemsPerPage)->get();
The solution for me was to pass a field name to the distinct() method.
With your example:
$queryJob = DB::table('jobs as j')
// joins, where and other chained methods go here
->distinct('j.id')
Solution taken from https://stackoverflow.com/a/69073801/3503615

Adding values in database with query in laravel

I want to add 10 from a current database value.
Current db value = 20;adding = 10;updated value 30;
The following code is not working.
DB::table('employee')->increment('bonus'=>'bonus+10');
Try this:
DB::table('employee')->increment('bonus', 10);
Try this -
DB::table('employee')->increment('bonus', 10);
You can do like this as well:
DB::table('employee')
->where('rowID', 1) // if you to add in a perticular table else comment it for updating entire column's value by adding 10 in it.
->update([
'bonus' => DB::raw('bonus + 10'),
]);
Hope this will help you.

Laravel 4 Paginantor::make, returning same data on every page

I have a set of documents, which I have stored in an array $resultset. I want to apply laravel Paginator::make method on it.
In Controller:
$total_resultset = count($resultset);
$perPage = 10;
$currentPage = Input::get('pageno',1);
$offSet = ($currentPage * $perPage) - $perPage;
$pagedData = array_slice($resultset, $offSet,$perPage, true);
$resultset = Paginator::make($pagedData, $total_resultset, $perPage);
and in view:
#foreach($resultset as $r)
$r->title
#endforeach
{{ $resultset->links() }}
The problem is, when I use $PagedData in Paginator::make it returns 10 docs per page. But all of the pages have same set of documents, rather than different docs, the page=2 does not show new set of docs. And when I use $resultset in Paginator::make it returns me all of the documents rather than only 10.
I can't use Eloquent or DB query as the $resultset has docs, based on different queries. I'm confused where I'm wrong.
P.S I've been trying to solve the issue since last two days, but didn't get successful. Please help me out, any kind of help would be appreciated.
Thanks in advance
$total_resultset = count($resultset);
$perPage = 10;
$currentPage = Input::get('pageno',0); //if you use default 1 you will skip 10 results
$offSet = ($currentPage * $perPage);// Ex: (1*10)= 10 - 10 = 0 you are not moving
$pagedData = array_slice($resultset, $offSet,$perPage, false);//maybe you are using Eloquent? you can use take() and skip() if is the case; and you was preserving the array keys in slice so never move really
$resultset = Paginator::make($pagedData, $total_resultset, $perPage);
hope helps :)

Doctrine ORM Update Row with ID

Is there a way to update a row directly with and ID? I just want the ability to update a table row field without querying for the object first. I tried this...
$id = 1;
$s = new Sandbox();
$s->setId($id);
$s->setFname('moon');
$e = $em->merge($s);
$em->flush($e);
and it tried to do an update to the database, however, it failed because it tried to update all the undefined fields as well whereas I just want to update the fname field.
Thanks
$id = 1;
$s = $em->getReference('Sandbox', $id);
$s->setFname('moon');
$em->persist($s);
$em->flush($s);

Resources