I have the following in a partial view:
<?php
Yii::app()->clientScript->registerPackage('nivo-slider');
Yii::app()->clientScript->render();
However, I receive an error stating Missing argument 1 for CClientScript::render(),
What arguments does the render method require? I checked the docs but couldn't find anything definitive.
They seem pretty clear to me. You need to pass it a reference variable which will hold the value of the rendered string.
// as an example:
$output = NULL;
Yii::app()->clientScript->render($output);
// you can now do something with output!
Related
I made a custom conditional directive to determine if a user is allowed or not by the role.
Blade::if('isAllow', function($type, $my_role_id) {
//dd($type, $my_role_id);
$rol = Role::where('name', $type)->first();
return $my_role_id == $rol->id? true : false;
});
I use it this way.
#isAllow('Requestor', Auth()->user()->role_id) All #elseisAllow Reviewed #endisAllow
If into the directive, I put a dd() to see the values, it shows them to me without much problem.
But, when I want to use it in the template, it shows me the following error:
Too few arguments to function App\Providers\AppServiceProvider::App\Providers\{closure}(), 0 passed and exactly 2 expected
I can't find a solution to explain me why is not getting any param when it does. Any help?
you wrote #elseisAllow which is close to the meaning of else if and after #elseisAllow you did not provide arguments so you are getting error .
if you don't want to check another condition just use #else
but if you want to check more you should provide arguments that would be something like this : #elseisAllow('Author',auth()->user()->role_id)
I have an Eloquent Model, for which I created a custom toArray()-method, to include fields from a meta table (which I get by using the eloquent-meta plugin):
class User extends Model{
// ... Other stuff
public function toArray(){
return array_merge(parent::toArray(), $this->getAllMeta()->toArray());
}
}
When I now try to send this model as a JSON response using Response::json(...), I get:
UnexpectedValueException in Response.php line 403: The Response content must be a string or object implementing __toString(),
"boolean" given.
I have traced the error to the JsonResponse.setData($data)-method, in which the json_encode-call returns false. The json_last_error()-method returns JSON_ERROR_SYNTAX and the json_last_error_msg()-method returns Syntax error.
Using the debugger I stopped on the line below and evaluated the statement myself. As expected, it does not work, however if I call it like this, it works:
json_encode($data, JSON_PARTIAL_OUTPUT_ON_ERROR);
This returns the complete, valid JSON that I expect, without any missing or NULL values.
What's even stranger is, if I stop in the toArray()-method and supply the merged array to json_encode, it works fine, even without partial.
Am I overlooking something obvious here??
The problem was with the eloquent-meta plugin I used. Here's the relevant part from my issue:
I traced the error back to the
Helpers.maybeDecode($value)-method:
The current implementation tries to parse the value with
json_decode($value) and checks, whether that worked, by checking the
json_last_error()-function. The problem is, that this doesn't reset
the last error.
When the Helpers.maybeDecode($value)-method is called, while
Laravel is encoding the model, and the value it tried to decode
was not an valid json (for example, a simple string), the error
code is set, causing the json_encode()-function to see it and return
null. The problem is with the global nature of the error-variable.
My proposed workaround for this is to reset the
json_last_error()-function after checking if decoing worked, and the
only way I have found to do this is by decoding something valid (even
if it's just an empty array).
Here is the Pull Request with the fix.
I am using Laravel 4 for a project I am working on. I need to retrieve the first comment from the post. I use the following code to do so.
$comments = Comment::where('post_id', $post->id)->first();
This successfully retrieves the first comment (I know that because I print_r-ed $comments and it returned all the right information).
However, the following line of code triggers the error __toString() must not throw an exception
<td>{{$comments->content}}</td>
When I print_r-ed that it returned type string, and returned the correct string as well. Why then would it even try to convert $comments->content to type string when it is already a string?
Based off the information you've given and my experience with Laravel I'd bet that the line of code causing the exception is not the line you've put in your question.
<td>{{$comments->content}}</td>
This exception is complaining about the view throwing an exception. If this particular line was the issue you'd get a more descriptive exception about how $comments->content can't be converted into a string. You've also already tested that it is indeed a string.
I'd recommend finding where your "View" object is being echoed to the view and change it like so.
{{ View::make('yourbladefile')->__tostring() }}
This worked for me by providing a more accurate and informative exception. For more info on your exception you should check out Why it's impossible to throw exception from __toString()?
It's what gave me the idea in the first place. I know it's not a perfect answer so please let me know if this works and I'll update my answer if this turns out not be the case. Good luck.
I know that this is an old question, but for future googlers (like me) there is another way to solve that error, and it is independent of your framework:
public function __toString()
{
try {
return (string) $this->attributeToReturn; // If it is possible, return a string value from object.
} catch (Exception $e) {
return get_class($this).'#'.spl_object_hash($this); // If it is not possible, return a preset string to identify instance of object, e.g.
}
}
You can use it with your custom class with no framework, or with an entity in Symfony2/Doctrine... It will work as well.
I am creating a function that converts a users initials (STRING) to their userid (INT)
problem is when I call the function I get a call to undefined func error because the below declared function is no where to be found in the Source!
// connect to database -- this works, I checked it
function convertRadInitToRadID($radInits){
$sqlGetRadID="SELECT id FROM sched_roster WHERE radInitials == '".$radInits."'";
$resultGetRadID=mysql_query($sqlGetRadID);
$radID=mysql_result($resultGetRadID,0);
return $radID;
}
...I then create and array ($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter) of user initials, it works with no errors I tested it independently
$randKey=rand(0,(count($radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter)-1));
$randRad=$radNotOnVacay_and_NonMRNotonVacayWeekBeforeAndAfter[$randKey];
$randAssignedRadID=convertRadInitToRadID($randRad); // call to undefined function error here
when I view source the function definition code (where I define the function) is nowhere to be seen in the source. Very strange. I tried placing it around different areas of the script, same error. The function definition is declared appropriately and is wrapped in .
Very strange. The function just doesn't appear. Quotations, syntax, semi-colons, etc are all spot on.
No syntax errors. Advice?
I Strongly agree with Answer #1.
In addition a usual problems occur in php if you are defining function after calling it. i.e. your calling code is before function defination then it will not run and will give an error of undefined function.
You can create a class then define this function in that class and on the time of calling you can call that function with help of $this->function(args)
I think this will resolve your problem in mean while i am trying to run your code on my machine, lets see what happen
May be your function is a method of some class. So, if it is, you should use it in another way:
MyClass::convertRadInitToRadID($radInits) // if static method
or like this
$class = new MyClass();
$class ->convertRadInitToRadID($radInits)
Trying to make sense of your question... Are you trying to call the function using JavaScript? If so, remember that JavaScript is run on the browser, and PHP is run on the server (and this is why when you "view source" you don't see the function anywhere). To send data back from JavaScript to PHP you should use AJAX.
I found the answer: I was using Jquery UI tabs.... there must be a conflict with tabs. When I run the code without tabs there is no issue.
Thanks for the '==' fix.. appreciate it. my bad
thanks for reminding me about the 80 char varname code limit
I have methods which parameters can only handle certain types of variable. I have a few ideas on how to validate these type and I need your help to choose the best way.
I could:
Just return false if one of the types of variable is wrong, without letting the user know what's happening. Plus, if the function's output isn't usually checked by the user--e.g. ob_start()--they won't know it's even wrong;
Throw a custom InvalidArgumentException saying "Type of parameter X is incorrect." Thus I have to check every single parameter, making the code unreadable. Plus, the exception actually has to be catched, and I dislike these try...catch in my code.
Call error_log(). But yet I have to check every single parameter.
What option would you choose? Why? Otherwise, do you have a better idea?
UPDATE
When I talk about types, I mean these:
http://php.net/manual/en/language.types.php
The only way to do type checking in php is using the built-in function. You can find a list here : http://www.php.net/manual/en/ref.var.php It's a real pain in the ass, but you have no choice.
For the checking itself, I'll check all the parameters type at the beginning of the function and throw an error if not. Then you can always add some debuging print_r to discover the culprit.
If you are talking to arguments passed in your code of course you MUST log errors and fix every possibile mistakes in your code.
Example:
function(){
if (!is_array($arg))
trigger_error();
}
OR
Using only objects you can specify the type of arguments
Example:
function yay(Class1 $arg1, Class2 $arg2){
//That's it!
}