Is there a way to evaluate a variable in a blade directive? I try to get the value of an associative array in function of dynamically passed arguments over directive
Having the following will fail
Blade::directive('imagepol', function ($expression) {
$options = get_field('fallback_image', 'option');
if(!empty($expression) && $options) {
return "<?= isset($options[$expression]) ? wp_get_attachment_image_srcset($options[$expression]['ID']) : ''?>";
}
});
I call the directive as it follows
#imagepol($item->get('pattern_options'))
In this case will fail with the following
Undefined array key "$item->get('pattern_options')"
Related
I write middleare to decide permission. But giving error.
Route page
Route::middleware([Permission::class])->group(function($id){
});
middleware
public function handle(Request $request, Closure $next,$id)
{
$id = $request->id; //$id is returning null
}
Giving this eror
Too few arguments to function App\Http\Middleware\Permission::handle(), 2 passed in /home/saide/Desktop/saide-backoffice/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php on line 167 and exactly 3 expected
I think when you called Middleware you used a square bracket, you have used an array for submitting the parameter to the middleware, use the below code
Route::middleware([Permission::class])->group(function($id){
});
For submitting multiple parameters through middleware use this code:
Route::get('/', function () {
//
})->middleware(['first', 'second']);
For passing single middleware use this:
Route::get('/profile', function () {
//
})->middleware('auth');
Information Source: https://laravel.com/docs/8.x/middleware
The issue is that the middleware is expecting a parameter and you aren't supplying it.
If you want your middleware to have an additional parameter $id, then route should be like this:
Route::middleware([Permission::class.':id_value_goes_here'])->group(function () {
});
If you need the ID to be a dynamic parameter based on a route parameter (e.g. Route::get('/posts/$id', ...)) or something passed to the request, then you should omit the extra parameter from the middleware handle()'s method signature:
public function handle(Request $request, Closure $next)
{
$id = $request->id; // $id will now get correctly set
}
When I'm trying to use Template strings inside the data function in vuejs but, it always returns undefined any idea how to solve this ?
I was trying to make a URL for an API call dynamic
Cheers,
data() {
return {
baseUrl: `https://example.com/api/json?key=${this.key}`,
key: "IzNDU2Nzg5MDEyMzQ1Njc"
};
}
This is a JavaScript issue. If you run the following simple example in JavaScript you'll get a "is not defined" error (when running in strict mode).
{ a: `${b}`, b: "123" }
> VM246:1 Uncaught ReferenceError: b is not defined
You can't reference an adjacent variable ('key' in your example) in an object literal declaration.
You can use a Vue.je computed property for baseURL:
computed: {
baseUrl() {
return `https://example.com/api/json?key=${this.key}`;
}
}
The data property cannot be made dynamic. Use a computed property like below:
computed: {
baseUrl() {
return `https://example.com/api/json?key=${this.key}`
}
}
I am filtering projects with a computed property like this:
filtered_projects() {
return this.projects.filter( (project) => {
return project.title.toLowerCase().indexOf(this.project_filter.toLowerCase()) !== -1
})
}
When I add a new project using this
submitNewProject() {
let path = '/api/projects';
Vue.http.post(path, this.project)
.then( (rsp) => {
this.projects.push(rsp.data);
this.project = this.getSingleProject();
this.create_project = false;
return true;
});
}
This is giving me an error that I can't find
TypeError: Cannot read property 'toLowerCase' of undefined
It may just be that you are not correctly passing the projects data to the projects array.
Firstly vue-resource now uses body not data to get the response, therefore you may need to use:
this.projects.push(rsp.body)
then this will give you a single array item containing all projects which doesn't look like what you want. I believe instead you're after:
this.projects = rsp.body
Assuming the response body is an array of objects this will then allow:
this.projects.filter(project => {})
to work as expected. Meaning project.title should now be valid
EDIT
For a project title to be set to lowerCase you must be returning an object with a title param, i.e.
rsp.body = {
title: 'FOO',
}
which you'd then set on the projects array via:
this.projects.push(rsp.body)
so the first thing to fix is your response, sort your endpoint so it returns what you are expecting, then the rest of the above code should work
You need preserve "this" before Vue.http.post (self=this) and then in callback change this.projects to self.projects.
Explanation:
How to access the correct `this` context inside a callback?
for example:
class Foo() {
const BAR_FUNC = 'bar';
//!!!the following function name needs to use self::BAR_FUNC rather than "hardcoded" text "bar" in the function name
function get_default_bar() {
return 'this is my bar';
}
}
http://au1.php.net/functions.variable-functions has an example of dynamically named function, but it doesn't work for class method:
Create and call a dynamically named function
$tmp = "foo";
$$tmp = function() {
global $tmp;
echo $tmp;
};
$$tmp();
Outputs "foo"
The example you gave is for calling a function based on a dynamic name, not creating a function based on a dynamic name.
There is an existing answer at Dynamically Create Instance Method in PHP
I'm calling a simple javascript function via onchange
echo $form->dropDownList($model, 'job_title', $jobTypes, array('onchange'=>'jobTitle(this);')); ?>
Trying to register jobTitle via clientScript->registerScript
Yii::app()->clientScript->registerScript('jobTitle', "
function jobTitle(e) {
alert('e');
}
");
Yet I'm getting an error that jobTitle is not defined...
The Reason:
As the default position is CClientScript::POS_READY, the generated js is:
jQuery(function($) {
// ... there could be some other yii scriptlets too ...
function jobTitle(e) {
alert('e');
}
});
Which means your function jobTitle is available only within the jQuery(); function scope and not from outside it, and that's why you get the undefined error.
Solution 1:
If you use the positions : CClientScript::POS_HEAD or CClientScript::POS_BEGIN or CClientScript::POS_END with your registerScript call, i.e:
Yii::app()->clientScript->registerScript('jobTitle', "
function jobTitle(e) {
alert('e');
}
", CClientScript::POS_END);
the function will be defined outside, and in the global scope, and then you will be able to use the function.
Solution 2:
Alternatively if you need the function within ready(), i.e within jQuery(function($){});, you can define the function in the global window object, and still access it from outside:
Yii::app()->clientScript->registerScript('jobTitle', "
window.jobTitle = function jobTitle(e) {
alert('e');
}
");
Solution 3:
Or you can simply add an event handler from jQuery itself, instead of doing it inline in the html:
Yii::app()->clientScript->registerScript('jobTitle', "
$('body').on('change', 'id-of-job-title-input', function(e){
console.log(e);
});
");