Laravel Query Builder fetch multiple table - laravel

$name = $_POST['name'];
$statement = $db->prepare("SELECT * FROM authors WHERE name = ?");
$statement->execute(array($name));
$author_id = $statement->fetch()["id"];
$statement = $db->prepare("SELECT * FROM quotes WHERE author_id = ?");
$statement->execute(array($author_id));
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as $row) {
$quote = $row['quote'];
}
How can i do same operation in laravel query builder ?

You can try like this:
$name = $_POST['name'];
$where[] = ['authors.name ', $name];
$data = \DB::table('authors')
->join('quotes', 'quotes.author_id', '=', 'authors.id')
->where($where)
->get();

Related

Get customer ID using Mage::getModel('customer/customer')

I not able to get the customersID am I calling it correctly with Mage::getModel
$customer = Mage::getModel('customer/customer')
->setWebsiteId(2)
->load($this->getRequest()->getParam('customer'));
$customer_id = $customer->getId();
$sku = 'BOX3151';
$sql = "SELECT sfoi.item_id FROM sales_flat_order_item sfoi INNER JOIN sales_flat_order sfo
ON sfo.entity_id = sfoi.order_id WHERE sfo.customer_id = '$customer_id' AND sfoi.sku = '$sku'";
$read_db = Mage::getSingleton('core/resource')->getConnection('core_read');
$result = $read_db->fetchAll($sql);
$total_rows = count($result);
if($total_rows >= 1):
echo ' Found ';
else:
echo ' Empty ';
endif;
endif;
Please check for available data in
$this->getRequest()->getParam('customer')
if you have $customerId there you can use the load() method.
$customer = Mage::getModel('customer/customer')
->setWebsiteId(2)
->load($customerId)
also you can get customer object by $email.
$customer = Mage::getModel('customer/customer')
->setWebsiteId(2)
->loadByEmail($email);
<?php
$email = "customer#gmail.com";
$customer = Mage::getModel("customer/customer")->setWebsiteId(Mage::app()->getWebsite()->getId())->loadByEmail($email);
//use
echo $customer->getId();
echo $customer->getEmail();
echo $customer->getFirstname();
?>

For loop request Laravel

Just a quick question guys really thankful for everyone who can help.
How do I foreach loop inside create eloquent in laravel
Here is my code. I'm trying to get and match the answers for their respective survey questions
public function answer_survey(Request $request, $id)
{
$userId = Auth::user()->id;
$user = User::where('id', $userId)->first();
$userNectarPoint = UserNectarPoint::where('user_id', $userId)->first();
$survey = Survey::find($id);
$survey_questions = SurveyQuestion::where('survey_id', $survey->id)->get();
$current_nectarpt_value = $userNectarPoint->nectar_points;
$questionIds = $request->id;
$questions = $request->question;
$answers = $request->answer;
foreach ($answers as $answer) {
}
foreach ($survey_questions as $survey_question) {
$userAnsweredSurvey = new UserAnsweredSurvey;
$userAnsweredSurvey->user_id = $userId;
$userAnsweredSurvey->user_fullname = $user->first_name . ' ' . $user->middle_initial . ' ' . $user->family_name;
$userAnsweredSurvey->survey_id = $survey->id;
$userAnsweredSurvey->survey_title = $survey->title;
$userAnsweredSurvey->survey_question_id = $survey_question->id;
$userAnsweredSurvey->survey_question = $survey_question->question;
$userAnsweredSurvey->answer = $answer;
$userAnsweredSurvey->save();
}
$result_nectarpt = $current_nectarpt_value + $survey->reward_points;
$userNectarPoint->nectar_points = $result_nectarpt;
$userNectarPoint->save();
return back()->with('status', 'survey_done');
}

How can I check if user is in usergroup "Friends" in Joomla 3?

how can I check, if current user is in usergroup called for example "Friends" (title) in Joomla 3?
$user = JUser::getInstance($result->id);
$groups = $user->getAuthorisedGroups(); // just IDs, not title
Thanks.
Check this code, I hope it will help you.
$AllGroups = $user->get('groups');
// getting all the groups
foreach($AllGroups as $groupId) {
$user = JFactory::getUser(); // getting user data
$db = JFactory::getDBO();
$userid = $user->get('id'); // logged in user id
$groups = JAccess::getGroupsByUser($userid);
$groupid_list = '(' . implode(',', $groups) . ')';
$query = $db->getQuery(true);
$query->select('id, title');
$query->from('#__usergroups');
$query->where('id IN ' .$groupid_list);
$db->setQuery($query);
$rows = $db->loadRowList();
$grouplist = '';
foreach($rows as $group)
{
if ($groupId == $group[0]) { // checking the logged in user group
$grouplist = $group[1];
}
}
var_dump($grouplist); // it will give you the usergroup name
}
$user = JUser::getInstance($result->id);
$AllGroups = $user->get('groups');
$groupNames = array();
$userid = $user->get('id'); // logged in user id
$groups = JAccess::getGroupsByUser($userid);
$groupid_list = '(' . implode(',', $groups) . ')';
foreach ($AllGroups as $groupId) {
$query = $db->getQuery(true);
$query->select('id, title');
$query->from('#__usergroups');
$query->where('id IN ' . $groupid_list);
$db->setQuery($query);
$rows = $db->loadRowList();
$grouplist = '';
foreach ($rows as $group) {
if ($groupId == $group[0]) { // checking the logged in user group
$grouplist = $group[1];
}
}
array_push($groupNames, $grouplist);
}
$query = $db->getQuery(true)
->select('*')
->from('#__myTable');
if (in_array("Friends", $groupNames)) {
// User is in group "Friends"
}

Magento - Get customer gender from order query

I have made a query that gets customer's orders. Can I get customer gender based on this query? If not, how can I get customer gender? This is the code I have so far:
<?php
require_once('app/Mage.php'); //Path to Magento
umask(0);
Mage::app();
$productId = 297;
$orders = Mage::getModel('sales/order')->getCollection();
$orders
->getSelect()
->joinLeft(
array(
'order_item' =>Mage::getSingleton('core/resource')->getTableName('sales/order_item')),
'order_item.order_id = main_table.entity_id',
array('product_id'=>'product_id'))
->where('product_id=?', $productId)
->group('main_table.entity_id')
->order('main_table.entity_id DESC');
$fields = "date,company,name,country,city,address,postcode,itemname,quantity,price,sku \n";
foreach($orders as $order):
$order->getData();
$order->getIncrementId();
$billingAddress = $order->getBillingAddress();
$shippingAddress = $order->getShippingAddress();
$billingStreet = $billingAddress->getStreet();
$countryCode = $billingAddress->getCountry();
$country = Mage::getModel('directory/country')->loadByCode($countryCode);
$fields .=
date('Y-m-d',strtotime($order->getCreatedAt())).','.
$billingAddress->getCompany().','.
$billingAddress->getName().','.
$country->getName().','.
$billingAddress->getCity().','.
$billingStreet[0].','.
$billingAddress->getPostcode().','.
$order->getShippingDescription() .',';
foreach ($order->getAllItems() as $itemId => $item):
$itemname = $item->getName();
$itemname = str_replace('&', " ", $itemname);
$fields .=
$itemname. ','.
$item->getQtyOrdered().','.
$item->getPrice().','.
$item->getSku().',';
endforeach;
$fields = rtrim($fields, ',');
$fields .= "\n";
endforeach;
$name = date('d-m-Y-H-i-s');
$output = fopen('backend/assets/csv/' . $name . '.csv', 'w');
fwrite ($output,$fields);
fclose ($output);
foreach($orders as $order){
$customer = $order->getCustomer();
Mage::log($customer->getGender());
}
if this does not work then:
foreach($orders as $order){
$customer = $order->getCustomer();
$customerObj = Mage::getModel('customer/customer')->load( $customer->getEntityId());
Mage::log($customerObj->getGender());
}

How to get parameters in JFormField from a disabled plugin in Joomla 1.6/2.5?

How i can get some parameters from a disabled/not yet actived plugin in joomla 1.6/2.5?
$module = JPluginHelper::getPlugin('system','myplugin');
$moduleParams = new JParameter($module->params);
$val = $moduleParams->get("key");
This method didn't work becouse i need to use within an element JFormField generator.
Thanks for help!
With JPluginHelper::getPlugin it's possible to access only enabled plugins, so here's the code for direct access to database.
// Build query
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
->select( 'params' )
->from( '#__extensions' )
->where( 'type = ' . $db->q('plugin') )
->where( 'folder = ' . $db->q('authentication') ) // Plugin type
->where( 'element = ' . $db->q('gmail') ) // Plugin element
;
// Execute query
$db->setQuery($query);
try
{
$result = $db->loadResult();
}
catch (RuntimeException $e)
{
return false;
}
// Parse parameters
if (!empty($result))
{
$params = new JRegistry($result);
$val = $params->get('key', 'defaultValue');
}
You may store query results in in the JFormField Object so save database queries in case field is availabile multiple times.
protected $results = null;
Perhaps you may want to try this:
// Get plugin parameters
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('`params`')
->from ('`#__extensions`')
->where ("`type` = 'plugin'")
->where ("`folder` = 'system'")
->where ("`element` = 'myplugin'");
$db->setQuery($query);
$res = json_decode($db->loadResult(), true);
$val = $res['key'];
Just find the answer by myself.
$data = null;
foreach ((array) $this->form as $k => $v) {
if($val instanceof JRegistry){
$data = &$v;
break;
}
}
$data = $data->toArray();
$val = $data['params']['key'];
Thanks! Bye!

Resources