array inside array in codeigniter - codeigniter

I am trying to put the integer value of $QQ array into presenter array, no luck please help.
$QQ = $this->MAudio->getAllAudio();
$presenter = $this->Profile_model->getProfile($QQ['a_presenter_id']);
$data['name'] = $presenter['name'];

I think issue is with $this->MAudio->getAllAudio(); I think u have return Object now u got to convert that object into value by calling row() or row_array() or result() or result_array()
Try this code
$QQ = $this->MAudio->getAllAudio()->row_array();
$presenter = $this->Profile_model->getProfile($QQ['a_presenter_id']);
$data['name'] = $presenter['name'];

Related

How to turn a collection into an array in laravel

I'm not sure if my title is correctly done, but what I'm trying to do is get all the notifications that a user didn't read.
I have 2 tables the first table is notifications and the second one is read_notifications.
Here is my code in User.php model
$read = DB::table('read_notifications')->select('notification_id')->where('user_id', $this->id)->get();
$unread = Notification::whereNotIn('id', $read)->get();
Here I'm getting all the notification IDs in the read_notifications table and I want to put them in the $unread statement.
The error I get when I do this is
Object of class stdClass could not be converted to string
Notification::whereNotIn('id', $read)->get();
$read is an object and id should be a string (or integer). You should use an id field from your $read object like $read[$i]->id.
Also whereNotIn needs an array as a second argument, so you need to collect your IDs from $read to an array like [1,2,3].
Thanks for helping. I found what I needed, I needed to use the pluck method.
Here is the updated code
$read = DB::table('read_notifications')->select('notification_id')->where('user_id', $this->id)->pluck('notification_id');
$unread = Notification::whereNotIn('id', $read)->get();
for whereNotIn to work $read should be an array.
$read = DB::table('read_notifications')->select('notification_id')->where('user_id', $this->id)->get();
foreach($read as $re)
$ot[] = $re->notification_id;
$unread = Notification::whereNotIn('id', $ot)->get();
OR you can use pluck
$read = DB::table('read_notifications')->where('user_id', $this->id)->pluck('notification_id');
$unread = Notification::whereNotIn('id', $read)->get();

How to get string value using eloquent query

I am trying to execute a query using eloquent, however it returns me an array. What I want is to only get the string value of my query.
This is what I get
[{"name":"hey"}] [{"name":"sdasdasd"}]
Here's my eloquent query:
$categoryName = Category::select('name')->where('id', request('category'))->get();
$subCategory = Subcategory::select('name')->where('id', request('subCat'))->get();
as per the Laravel Docs
Retrieving A Single Row / Column From A Table
If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single stdClass object:
$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;
If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
so in your example maybe you need to do something like the following :
$subCategory = Subcategory::select('name')->where('id', request('subCat'))-> first();
echo $subCategory->name;
There are so many ways to achive your goal.
Solutions
Use pluck().
$categoryName = Category::select('name')
->where('id', request('category'))
->pluck()
->first();
Use model properties.
$categoryName = Category::find(request('category'))
->name;
If you take only name then follow the below code.
$categoryName = Category::where('id', request('category'))->first()->name;
$subCategory = Subcategory::where('id', request('subCat'))->first()->name;
echo $categoryName;
echo $subCategory;
If you take all columns in a row follow the below code:
$categoryName = Category::where('id', request('category'))->first();
$subCategory = Subcategory::where('id', request('subCat'))->first();
echo $subCategory->name;
echo $categoryName->name;
I hope this i may help you.Try this.
You can get the answer using
$name = DB::table('Category')->where('id','=',request('category'))->pluck('name')->first();
echo $name;
$name = Category::select('name')->where('id', request('category'))->take(1)->orderBy('id','asc')->first()->name;
echo $name;
OR
echo Category::select('name')->where('id', request('category'))->take(1)->orderBy('id','asc')->first()->name;

how to store the query result into an array in codeigniter

this is my model code.i have to store these values into an array.someone please help with it.
$this->db->select('value,T_Id,T_MId,T_DID,T_Date,T_UOM');
$this->db->from('mapping,main');
$this->db->where('main.Id=mapping.S_ID');
$this->db->where('main.MId=mapping.S_MId');
$this->db->where('main.DID=mapping.S_DID');
$this->db->where('main.Date=mapping.S_Date');
$this->db->where('main.UOM=mapping.S_UOM');
$qu1 = $this->db->get();
$n=$qu1->num_rows();
echo $n;
$a = $qu1->result_array();
print_r($a);
You have to add return statement at the end of this code ,Please check the code:
public function YourFunctionName(){
$arrReturn = array();//Declare the array to be passed
$this->db->select('value,T_Id,T_MId,T_DID,T_Date,T_UOM');
$this->db->from('mapping,main');
$this->db->where('main.Id=mapping.S_ID');
$this->db->where('main.MId=mapping.S_MId');
$this->db->where('main.DID=mapping.S_DID');
$this->db->where('main.Date=mapping.S_Date');
$this->db->where('main.UOM=mapping.S_UOM');
$qu1 = $this->db->get();
$result = $qu1->result_array();
if(!empty($result)){
$arrReturn = $result;
}
return $arrReturn;
}
In this case if data which will be available from query will be passed to place where it is called from.
And in case no data are found then blank array will be passed.
Hope this helps you.

How do i get php object from json?

this case Google api v3 calendar
$array = $client->getAccessToken();
dump $array
$array = { ["access_token"]=>"i81bIkE" ["token_type"]=>"Bearer" ["expires_in"]=>3600 ["refresh_token"]=>"G3LQL_cQ" ["created"]=> 1495019807 }
i want to get ["refresh_token"] value.
$refresh_token = $array -> refresh_token;
i try this code ,but error
$token_array = json_decode($array);
error message
Warning: json_decode() expects parameter 1 to be string, array given in...
plese help ...
#Andy, as this tell you, you can't use this with array.
did you use:
echo $array['refresh_token'];
json_decode is a PHP method with string param. You try to pass an array.
refresh_token is a string. You can do :
$refresh_token = $array['refresh_token'];
to get it.

How do I select a single row in Magento in a custom database for display in a block?

I don't want to use foreach to loop through an array of multiple rows, as I am planning on only displaying only one row and using a variable. I can't find information for this online.
What doesn't work
$param = $this->getRequest()->getParam('manufacturer');
$extrabrand = Mage::getModel('brands/brands')->getCollection();
$extrabrand->addFieldToFilter('attributelabelid', $param);
//$extrabrand->setAttributelabelid($param);
$extrabrand->load();
Fatal error: Call to undefined method
Desbest_Brands_Model_Mysql4_Brands_Collection::getDescription() in
/home/desbest/public_html/clients/magentofull/app/design/frontend/default/default/template/Desbest_Brands/brand_info.phtml
on line 20
Plus there is no EAV.
Without seeing the code in brand_info.phtml it's hard to say what the problem is, but my guess is you're using the collection in $extrabrand as though it were a model. Try this instead
//get the parameter from the request
$param = $this->getRequest()->getParam('manufacturer');
//instantiate the brand/brand model, and use
//its `getCollection` method to return a collection
//object
$collection = Mage::getModel('brands/brands')->getCollection();
//add the paramater as a filter
$collection->addFieldToFilter('attributelabelid', $param);
//get the first item of the collection (load will be called automatically)
$extrabrand = $collection->getFirstItem();
//look at the data in the first item
var_dump($extrabrand->getData());
If you need to get only 1 element (first) from the collection, use current() function:
$param = $this->getRequest()->getParam('manufacturer');
$extrabrandCollection = Mage::getModel('brands/brands')->getCollection()
->addFieldToFilter('attributelabelid', $param);
$extrabrand = current($extrabrandCollection->getItems());

Resources