laravel where if get a single value - laravel

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!

Related

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

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

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 Use Where clause in get API

I am creating API to Get Data from Table.
My table is Tbl_Menu.
Id MenuName Status
-----------------------------------------------------
1 idli True
2 Utappa false
3 MeduWada True
i want data in API GET having only Status = true.
My Get API is
public IEnumerable<Tbl_Menu> GetTbl_Menu()
{
var tbl_menu = db.Tbl_Menu;
return tbl_menu.AsEnumerable();
}
I have to use Where Clause.
so what changes I suppose to do.
check this.
var tbl_menu = db.Tbl_Menu;
return tbl_menu.Where(t=> t.Status == true).ToList();

LINQ to dataset: CopyToDataTable()

I want to query a datatable (dt) and load a 2nd dt with the resultant collection of datarows. Fine - we have the CopyToDataTable() extn mthd for exactly that purpose. However it is constrained to enumerate only over DataRows, which means that I cannot return anything else e.g. a collection of anonymous types. So - how can I modify the values in the datarows?
Eg I have a dt with 3 columns: MyPK, VARCHAR01, VARCHAR02.
Foreach row, if VARCHAR01 or VARCHAR02 has the value "" (i.e. String.Empty) I want to replace that with NULL (which the underlying type allows).
I would do this as follows:
var MyCleanedDatarows =
from o in ds.Tables["dt"].AsEnumerable()
select new {
MyPK = o.Field<string>("MyPK"),
VARCHAR01 = (o.Field<string?>("VARCHAR01") == "" ? NULL : o.Field<string?>("VARCHAR01") ),
VARCHAR02 = (o.Field<string?>("VARCHAR02") == "" ? NULL : o.Field<string?>("VARCHAR02") )
};
...but then I cant use CopyToDataTable() to get back to a dt. I'm thinking I need to modify the datarows before invoking select operator, but I dont know how to achieve that. Any help/thoughts would be v.greatfully recieved.
Thanks in advance,
Tamim.
Take a look at this approach, in MSDN documentation.
http://msdn.microsoft.com/en-us/library/bb669096.aspx

Resources