Laravel5.2 withCount not working - eloquent

I tried to use withCount() method just like the documentation and always i get this error :
Call to undefined method Illuminate\Database\Query\Builder::withCount()
public function index($id)
{
$data = \App\Institute::find($id);
if(!$data)
return response()->json(['message' => trans('system.notFound')])->setStatusCode(404);
$users = \App\MainArea::withCount('institute')->where('institute_id',$id)->get();
return $users;
}
any suggestion ?

I solved the issue by updating the composer.
composer update

Related

Octobercms set dynamically datepicker minDate

I need to set dynamically
public function filterFields($fields, $context = null)
{
$return_date = $fields->return_date->value;
if(empty($return_date)) {
$fields->return_date->minDate = Carbon::now();
}
}
Not work!
Hmm, may be datetime widget is setting this restrictions at init time and filterfields is fired after that so it can not use new modified data.
to over come this we set config data before init , we use controller method formExtendFieldsBefore add this method to your controller
public function formExtendFieldsBefore($form) {
$form->fields['return_date']['minDate'] = \Carbon::now()->format('Y-m-d');
}
it should work, please check this and if face any issue please comment.
You should pay attention to your model's fields.yaml - Where have you defined the return_date field, is it in the primary or secondary tabs ?
Try :
public function formExtendFieldsBefore( $widget ) {
$widget->tabs['fields']['return_date']['minDate'] = Carbon::now()->format('Y-m-d');
}
Or
public function formExtendFieldsBefore( $widget ) {
$widget->secondaryTabs['fields']['return_date']['minDate'] = Carbon::now()->format('Y-m-d');
// notice here $widget->secondaryTabs Vs $widget->tabs
}
This should work, if it doesn't please share your fields.yaml file .

How to query by substring in a MongoDB ODM

I have this actual code
public function findCode($code_received)
{
$pellicules =
$this->createQueryBuilder()
->field('code_base')->equals($code_received)
->getQuery()
;
}
But sometimes my code received is inside my code_base, for example:
Code_received = Abata
Code_base = Abata23xiub
I tried to use the in instead of equals but I'm not having success. How could I do it?

Cannot call method 'id' of undefined

My world.js looks like this:
var protractor = require('protractor');
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().usingServer('xxxxx').
withCapabilities(webdriver.Capabilities.firefox()).build();
driver.manage().timeouts().setScriptTimeout(100000);
module.exports.World = function World(callback) {
this.browser = protractor.wrapDriver(driver);
this.by = protractor.by;
callback();
};
then in steps.js:
{
element(by.id('username')).sendKeys("admin");
}
When I ran it using cucumber.js, the error is:
TypeError: Cannot call method 'id' of undefined
but if I remove world.js and run it using protractor, it works.
How can I fix this?
It looks like you're not exporting by globally. I'm not sure why you're able to use the element function at all - but in any case, you should probably be doing something like:
module.exports.World = function World(callback) {
global.browser = protractor.wrapDriver(driver);
global.by = protractor.by;
};

Fatal error: Call to a member function loadByOption() on a non-object

I am facing a problem. I have got following error "Fatal error: Call to a member function loadByOption() on a non-object in /var/www/Joomla/administrator/components/com_vodes/models/config.php on line 58". I am using Joomla 3.3.3 . I have upgraded it from 1.5 to 3.3.3
function save()
{
// initialize variables.
$table = JTable::getInstance('component');
$params = JRequest::getVar('params', array(), 'post', 'array');
$row = array();
$row['option'] = 'com_vodes';
$row['params'] = $params;
// load the component data for com_ajaxregister
if (!$table->loadByOption('com_vodes')) {
$this->setError($table->getError());
return false;
}
// bind the new values
$table->bind($row);
// check the row.
if (!$table->check()) {
$this->setError($table->getError());
return false;
}
// store the row.
if (!$table->store()) {
$this->setError($table->getError());
return false;
}
return true;
}
Please help to sought it out.
$table = JTable::getInstance('component');
you are passing the wrong parameter in your getInstance function the getInstance function returns null which makes the $table variable a non-object because it has no value. use your component name in setting the parameter.

Symfony form with doctrine table other than getTable()->find() is not working

I get a really anoying error when I try to edit an entry from a table, in tutorial they always use getTable()->find(), but I need to verify that the person logged in is the owner of that entry here what I did:
In the action:
public function executeEdit(sfWebRequest $request)
{
$id = $request->getParameter('id');
$userid = $this->getUser()->getGuardUser()->getId();
$ad = Doctrine_Core::getTable('BambinbazarArticles')->getMyAd($id, $userid);
$this->forward404Unless($ad, sprintf('Object bambinbazar_articles does not exist (%s).', $request->getParameter('id')));
$this->form = new BambinbazarArticlesForm($ad);
}
In the model:
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid);
return $q->execute();
}
I tried it with and without the ->execute(), did doctrine clean, cleared cache, rebuilded model,
Always get the same error 'The "%s" form only accepts a "%s" object.
If I use the Doctrine_Core::getTable('BambinbazarArticles')->find() it work, but of course, i need more than that..
I am becoming crazy over this.
execute() can return multiple rows; effectively you're getting a recordset back, rather than the individual object that your form is expecting. Try fetching a single object, using, e.g.:
return $q->execute()->getFirst();
or
return $q->fetchOne();
Its probably because your query is returning a Doctrine_Collection, not the actual Doctrine_Record youre expecting. Instead of execute use fetchOne.
public function getMyAd($id, $userid)
{
$q = $this->createQuery('c')
->where('c.id = ? ', $id)
->addWhere('c.userid = ? ', $userid)
->limit(1);
return $q->fetchOne();
}