Codeigniter updating data with OR condition in ID - codeigniter

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

Related

How to update a single value with 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

laravel where if get a single value

Hello there
I want to write a query. I want to select the meter field with status=1, but if there is no status=1, The last field with status=0 will be selected.
//get latest row with status = 1
$unitMeter = DB::table('unit_meters')->where('status','=',1)->latest()->first();
if (!$unitMeter){// if is null
$unitMeter = DB::table('unit_meters')->where('status','=',0)->latest()->first();
}
if ($unitMeter){// if isn't null ( mean it's object )
$meter = $unitMeter->meter;
}
Also, add this in top of your controller:
use Illuminate\Support\Facades\DB;
I hope it helps!

how to get 2 table data into single variable array in laravel?

i have 2 tables User and subadmin consider user have 3 columns and subadmin has 2 column i want to get 3+2 (5 column) data into a single veriable array
the technique i want to use is that in user table i have id which is same in subadmin table with sub_admin_id(column) how i can use eloquent model to first link id with sub_admin_id and then into a single query get 5 column in single veriable array
here i am using to get data
$subadmindata = User::find($id)->get(); // [column1 ,column2 ,column3 ]
$subadmindata1 = SubAdmin::find($id)->get(); // [column1 ,column2 ]
output should be
$data = // [column1 ,column2 ,column3 , column4 ,column5 ]
note i dont want to use array merge or combine method i want to use eloquent model for my learning
you could use concat like this
$subadmindata = User::find($id)->get();
$subadmindata1 = SubAdmin::find($id)->get(); // it will return array of collections
$data = $subadmindata->concat($subadmindata1);
Notice when you use get after find it stop it's jobs so there is no need to find here
get() method will give you a collection not array, so you can merge two collection as follows.
$subadmindata = User::find($id)->get();
$subadmindata1 = SubAdmin::find($id)->get();
$data = $subadmindata->merge($subadmindata1);
You can't use find with get(Assuming that you need a one result not all of the users). Try this. But looks like you need build the relationships correctly first. Anyway, quick answer is below.
$userCols = User::select('column1','col2','col3')->find($id);
$subAdminCols = SubAdmin::select('col4','col5')->find($id);
$cols = array_merge($userCols->toArray(), $subAdminCols->toArray());
try this
in user model
public function subadmin(){
return $this->hasOne(SubAdmin::class,'sub_admin_id');
}
in controller
$sub_admins = User::find($id);
dd($sub_admins->subadmin->sub_admin_id)
you can use php ... operator to to push data
example like this
$subadmindata = User::find($id)->get()->toArray(); // [column1 ,column2 ,column3 ]
$subadmindata1 = SubAdmin::find($id)->get()->toArray(); // [column1 ,column2 ]
array_push($subadmindata, ...$subadmindata1);
return $subadmindata
ref link https://wiki.php.net/rfc/spread_operator_for_array

Page views mysql

Codeigniter I want to increase +1 when loading page. but the query does 0
'views + 1' and I did 10 no problem.
It didn't make sense to throw the query for views.
function get_views()
{
$this->db->set('views', 'views+1');
$this->db->where('Id', 1);
$this->db->update('pages'); // gives UPDATE mytable SET field = field+1 WHERE id = 2
}
You need to pass a third parameter as false like below:
$this->db->set('views', 'views+1', FALSE);
The Third Parameter tells CodeIgniter not to protect the generated query with backticks. In your case the final query will be
UPDATE pages SET views = views + 1 WHERE Id = '1';

how to get id from one table an add another table in laravel 4

I am try to add id from one table to another table to give a relationship to the tables.
i have 2 table ..
classes.(id,cls,divn)
id cls divn
1 1 A
2 1 B
4 2 A
5 2 B
6 2 C
7 3 A
2.teacher(id teachername,tclsid,tsub)
Now i want ,im enter teachername and select class,division ,subject on select box using html tag.
if im select class and division check to classes table.and get id from selected class & division to insert into teacher table tclsid feld.
how its possible??
im trying to select multiple class and division
im using controller.php is here
$clss =implode(',', Input::get('tcls'));
$divn =implode(',', Input::get('tdivn'));
$teachers =new ForumTeacher();
$teachers ->tname=Input::get('tname');
$teachers->tmobile=Input::get('tmobile');
$teachers ->ttype=Input::get('ttype');
$teachers ->tsubject = implode(',', Input::get('tsubject'));
$cs = ForumClass::where('cls', $clss)->where('divn', $divn )->first();
$teachers->tcls= $cs->id;
but not work this code $teachers->tcls= $cs->id;` ...
i want teacher table like this
id teachername tclsid tsub
1 xyz 1,5 maths
2 poq 5,7 english
if xyz teacher select class &division are 1,2 & A,B(so id is 1,5)
if poq teacher select class &division are 2,3 & B,A (so id is 5,7)
but i got an error
ErrorException (E_UNKNOWN)
Trying to get property of non-object
error code line is
$teachers->tcls= $cs->id;
Your $teachers var is holding a new, unsaved ForumTeacher model. You have not assigned the tcls value in $teachers. So the $teachers['tcls'] is not set, Add:
$teachers->tcls = $cs->id;
I don't understand what your are trying to do with:
$clsid[$cs->id] = $teachers['tcls'];
If you set the tcls attribute of $teachers first, what you are doing is the same as:
$clsid[$cs->id] = $cs->id;
Don't forget to save the model when you are done setting its attributes:
$teachers->save();
Get id from one table an add another table in laravel 4
$cs = ForumClass::where('cls','=', $clss)->where('divn','=', $divn )->first();
$teachers->tcls= $cs->id;
Reread your question and code and from what I understand you are getting multiple class, division combinations and subject for multiple teachers. You either have to have one teacher and one cls and one divn and one subject or the same number of these corresponding to different teachers. Otherwise, I am confused by your model, so I am assuming you have one of each per teacher instance.
Then you need to iterate over the inputs and create one teacher at a time or create an array and batch create them. I'll do the iteration for simplicity of following it:
// assuming all input arrays have the same count() of elements and
// are all in the same order
$tclss = Input::get('tcls');
$tdivns = Input::get('tdivn');
$tnames = Input::get('tname');
$tmobiles = Input::get('tmobile');
$ttypes = Input::get('ttype');
$tsubjects = Input::get('tsubject');
$iMax = count($tclss);
for ($i = 0; $i < $iMax; $i++)
{
$tcls = ForumClass::where('cls', $tclss[$i])->where('divn', $tdivns[$i])->get();
if (is_null($tcls))
{
// cls, divn combination is missing from your table
// if you want to add it then do so or handle the error
$tcls = new ForumClass();
$tcls->cls = $tclss[$i];
$tcls->divn = $divns[$i];
$tcls->Save();
}
$teacher = new ForumTeacher();
$teacher->tname = $tnames[$i];
$teacher->tmobile = $tmobiles[$i];
$teacher->ttype = $ttypes[$i];
$teacher->tsubject = $tsubjects[$i];
$teacher->tcls = $tcls->id;
$teacher->save();
}
I did not test this but you get the idea of what to do.

Resources