I'm trying to get this package to work: https://github.com/laracasts/PHP-Vars-To-Js-Transformer
I did everything according to the description, but I get no output of the variables.
Laravel Version: laravel/framework (v5.2.24)
My config/app
Laracasts\Utilities\JavaScript\JavaScriptServiceProvider::class,
'JavaScript' => Laracasts\Utilities\JavaScript\JavaScriptFacade::class,
My config/javascript
'bind_js_vars_to_this_view' => 'footer',
'js_namespace' => 'qwerTEST'
in my controller
use JavaScript;
JavaScript::put(['fett' => 'testtesttest']);
in my main view
#include('layouts.footer');
my footer.blade.php
`<div>some text</div>`
and in my view
#extends('layouts.intern')
#section('content')
<script>
console.log('test');
console.log(qwerTEST.fett);
</script>
#endsection
the javascript of chrome gives out
test
Uncaught ReferenceError: qwerTEST is not defined
And there is nothing with qwerTEST javascript namespace in the sourcecode. But I get no error at all.
Am I missing something?
I found it:
in my config/javascript I have to write the full path to the view:
'bind_js_vars_to_this_view' => 'folder.footer'
Could be better described in the Package..
Related
Im trying to do a POST request with jQuery but im getting a error 405 (Method Not Allowed), Im working with Laravel 5
THis is my code:
jQuery
<script type="text/javascript">
$(document).ready(function () {
$('.delete').click(function (e){
e.preventDefault();
var row = $(this).parents('tr');
var id = row.data('id');
var form = $('#formDelete');
var url = form.attr('action').replace(':USER_ID', id);
var data = form.serialize();
$.post(url, data, function (result){
alert(result);
});
});
});
</script>
HTML
{!! Form::open(['route' => ['companiesDelete', ':USER_ID'], 'method' =>'DELETE', 'id' => 'formDelete']) !!}
{!!Form::close() !!}
Controller
public function delete($id, \Request $request){
return $id;
}
The Jquery error is http://localhost/laravel5.1/public/empresas/eliminar/5 405 (Method Not Allowed).
The url value is
http://localhost/laravel5.1/public/empresas/eliminar/5
and the data value is
_method=DELETE&_token=pCETpf1jDT1rY615o62W0UK7hs3UnTNm1t0vmIRZ.
If i change to $.get request it works fine, but i want to do a post request.
Anyone could help me?
Thanks.
EDIT!!
Route
Route::post('empresas/eliminar/{id}', ['as' => 'companiesDelete', 'uses' => 'CompaniesController#delete']);
The methodNotAllowed exception indicates that a route doesn't exist for the HTTP method you are requesting.
Your form is set up to make a DELETE request, so your route needs to use Route::delete() to receive this.
Route::delete('empresas/eliminar/{id}', [
'as' => 'companiesDelete',
'uses' => 'CompaniesController#delete'
]);
Your routes.php file needs to be setup correctly.
What I am assuming your current setup is like:
Route::post('/empresas/eliminar/{id}','CompanyController#companiesDelete');
or something. Define a route for the delete method instead.
Route::delete('/empresas/eliminar/{id}','CompanyController#companiesDelete');
Now if you are using a Route resource, the default route name to be used for the 'DELETE' method is .destroy. Define your delete logic in that function instead.
In my case the route in my router was:
Route::post('/new-order', 'Api\OrderController#initiateOrder')->name('newOrder');
and from the client app I was posting the request to:
https://my-domain/api/new-order/
So, because of the trailing slash I got a 405. Hope it helps someone
If you didn't have such an error during development and it props up only in production try
php artisan route:list to see if the route exists.
If it doesn't try
php artisan route:clear to clear your cache.
That worked for me.
This might help someone so I'll put my inputs here as well.
I've encountered the same (or similar) problem. Apparently, the problem was the POST request was blocked by Modsec by the following rules: 350147, 340147, 340148, 350148
After blocking the request, I was redirected to the same endpoint but as a GET request of course and thus the 405.
I whitelisted those rules and voila, the 405 error was gone.
Hope this helps someone.
If you're using the resource routes, then in the HTML body of the form, you can use method_field helper like this:
<form>
{{ csrf_field() }}
{{ method_field('PUT') }}
<!-- ... -->
</form>
It will create hidden form input with method type, that is correctly interpereted by Laravel 5.5+.
Since Laravel 5.6 you can use following Blade directives in the templates:
<form>
#method('put')
#csrf
<!-- ... -->
</form>
Hope this might help someone in the future.
When use method delete in form then must have to set route delete
Route::delete("empresas/eliminar/{id}", "CompaniesController#delete");
I solved that issue by running php artisan route:cache which cleared the cache and it's start working.
For Laravel 7 +, just in case you run into this, you should check if the route exists using
php artisan route:list
if it exists then you need to cache your routes
php artisan route:cache
I'm trying to require spatie/laravel-honeypot and when navigating to the page where the component should be rendered, I get the following error: Unable to locate a class or view for component [honeypot] I cleared all caches, ran the command: composer dump-autoload, but it didn't help. But if i'm trying to get registered blade components by following code Blade::getClassComponentAliases(), i get the following output:
array:5 [▼
"dynamic-component" => "Illuminate\View\DynamicComponent"
"orchid-icon" => "Orchid\Icons\IconComponent"
"orchid-popover" => "Orchid\Screen\Components\Popover"
"tabuna-breadcrumbs" => "Tabuna\Breadcrumbs\BreadcrumbsComponent"
"honeypot" => "Spatie\Honeypot\View\HoneypotComponent"
]
Why am I getting the error anyway?
Wow, Just seeing this;
If you're using <x-honeypot /> in your form, replace it with #honeypot
I am starting with the Inertia Laravel example https://github.com/drehimself/inertia-example
which is nothing but Laravel with Vue in one monolithic codebase, using Inertia.js:
https://github.com/inertiajs/inertia-laravel
https://github.com/inertiajs/inertia-vue
I am trying to access Laravel's .env variables inside my .vue component files
.env file:
APP_NAME=ABC
In app\Providers\AppServiceProvider.php:
public function register()
{
Inertia::share('appName', env('APP_NAME'));
}
In resources\js\Home.vue component:
<template>
<div>
<span class="tw-text-left">{{ appName }}</span>
</div>
</template>
<script>
export default {
props: [
"appName",
]
}
</script>
In my vue console, appName shows up blank. It should show "ABC" but doesn't.
What's going on here, and how I can access all my env variables, ideally without passing everything through the controller, which doesn't seem very efficient?
I finally got it working. Here's how, for those interested:
In AppServiceProvider:
Inertia::share(function () {
return [
'app' => [
'name' => config('app.name'),
],
];
});
In your vue file:
<template>
<div>
App name: {{ $page.app.name }}
</div>
</template>
The 2nd part is what I was missing..I was trying to accept the app.name prop, and was missing $page.
Hope this helps somebody!
I know this is kind of old, but I ran into this same issue and the methods above and around the net did not work for me. Something might have changed with Inertia? Anyway, I did get it working though.
Just like above add the following to the register method within your AppServiceProvider:
Inertia::share('appName', config('app.name'));
// I'm using config, but your could use env
Then in your Vue component access the $inertia variable like so:
{{ $inertia.page.props.appName }}
From the documentation on the author's website, you need to instruct vue to inject the page into your component, and then you can accessed the shared variables.
For example:
<template>
<div>
<span class="tw-text-left">{{ page.props.appName }}</span>
</div>
</template>
<script>
export default {
inject: ['page'],
// ...
}
</script>
if you want to share multiple variables with view use the following:
Inertia::share('app', [
'name' => config('app.name'),
'url' => config('app.url'),
]);
I need to change the route of my page from mysite/practice to mysite/practice/new
I have a React project set up in laravel with lumen.
routes.php:
$app->get('/practice', ['as' => 'pet_practices', 'middleware' => 'check_vet_roles', 'uses' => 'PetsController#practice']);
PetsController.php
public function practice(Request $request)
{
$variables =$this->getUserInfo($request);
return view('practice-profile') ->with('variables', $variables);
}
practice-profile.blade.php
#extends('layouts.master')
#section('content')
<div id="practice-profile"></div>
#endsection
My JSX file
var React = require('react');
var ReactDOM = require('react-dom');
var PageCollectionComponent = require('./PageCollectionComponent');
if (document.getElementById('practice-profile')) {
ReactDOM.render(<PageCollectionComponent />,document.getElementById('practice-profile'));
}
Everything works fine in this setup. When I go to mysite/practice page loads fine.
But if I change the route to
$app->get('/practice/new', ['as' => 'pet_practices', 'middleware' => 'check_vet_roles', 'uses' => 'PetsController#practice']);
and go to mysite/practice/new it doesn't work anymore. The layout loads from the blade file, but it doesn't get to the JSX file anymore. Why is that?
In the console, I get this error:
Uncaught SyntaxError: Unexpected token :
I'm fairly new to this project so it's probably something fundamental that I am missing here.
It's hard to tell without more info. But, maybe the problem occurs because you're moving up a directory level, make sure that your JS file is being loaded from the correct location. If you're using a relative path like <script src="app.js"></script> then going a level deeper will try to request yoursite/practice/app.js instead of yoursite/app.js.
I am creating documentation for my blade based components in a laravel project and would like to display syntax highlighted blade code snippets as part of the documentation a la something like this:
I have installed graham-campbell/markdown package and I try use it inside a .blade.php file as follows:
(Do not mind the escape character)
However, the output I get is as follows:
You can wrap the Blade in a #verbatim directive and use Highlight JS with a style you like
<p>You can use the laravel code template like this</p>
#markdown
```php
#verbatim
#include('components.inputs.text', [
'name' => 'input_name',
'label' => 'testing',
])
#endverbatim
```
#endmarkdown
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/styles/a11y-dark.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.15.10/highlight.min.js"></script>
<script>
hljs.initHighlightingOnLoad();
</script>
Result
Hope this helps