Laravel doesntHave with callback said object of class Closure could not be converted to string - eloquent

i want to write a query like that:
$schedule = Schedule::doesntHave('orders', function ($q) {
$q->where('status', Order::STATUS_CANCEL);
})
and laravel throw exception that said:
"Object of class Closure could not be converted to string"
can you help me if you faced this problem?

I find the solution
We should send second parameter to query for can translate query; My query should be like this:
$schedule = Schedule::doesntHave('orders', 'and', function ($q) {
$q->where('status', Order::STATUS_CANCEL);
});

Related

where Has Condition on a hasMany relationship in Laravel 5.8

I have a relationship in a model FeeModuleModel as shown below
public function heads()
{
return $this->hasMany('App\Models\FeeHeadModel','location_id','id');
}
and in my controller file i need to fetch only the values FeeModuleModel where the FeeHeadModel has type as unstructured
My controller code is as shown below
$modules = FeeModuleModel::where('vt_ay_id', '=', Session::get('sess_ay_id'))->with(['heads'=>function($q){
$q->where('type','=','unstructured');
}])->orderby('priority', 'asc')->get();
This fails with the following error
Call to a member function getRelationExistenceQuery() on array
What is the problem with my code and what I can do to solve it
Please change your code to this one
$modules = FeeModuleModel::with(['heads'=>function($q){
$q->where('type','=','unstructured');
}])->where('vt_ay_id', '=', Session::get('sess_ay_id'))->orderby('priority', 'asc')->get();

How to pass reference to $self->class->method?

I am calling a method as follows:
$self->class->method
I would like to pass a reference to this method as a parameter into a subroutine.
I have tried
\&{ $self->class->method }
but I get the following error:
Unable to create sub named ""
Any ideas on how to do this?
You can take a reference to static class method, but in your case you could use anonymous closure to achieve similar,
my $ref = sub { $self->class->method };
# ..
my $result = $ref->();
The class method is a little strange. I would expect a method with a name like that to return the class string, but it clearly returns an object as it has a method method.
I recommend that you use UNIVERSAL::can, which returns a reference to the given method if it exists. So you can write code like this
my $method_ref = $self->class->can('method');
mysub($method_ref);
You can also use the curry module to achieve this:
use curry;
my $mref = $self->class->curry::method;

Call generic method with 'out' parameter

I have a 'SourceClass' class with the simple C# method:
public bool TryGetSection<T>(out T result) where T : class, new()
{ //do something and return true/false }
and I'd like to execute it from powershell code.
$_config = New-Object [SourceClass]
$method = [SourceClass].GetMethod("TryGetSection")
$genericMethod = $method.MakeGenericMethod([AuthenticationProviderTypeConfig])
$authenticationProviderTypeConfig = New-Object AuthenticationProviderTypeConfig
$genericMethod.Invoke($_config, $authenticationProviderTypeConfig)
$authenticationProviderType = $authenticationProviderTypeConfig.Type
After execution, I'm expecting that the $authenticationProviderType will be filled, but it isn't. I'm just getting an empty string. I suppose I miss something.
Is it possible to call generic method that has 'out' parameter?
Thanks.

use pdo connection inside php function

I have a database class for pdo access to my db.
Inside the class I have a function :
public function isSenderIdinDB($id)
I do in my script:
$conn=new Database($credentials);
$id=something;
echo $conn->isSenderIdinDB($id);
works fine.
Now I'd like to use a function in my script, like this:
echo fn_isSenderIdinDB($id);
with:
function fn_isSenderIdinDB($id) {
return $conn->isSenderIdinDB($id);
}
But it doesn't work. I tried with a:
global $conn;
inside the fn_isSenderIdinDB function, as suggested elsewhere on SO, without success.
Any help appreciated, thanks
Nicolas
This sounds like a variable scope issue, you could try passing $conn as a parameter to your function like this.
function fn_isSenderIdinDB(&$connObj, $id) {
return $connObj->isSenderIdinDB($id);
}
And then call your function like that :
echo fn_isSenderIdinDB($conn, $id);
Perhaps it would help if we could see the whole script or the errors your are getting.

How to use Zend Framework's Partial Loop with Objects

I am quite confused how to use partialLoop
Currently I use
foreach ($childrenTodos as $childTodo) {
echo $this->partial('todos/_row.phtml', array('todo' => $childTodo));
}
$childrenTodos is a Doctrine\ORM\PersistantCollection, $childTodo is a Application\Models\Todo
I tried doing
echo $this->partialLoop('todos/_row.phtml', $childrenTodos)
->setObjectKey('Application\Models\Todo');
But in the partial when I try to access properties/functions of my Todo class, I cant seem to get them always ending up with either call to undefined method Zend_View::myFunction() when I use $this->myFunction() in the partial or if I try $this->todo->getName() I get "Call to a member function getName() on a non-object". How do I use partialLoops?
Try this
echo $this->partialLoop('todos/_row.phtml', $childrenTodos)
->setObjectKey('object');
Then in your partial you can access the object like this
$this->object
object is the name of the variable that an object will be assigned to
You can also do this once in your Bootstrap or other initialization class if you have access to the view object like so
protected function initPartialLoopObject()
{
$this->_view->partialLoop()->setObjectKey('object');
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->setView($this->_view);
}
I also had "Call to function on non object" error when trying suggested syntax, seems like they've changed something on later versions of Zend Framework. The following works for me on ZF1.12:
echo $this->partialLoop()
->setObjectKey('object')
->partialLoop('todos/_row.phtml', $childrenTodos);