Cache not delivering? - magento

I recently updated to magento 1.9 but found my categories very slow. I couldn't find the problem so guessed this had to do with magento 1.9 and decided to downgrade to magento 1.8. Everything was working fine then, but after a while magento became slower again. I had made some changes due to some other errors, as I slightly changed /etc/local.xml. Obviously, I undid it again, even though it should not have been the reason in the first place. Unfortunately, this didn't solve my problem.
My current local.xml:
<cache>
<backend>Cm_Cache_Backend_File</backend>
<prefix>SH24_</prefix>
</cache>
Cm Cache Backend is definitely working. Same results when using standard cache.
I turned on the profiler, and how files are delivered looks completely different on a different development page.
http://i.imgur.com/fnLRheX.png
(Slow Page)
This is how it looks like on another test development site:
http://i.imgur.com/5lwHxH2.png
(Fast Page)
Generating difference is 6 seconds.
As you can tell, in the slow site, everything is nested under layout/db_updates, but on the slow site, it's not.
The most time consuming call is
CONFIGURABLE:Mage_Catalog_Model_Product_Type_Configurable::getConfigurableAttributes
which is not called on the fast site. Instead it's just:
__EAV_LOAD_MODEL__
In var/cache files appear. Error Log doesn't log anything.
Does anyone know, how I could solve this?
Thank you very much!
EDIT:
What I have found so far.
/Mage/Core/Model/Layout/update.php
public function fetchDbLayoutUpdates($handle)
{
$_profilerKey = 'layout/db_update: '.$handle;
Varien_Profiler::start($_profilerKey);
Mage::log($_profilerKey, null, 'mylogfile.log');
$updateStr = $this->_getUpdateString($handle);
if (!$updateStr) {
return false;
}
$updateStr = '<update_xml>' . $updateStr . '</update_xml>';
$updateStr = str_replace($this->_subst['from'], $this->_subst['to'], $updateStr);
$updateXml = simplexml_load_string($updateStr, $this->getElementClass());
$this->fetchRecursiveUpdates($updateXml);
$this->addUpdate($updateXml->innerXml());
Varien_Profiler::stop($_profilerKey);
return true;
}
As expected is $updateStr empty and the call returns falls.
So I had a closer look on $this->_getUpdateString($handle):
return Mage::getResourceModel('core/layout')->fetchUpdatesByHandle($handle);
I found fetchUpdatesByHandle($handle); in /Mage/Core/Model/Resource/Layout.php:
public function fetchUpdatesByHandle($handle, $params = array())
{
$bind = array(
'store_id' => Mage::app()->getStore()->getId(),
'area' => Mage::getSingleton('core/design_package')->getArea(),
'package' => Mage::getSingleton('core/design_package')->getPackageName(),
'theme' => Mage::getSingleton('core/design_package')->getTheme('layout')
);
foreach ($params as $key => $value) {
if (isset($bind[$key])) {
$bind[$key] = $value;
}
}
$bind['layout_update_handle'] = $handle;
$result = '';
$readAdapter = $this->_getReadAdapter();
if ($readAdapter) {
$select = $readAdapter->select()
->from(array('layout_update' => $this->getMainTable()), array('xml'))
->join(array('link'=>$this->getTable('core/layout_link')),
'link.layout_update_id=layout_update.layout_update_id',
'')
->where('link.store_id IN (0, :store_id)')
->where('link.area = :area')
->where('link.package = :package')
->where('link.theme = :theme')
->where('layout_update.handle = :layout_update_handle')
->order('layout_update.sort_order ' . Varien_Db_Select::SQL_ASC);
$result = join('', $readAdapter->fetchCol($select, $bind));
}
return $result;
}
$result is expected an empty string.
$select looks like this:
2014-06-22T13:52:12+00:00 DEBUG (7): handle CATEGORY_56
query
object(Varien_Db_Select)#295 (4) {
["_bind":protected]=>
array(0) {
}
["_adapter":protected]=>
object(Magento_Db_Adapter_Pdo_Mysql)#80 (31) {
["_defaultStmtClass":protected]=>
string(29) "Varien_Db_Statement_Pdo_Mysql"
["_transactionLevel":protected]=>
int(0)
["_connectionFlagsSet":protected]=>
bool(true)
["_ddlCache":protected]=>
array(1) {
[1]=>
array(2) {
["catalog_product_flat_1"]=>
array(56) {
["entity_id"]=>
array(14) {
["SCHEMA_NAME"]=>
NULL
["TABLE_NAME"]=>
string(22) "catalog_product_flat_1"
["COLUMN_NAME"]=>
string(9) "entity_id"
["COLUMN_POSITION"]=>
int(1)
["DATA_TYPE"]=>
string(3) "int"
["DEFAULT"]=>
NULL
["NULLABLE"]=>
bool(false)
["LENGTH"]=>
NULL
["SCALE"]=>
NULL
["PRECISION"]=>
NULL
["UNSIGNED"]=>
bool(true)
["PRIMARY"]=>
bool(true)
["PRIMARY_POSITION"]=>
int(1)
["IDENTITY"]=>
bool(false)
}
["attribute_set_id"]=>
array(14) {
["SCHEMA_NAME"]=>
NULL
["TABLE_NAME"]=>
string(22) "catalog_product_flat_1"
["COLUMN_NAME"]=>
string(16) "attribute_set_id"
["COLUMN_POSITION"]=>
int(2)
["DATA_TYPE"]=>
string(8) "smallint"
["DEFAULT"]=>
string(1) "0"
["NULLABLE"]=>
bool(false)
["LENGTH"]=>
NULL
["SCALE"]=>
NULL
["PRECISION"]=>
NULL
["UNSIGNED"]=>
bool(true)
["PRIMARY"]=>
bool(false)
["PRIMARY_POSITION"]=>
NULL
["IDENTITY"]=>
bool(false)
}
["type_id"]=>
array(14) {
["SCHEMA_NAME"]=>
NULL
["TABLE_NAME"]=>
string(22) "catalog_product_flat_1"
["COLUMN_NAME"]=>
string(7) "type_id"
["COLUMN_POSITION"]=>
int(3)
["DATA_TYPE"]=>
string(7) "varchar"
["DEFAULT"]=>
string(6) "simple"
["NULLABLE"]=>
bool(false)
["LENGTH"]=>
string(2) "32"
["SCALE"]=>
NULL
["PRECISION"]=>
NULL
["UNSIGNED"]=>
NULL
["PRIMARY"]=>
bool(false)
["PRIMARY_POSITION"]=>
NULL
["IDENTITY"]=>
bool(false)
}
["cost"]=>
array(14) {
["SCHEMA_NAME"]=>
NULL
["TABLE_NAME"]=>
string(22) "catalog_product_flat_1"
["COLUMN_NAME"]=>
string(4) "cost"
["COLUMN_POSITION"]=>
int(4)
["DATA_TYPE"]=>
string(7) "decimal"
["DEFAULT"]=>
NULL
["NULLABLE"]=>
bool(true)
["LENGTH"]=>
NULL
["SCALE"]=>
string(1) "4"
["PRECISION"]=>
string(2) "12"
["UNSIGNED"]=>
NULL
["PRIMARY"]=>
bool(false)
["PRIMARY_POSITION"]=>
NULL
["IDENTITY"]=>
bool(false)
}
["created_at"]=>
array(14) {
["SCHEMA_NAME"]=>
NULL
["TABLE_NAME"]=>
string(22) "catalog_product_flat_1"
["COLUMN_NAME"]=>
string(10) "created_at"
["COLUMN_POSITION"]=>
int(5)
["DATA_TYPE"]=>
string(9) "timestamp"
["DEFAULT"]=>
NULL
["NULLABLE"]=>
bool(true)
["LENGTH"]=>
NULL
["SCALE"]=>
NULL
["PRECISION"]=>
NULL
["UNSIGNED"]=>
NULL
["PRIMARY"]=>
bool(false)
["PRIMARY_POSITION"]=>
NULL
["IDENTITY"]=>
bool(false)
}
[MORE ATTRIBUTES]
}
}
}
["_bindParams":protected]=>
array(0) {
}
["_bindIncrement":protected]=>
int(0)
["_debug":protected]=>
bool(false)
["_logQueryTime":protected]=>
float(0.05)
["_logAllQueries":protected]=>
bool(false)
["_logCallStack":protected]=>
bool(false)
["_debugFile":protected]=>
string(23) "var/debug/pdo_mysql.log"
["_debugIoAdapter":protected]=>
NULL
["_debugTimer":protected]=>
int(0)
["_cacheAdapter":protected]=>
object(Varien_Cache_Core)#18 (6) {
["_specificOptions":protected]=>
array(1) {
["slab_size"]=>
int(0)
}
["_backend":protected]=>
object(Cm_Cache_Backend_File)#10 (4) {
["_options":protected]=>
array(11) {
["cache_dir"]=>
string(41) "/home/www/xxxx/magento/var/cache/"
["file_name_prefix"]=>
string(2) "cm"
["file_locking"]=>
bool(true)
["read_control"]=>
bool(false)
["read_control_type"]=>
string(5) "crc32"
["hashed_directory_level"]=>
int(2)
["use_chmod"]=>
bool(false)
["directory_mode"]=>
int(504)
["file_mode"]=>
int(432)
["hashed_directory_umask"]=>
int(504)
["cache_file_umask"]=>
int(432)
}
["_isTagDirChecked":protected]=>
NULL
["_metadatasArray":protected]=>
array(0) {
}
["_directives":protected]=>
array(3) {
["lifetime"]=>
int(7200)
["logging"]=>
bool(false)
["logger"]=>
NULL
}
}
["_options":protected]=>
array(9) {
["write_control"]=>
bool(true)
["caching"]=>
bool(true)
["cache_id_prefix"]=>
string(5) "xxx"
["automatic_serialization"]=>
bool(false)
["automatic_cleaning_factor"]=>
int(0)
["lifetime"]=>
int(7200)
["logging"]=>
bool(false)
["logger"]=>
NULL
["ignore_user_abort"]=>
bool(false)
}
["_lastId":"Zend_Cache_Core":private]=>
string(37) "XX_DB_PDO_MYSQL_DDL_eav_attribute_1"
["_extendedBackend":protected]=>
bool(true)
["_backendCapabilities":protected]=>
array(6) {
["automatic_cleaning"]=>
bool(true)
["tags"]=>
bool(true)
["expired_read"]=>
bool(true)
["priority"]=>
bool(false)
["infinite_lifetime"]=>
bool(true)
["get_list"]=>
bool(true)
}
}
["_isDdlCacheAllowed":protected]=>
bool(true)
["_ddlColumnTypes":protected]=>
array(13) {
["boolean"]=>
string(4) "bool"
["smallint"]=>
string(8) "smallint"
["integer"]=>
string(3) "int"
["bigint"]=>
string(6) "bigint"
["float"]=>
string(5) "float"
["decimal"]=>
string(7) "decimal"
["numeric"]=>
string(7) "decimal"
["date"]=>
string(4) "date"
["timestamp"]=>
string(9) "timestamp"
["datetime"]=>
string(8) "datetime"
["text"]=>
string(4) "text"
["blob"]=>
string(4) "blob"
["varbinary"]=>
string(4) "blob"
}
["_ddlRoutines":protected]=>
array(5) {
[0]=>
string(3) "alt"
[1]=>
string(3) "cre"
[2]=>
string(3) "ren"
[3]=>
string(3) "dro"
[4]=>
string(3) "tru"
}
["_tempRoutines":protected]=>
string(22) "#^\w+\s+temporary\s#im"
["_intervalUnits":protected]=>
array(6) {
["YEARS"]=>
string(4) "YEAR"
["MONTHS"]=>
string(5) "MONTH"
["DAYS"]=>
string(3) "DAY"
["HOURS"]=>
string(4) "HOUR"
["MINUTES"]=>
string(6) "MINUTE"
["SECOND"]=>
string(6) "SECOND"
}
["_queryHook":protected]=>
array(2) {
["object"]=>
object(Mage_Core_Model_Resource_Setup)#77 (9) {
["_resourceName":protected]=>
string(14) "livechat_setup"
["_resourceConfig":protected]=>
object(Mage_Core_Model_Config_Element)#124 (2) {
["setup"]=>
object(Mage_Core_Model_Config_Element)#401 (1) {
["module"]=>
string(14) "Zopim_Livechat"
}
["connection"]=>
object(Mage_Core_Model_Config_Element)#580 (1) {
["use"]=>
string(10) "core_setup"
}
}
["_connectionConfig":protected]=>
object(Mage_Core_Model_Config_Element)#35 (9) {
["host"]=>
string
["username"]=>
string
["password"]=>
string
["dbname"]=>
string(13)
["initStatements"]=>
string(14) "SET NAMES utf8"
["model"]=>
string(6) "mysql4"
["type"]=>
string(9) "pdo_mysql"
["pdoType"]=>
object(Mage_Core_Model_Config_Element)#580 (0) {
}
["active"]=>
string(1) "1"
}
["_moduleConfig":protected]=>
object(Mage_Core_Model_Config_Element)#70 (3) {
["active"]=>
string(4) "true"
["codePool"]=>
string(5) "local"
["version"]=>
string(5) "0.5.0"
}
["_callAfterApplyAllUpdates":protected]=>
bool(false)
["_conn":protected]=>
*RECURSION*
["_tables":protected]=>
array(0) {
}
["_setupCache":protected]=>
array(0) {
}
["_queriesHooked":protected]=>
bool(true)
}
["method"]=>
string(17) "callbackQueryHook"
}
["_pdoType":protected]=>
string(5) "mysql"
["_numericDataTypes":protected]=>
array(16) {
[0]=>
int(0)
[1]=>
int(1)
[2]=>
int(2)
["INT"]=>
int(0)
["INTEGER"]=>
int(0)
["MEDIUMINT"]=>
int(0)
["SMALLINT"]=>
int(0)
["TINYINT"]=>
int(0)
["BIGINT"]=>
int(1)
["SERIAL"]=>
int(1)
["DEC"]=>
int(2)
["DECIMAL"]=>
int(2)
["DOUBLE"]=>
int(2)
["DOUBLE PRECISION"]=>
int(2)
["FIXED"]=>
int(2)
["FLOAT"]=>
int(2)
}
["_config":protected]=>
array(13) {
["host"]=>
string
["username"]=>
string
["password"]=>
string
["dbname"]=>
string
["initStatements"]=>
string(14) "SET NAMES utf8"
["model"]=>
string(6) "mysql4"
["type"]=>
string(9) "pdo_mysql"
["pdoType"]=>
string(0) ""
["active"]=>
string(1) "1"
["charset"]=>
NULL
["persistent"]=>
bool(false)
["options"]=>
array(3) {
["caseFolding"]=>
int(0)
["autoQuoteIdentifiers"]=>
bool(true)
["fetchMode"]=>
int(2)
}
["driver_options"]=>
array(0) {
}
}
["_fetchMode":protected]=>
int(2)
["_profiler":protected]=>
object(Zend_Db_Profiler)#52 (4) {
["_queryProfiles":protected]=>
array(0) {
}
["_enabled":protected]=>
bool(false)
["_filterElapsedSecs":protected]=>
NULL
["_filterTypes":protected]=>
NULL
}
["_defaultProfilerClass":protected]=>
string(16) "Zend_Db_Profiler"
["_connection":protected]=>
object(PDO)#94 (0) {
}
["_caseFolding":protected]=>
int(0)
["_autoQuoteIdentifiers":protected]=>
bool(true)
["_allowSerialization":protected]=>
bool(true)
["_autoReconnectOnUnserialize":protected]=>
bool(false)
}
["_parts":protected]=>
array(12) {
["straightjoin"]=>
bool(false)
["distinct"]=>
bool(false)
["columns"]=>
array(1) {
[0]=>
array(3) {
[0]=>
string(13) "layout_update"
[1]=>
string(3) "xml"
[2]=>
NULL
}
}
["union"]=>
array(0) {
}
["from"]=>
array(2) {
["layout_update"]=>
array(4) {
["joinType"]=>
string(4) "from"
["schema"]=>
NULL
["tableName"]=>
string(18) "core_layout_update"
["joinCondition"]=>
NULL
}
["link"]=>
array(4) {
["joinType"]=>
string(10) "inner join"
["schema"]=>
NULL
["tableName"]=>
string(16) "core_layout_link"
["joinCondition"]=>
string(52) "link.layout_update_id=layout_update.layout_update_id"
}
}
["where"]=>
array(5) {
[0]=>
string(33) "(link.store_id IN (0, :store_id))"
[1]=>
string(23) "AND (link.area = :area)"
[2]=>
string(29) "AND (link.package = :package)"
[3]=>
string(25) "AND (link.theme = :theme)"
[4]=>
string(50) "AND (layout_update.handle = :layout_update_handle)"
}
["group"]=>
array(0) {
}
["having"]=>
array(0) {
}
["order"]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(24) "layout_update.sort_order"
[1]=>
string(3) "ASC"
}
}
["limitcount"]=>
NULL
["limitoffset"]=>
NULL
["forupdate"]=>
bool(false)
}
["_tableCols":protected]=>
array(0) {
}
}
Result
string(0) ""
The weird thing is, that in my fast test development, $result is also empty?
Really don't know what this all means!
EDIT2:
I noticed this when looking at the instantiated models using Magneto Debug:
MODELS
Resource Name Model Class Times Instantiated
catalog/category_flat Mage_Catalog_Model_Category 32
cms/page Mage_Cms_Model_Page 18
cms/block Mage_Cms_Model_Block 5
catalog/attribute Mage_Catalog_Model_Resource_Eav_Attribute 2
eav/entity_type Mage_Eav_Model_Entity_Type 2
core/url_rewrite Mage_Core_Model_Url_Rewrite 2
index/process Mage_Index_Model_Process 2
core/flag Mage_Catalog_Model_Product_Flat_Flag 1
customer/group Mage_Customer_Model_Group 1
But: catalog/product Mage_Catalog_Model_Product 9 is not shwon, even though there are displayed products, and which is instantiated in the fast shop.

Related

Laravel nested foreach returns duplicates

I'm trying to show all the user-owned courses.
The course details are showing okay, but when I try to get the user progress with a second foreach loop, it returns the same value for all of the courses.
NOTE: I can't get the progress from the first foreach because the progress is stored in the user_courses table and the rest of the course details such as the course name and description,are stored in the courses table.
Here's the code:
#foreach($user->courses as $course)
<div class="course-flex-item">
<div class="course-header">
<strong>{{ $course['title'] }}</strong>
</div>
<div class="course-description">
{{ $course['description'] }}
</div>
<div class="continue-button">
<button>Continue</button>
</div>
<div class="image-container">
<img class="course-image" src="https://www.w3.org/html/logo/downloads/HTML5_Badge_256.png" alt="fasdf">
#foreach($user->user_courses as $user_course)
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="70"
aria-valuemin="0" aria-valuemax="100" style="width:{{ $user_course['progress'] }}%;background-color: #f64c71;font-family:'Montserrat';font-weight:bold">
<span class="">{{ $user_course['progress'] }}%</span>
</div>
</div>
#endforeach
</div>
</div>
#endforeach
For example, if the user has 2 courses, one with 12% completion and the second with 30%, it'll show 30% for both of the courses.
var_dump($user_courses):
First course:
bject(App\User_Course)#1242 (27) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(13) "user__courses" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(6) ["user_id"]=> int(2) ["course_id"]=> int(2) ["progress"]=> string(2) "20" ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(6) ["user_id"]=> int(2) ["course_id"]=> int(2) ["progress"]=> string(2) "20" ["created_at"]=> NULL ["updated_at"]=> NULL } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
12%
object(App\User_Course)#1245 (27) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(13) "user__courses" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(8) ["user_id"]=> int(2) ["course_id"]=> int(3) ["progress"]=> string(2) "12" ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(8) ["user_id"]=> int(2) ["course_id"]=> int(3) ["progress"]=> string(2) "12" ["created_at"]=> NULL ["updated_at"]=> NULL } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
Second course:
bject(App\User_Course)#1242 (27) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(13) "user__courses" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(6) ["user_id"]=> int(2) ["course_id"]=> int(2) ["progress"]=> string(2) "20" ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(6) ["user_id"]=> int(2) ["course_id"]=> int(2) ["progress"]=> string(2) "20" ["created_at"]=> NULL ["updated_at"]=> NULL } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
12%
object(App\User_Course)#1245 (27) { ["guarded":protected]=> array(0) { } ["connection":protected]=> string(5) "mysql" ["table":protected]=> string(13) "user__courses" ["primaryKey":protected]=> string(2) "id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true) ["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { } ["perPage":protected]=> int(15) ["exists"]=> bool(true) ["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(6) { ["id"]=> int(8) ["user_id"]=> int(2) ["course_id"]=> int(3) ["progress"]=> string(2) "12" ["created_at"]=> NULL ["updated_at"]=> NULL } ["original":protected]=> array(6) { ["id"]=> int(8) ["user_id"]=> int(2) ["course_id"]=> int(3) ["progress"]=> string(2) "12" ["created_at"]=> NULL ["updated_at"]=> NULL } ["changes":protected]=> array(0) { } ["casts":protected]=> array(0) { } ["classCastCache":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { } ["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["timestamps"]=> bool(true) ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["fillable":protected]=> array(0) { } }
#foreach($user->courses as $course)
<div class="course-flex-item">
<div class="course-header">
<strong>{{ $course['title'] }}</strong>
</div>
<div class="course-description">
{{ $course['description'] }}
</div>
<div class="continue-button">
<button>Continue</button>
</div>
<div class="image-container">
<img class="course-image" src="https://www.w3.org/html/logo/downloads/HTML5_Badge_256.png" alt="fasdf">
#if(is_array($course)
#foreach($course as $user_course)
<div class="progress">
<div class="progress-bar" role="progressbar" aria-valuenow="70"
aria-valuemin="0" aria-valuemax="100" style="width:{{ $user_course['progress'] }}%;background-color: #f64c71;font-family:'Montserrat';font-weight:bold">
<span class="">{{ $user_course['progress'] }}%</span>
</div>
</div>
#endforeach
#endif
</div>
</div>
#endforeach
Eliminate second foreach. Then get progres like this
{{ $course->user_course->where('user_id', $user->id)->first() != null ? $course->user_course->where('user_id', $user->id)->first()->progress : 0}}

How to store auntheticated username into a database field

I have a clinician field in my treatments table. I want the authenticated user to be stored in the clinician field of the table.
The field in the database migration is as
$table->string('clinician')->nullable();
In my TreatmentController, I have thisFacade
use Illuminate\Support\Facades\Auth;
And this function
public function store (Patient $patient)
{
Treatment::create([
'patient_id'=>$patient->id,
'clinician'=> Auth::user()->username,
'treatmentDetails' => request('treatmentDetails')
]);
return back()->with('success','Treatment saved!.');
}
I
In the Treatment model i have this.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Treatment extends Model
{
//
protected $guarded = [];
public function patients()
{
return $this->belongsTo('App\Patient');
}
}
var_dump(Auth::user()); returns this
object(App\User)#615 (27) { ["fillable":protected]=> array(3) { [0]=> string(8)
"username" [1]=> string(5) "email" [2]=> string(8) "password" }
["hidden":protected]=> array(2) { [0]=> string(8) "password" [1]=> string(14)
"remember_token" } ["connection":protected]=> string(5) "mysql"
["table":protected]=> string(5) "users" ["primaryKey":protected]=> string(2)
"id" ["keyType":protected]=> string(3) "int" ["incrementing"]=> bool(true)
["with":protected]=> array(0) { } ["withCount":protected]=> array(0) { }
["perPage":protected]=> int(15) ["exists"]=> bool(true)
["wasRecentlyCreated"]=> bool(false) ["attributes":protected]=> array(9) {
["id"]=> int(1) ["email"]=> string(16) "hans10#gmail.com"
["email_verified_at"]=> NULL ["password"]=> string(60)
"$2y$10$3KfEDTP1Vf014KZyvvH4Nu1thiLQtfKxz6Jo7wFpqAoKMg6SSRb2G" ["username"]=>
string(5) "Karim" ["roleId"]=> int(2) ["remember_token"]=> NULL
["created_at"]=> string(19) "2019-02-16 13:00:59" ["updated_at"]=> string(19)
"2019-02-16 13:00:59" } ["original":protected]=> array(9) { ["id"]=> int(1)
["email"]=> string(16) "hans10#gmail.com" ["email_verified_at"]=> NULL
["password"]=> string(60)
"$2y$10$3KfEDTP1Vf014KZyvvH4Nu1thiLQtfKxz6Jo7wFpqAoKMg6SSRb2G" ["username"]=>
string(5) "Karim" ["roleId"]=> int(2) ["remember_token"]=> NULL
["created_at"]=> string(19) "2019-02-16 13:00:59" ["updated_at"]=> string(19)
"2019-02-16 13:00:59" } ["changes":protected]=> array(0) { }
["casts":protected]=> array(0) { } ["dates":protected]=> array(0) { }
["dateFormat":protected]=> NULL ["appends":protected]=> array(0) { }
["dispatchesEvents":protected]=> array(0) { } ["observables":protected]=>
array(0) { } ["relations":protected]=> array(0) { } ["touches":protected]=>
array(0) { } ["timestamps"]=> bool(true) ["visible":protected]=> array(0) { }
["guarded":protected]=> array(1) { [0]=> string(1) "*" }
["rememberTokenName":protected]=> string(14) "remember_token" }
If Karim is the user who is currently logged in, i want the name Karim to be inserted in clinician field. The treatment is stored successfully but with a null value in the clinician field.
Try with $fillable proprty in your model.
https://laravel.com/docs/5.7/eloquent

laravel: query get duplicate records laravel

when i write this query. i have a two records in shown in result
although the table has only one.
$side_messages = Message::orderBy('id' , 'desc')
->where('to_id' , Auth::id())->orWhere('from_id' , Auth::id())->first();
dd($side_messages);
The result:
object(Message)#412 (20) { ["fillable":protected]=> array(4) { [0]=> string(5) "to_id" [1]=> string(7) "from_id" [2]=> string(3) "msg" [3]=> string(4) "seen" } ["guarded":protected]=> array(0) { } ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(7) { ["id"]=> string(1) "1" ["from_id"]=> string(1) "1" ["to_id"]=> string(1) "2" ["msg"]=> string(18) "test_query" ["seen"]=> string(1) "0" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["original":protected]=> array(7) { ["id"]=> string(1) "1" ["from_id"]=> string(1) "1" ["to_id"]=> string(1) "2" ["msg"]=> string(18) "test_query" ["seen"]=> string(1) "0" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) }
It's one value not two but it's repeated for some reason one for #attributes:[] and the other for #original:[]
first method return a single model instance (Illuminate\Database\Eloquent\Collection).
If you need to debug how much row is returned, you can use
$count = Message::orderBy('id' , 'desc')->where('to_id' , Auth::id())->orWhere('from_id' , Auth::id())->count();
I think the result your posted is an instance of your Message model. So you can't be sure that it's returning duplicate record.
first method always return single instance of model. Not collection (multiple record) of model instance.
This dump You post is a model object, so it contains all the model settings. The thing that is important is the section
object(Message)#412 (20) { ["fillable":protected]=> array(4) { [0]=> string(5) "to_id" [1]=> string(7) "from_id" [2]=> string(3) "msg" [3]=> string(4) "seen" } ["guarded":protected]=> array(0) { } ["connection":protected]=> NULL ["table":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true) ["attributes":protected]=> array(7) { ["id"]=> string(1) "1" ["from_id"]=> string(1) "1" ["to_id"]=> string(1) "2" ["msg"]=> string(18) "test_query" ["seen"]=> string(1) "0" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["original":protected]=> array(7) { ["id"]=> string(1) "1" ["from_id"]=> string(1) "1" ["to_id"]=> string(1) "2" ["msg"]=> string(18) "test_query" ["seen"]=> string(1) "0" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19) "0000-00-00 00:00:00" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["morphClass":protected]=> NULL ["exists"]=> bool(true) }
For much cleaner dump execute this:
$side_messages = Message::orderBy('id' , 'desc')->where('to_id' , Auth::id())->orWhere('from_id' , Auth::id())->first();
dd($side_messages->toArray());
And show me where is second object?!

Laravel 4.2 oneToMany relationship issue

Despite having implemented many L4 relationships and them working most of the time. I am struggling and for some reason cannot see where I have gone wrong. It is almost certainly something obvious.
The relationship is that A task has one topic, topics have many tasks.
Task Model
class Task extends Eloquent {
protected $guarded = array();
public static $rules = array(
);
public function resources()
{
return $this->belongsToMany('Resource');
}
public function topic()
{
return $this->belongsTo('Topic', 'topic_id');
}
}
Topic Model
class Topic extends Eloquent {
protected $guarded = array();
public static $rules = array();
public function tasks()
{
return $this->hasMany('Task');
}
}
Task Controller
public function show($id)
{
$task = $this->task->where('number', $id);
return View::make('tasks.show', compact('task'));
}
when I dd ($task) the relations array is empty.
object(Task)#688 (21) { ["guarded":protected]=> array(0) { }
["connection":protected]=> NULL ["table":protected]=> NULL
["primaryKey":protected]=> string(2) "id" ["perPage":protected]=>
int(15) ["incrementing"]=> bool(true) ["timestamps"]=> bool(true)
["attributes":protected]=> array(11) { ["id"]=> string(1) "9"
["number"]=> string(1) "6" ["topic"]=> string(1) "2" ["topic_old"]=>
string(10) "War Effort" ["task"]=> string(28) "You want us to collect
Moss?" ["objective"]=> string(36) "Collecting Information including
Q&A" ["method"]=> string(29) "Supported historical research"
["context"]=> string(0) "" ["level"]=> string(0) "" ["created_at"]=>
string(19) "0000-00-00 00:00:00" ["updated_at"]=> string(19)
"0000-00-00 00:00:00" } ["original":protected]=> array(11) { ["id"]=>
string(1) "9" ["number"]=> string(1) "6" ["topic"]=> string(1) "2"
["topic_old"]=> string(10) "War Effort" ["task"]=> string(28) "You
want us to collect Moss?" ["objective"]=> string(36) "Collecting
Information including Q&A" ["method"]=> string(29) "Supported
historical research" ["context"]=> string(0) "" ["level"]=> string(0)
"" ["created_at"]=> string(19) "0000-00-00 00:00:00" ["updated_at"]=>
string(19) "0000-00-00 00:00:00" } ["relations":protected]=> array(0)
{ } ["hidden":protected]=> array(0) { } ["visible":protected]=>
array(0) { } ["appends":protected]=> array(0) { }
["fillable":protected]=> array(0) { } ["dates":protected]=> array(0) {
} ["touches":protected]=> array(0) { } ["observables":protected]=>
array(0) { } ["with":protected]=> array(0) { }
["morphClass":protected]=> NULL ["exists"]=> bool(true)
["softDelete":protected]=> bool(false) }
You need to call the relationship that you declare in the model before. In this case I will use Eager Loading, with() function.
$tasks = $this->task->where('number', $id)->with('topic');
source: http://laravel.com/docs/4.2/eloquent#eager-loading
I would like to change the variable to $tasks because using
where(), you will get instant of
Illuminate\Database\Eloquent\Collection (many results)
And you can check the result by.
dd($tasks->get());

Issue in printing a session array variable

I have an peculiar session array variable,I need to pick one element to display on the screen.I'm not sure how to do that in smarty.Please give me the syntax how to manipulate the element in smarty.Here is the array values. I need to select ["school_name"] from the set.Please help
array(6) {
["id"]=> string(32) "cb60942f64d2b7bae2076926ae430f1d"
["sa"]=> NULL ["start_cache_timer"]=> int(1377752940) ["user"]=>
array(17) {
["host_id"]=> string(1) "9"
["user_id"]=> string(2) "16"
["school_name"]=> string(10) "Joseph High School"
["code"]=> string(10) "2E4C5339F8"
["academic_id"]=> string(2) "21"
["title"]=> string(2) "Mr"
["first_name"]=> string(3) "Jon"
["last_name"]=> string(7) "Bachura"
["name"]=> string(11) "Jon Bachura"
["email"]=> string(12) "jon#yeks.org"
["role"]=> string(1) "1"
["times_login"]=> int(656)
["acc_status"]=> string(1) "3"
["user_status"]=> string(1) "3"
["admin_user_id"]=>
string(2) "16"
["life"]=>
array(2) {
["current"]=>
int(1377756843)
["start"]=>
int(1377756843)
}
["training"]=>
array(1) {
["start_time"]=>
float(1377752946.14)
}
}
["mrole"]=>
NULL
["extAjaxSession"]=>
int(3)
}
Use $smarty.session:
$smarty.session.user.school_name

Resources