How can add Laravel Helper in Vue js Inertia JS [closed] - laravel

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
Improve this question
Good day
I Beginner in Inertia Js and Vue Js i have one Helper for Laravel normally I use this code in .blade Laravel and working Helper::reaction_count($id,$reaction) but in Inertia Js i thing must passed everything from controller , can help me , how i can add Helper code in .vue theme ?
Thanks in advance

If you want to pass object to Javascript from laravel directly then you can use laracasts/utilities package.
composer require laracasts/utilities
Then add a line in config/app.php
// config/app.php
'providers' => [
'...',
'Laracasts\Utilities\JavaScript\JavaScriptServiceProvider'
];
You can pass object to javascript like this
use JavaScript;
public function index()
{
JavaScript::put([
'count' => Helper::reaction_count($id,$reaction)
]);
...
}
For more information, please check here
https://github.com/laracasts/PHP-Vars-To-Js-Transformer

Related

Call to undefined function App\Http\Controllers\exec() [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
Please help me. The website works fine in the local host, but it is having error in the C panel. How to solve?
the path is:
app/config/app.php
you can use Process Component of Symfony that is already in Laravel
http://symfony.com/doc/current/components/process.html
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
inside your elcdetails use
$macAddr = new Process('getmac');
$macAddr->run();
// executes after the command finishes
if (!$macAddr->isSuccessful()) {
throw new ProcessFailedException($macAddr;
}
//or whatever you want to do with the output.
echo $macAddr->getOutput();
note that, if you are trying to get the client mac address, short answer is you cannot.
From your image, I'm supposing you defined the public function exec() function somewhere above or below in your controller. You cannot reference to it that way since its a method in your controller class.
So change it to this instead. $macAddr = $this->exec('getmac');

PUT and DELETE route is not showing in laravel route list on apiResource [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I added apiResource routes in my api.php file. Although it shows all the resourceful routes for product/category, it is not showing the routes for PUT and DELETE methods in the case of products routes.
Showing 404 not found error.
Any help?
You can solve the missing parameter names using the parameters method.
Route::prefix('products')->group(function() {
Route::apiResource('/', \App\Http\Controllers\ProductController::class)
->parameters(['' => 'product']);
Route::apiResource('/categories', \App\Http\Controllers\CategoryController::class);
});
That should give you the following:
Note that it is a convention to make your resources plural rather than singular.
You can accomplish a lot using scoped nested resources and shallow nesting:
Route::apiResource('products', ProductController::class);
Route::apiResource('products.categories', ProductCategoryController::class);
Route::apiResource('products', ProductController::class);
Route::apiResource('products.categories', ProductCategoryController::class)->shallow();

how to make language button switch on laravel [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
i'm using blade template, all file content is in view and i need 2 language on my project, how the easiest way to make the button switcher language? can someone help me?
Thank you
You can create language links like:
<a href="{{ route('locale.setting', 'en') }}">
EN
</a>
<a href="{{ route('locale.setting', 'es') }}">
ES
</a>
You will need a route that will change the current language:
Route::get('set-locale/{locale}', function ($locale) {
App::setLocale($locale);
session()->put('locale', $locale);
return redirect()->back();
})->middleware('check.locale')->name('locale.setting');
And a middleware that will set the appropriate language for other routes:
public function handle($request, Closure $next)
{
if(session()->has('locale')) {
app()->setLocale(session('locale'));
app()->setLocale(config('app.locale'));
}
return $next($request);
}
Laravel comes with localization support. Its explained in depth here.
What you basically need to do is to define all your localization strings inside the resources/lang directory. By default you'll find an en directory where there are files that hold localization for different parts of the application. You can create an app.php file inside the en folder and define keywords and their English text. Then add another folder for your language (for example es for Spanish) and also create another app.php in there with the Spanish texts. These will be your translation strings.
These strings can be used in your views using the __ function or #lang blade directive.
To change the current language (locale), you need to setup a route that listens for this request and set the locale accordingly. Something like this:
routes/web.php
Route::get('/setLang?lang={locale}', function ($locale) {
\App::setLocale($locale);
// Session::put('locale', $locale); // This should also work
return back();
});
that's alot of work... You should use language files, like en.php and use define() to store translations. Note that de variable should be the same across all files. Example:
en.php
define('GREETINGS', 'Hello');
Now in portuguese for example:
pt.php
define('GREETINGS', 'Olá');
Now, in your language selector or master switch, you should store in a session variable like user_language = 'en. On every request / page load, define the proper file to load translations.
EDIT #1 - To be clean, use a middleware to handle this.
I think you got the idea!

Laravel is there any other way to display data from controller to blade? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Suppose I only selected one specific row from the database,
and returned that result to view.blade.php, now I want to display that single row of data. How would I do it without using #foreach is there any other function in laravel that i can use?
you can use first() to get an object instead of collection. so you need not to loop through to get object property.
controller
public function functionName($parameter)
{
$value = Model::where('field_name', $parameter)->first();
return view('view-name', compact('value'));
}
view blade
{{ $value->property }}
Suppose you have a record stored in $data variable now
you can simply display it on your view template like
`echo $data->image; //without using any #foreach loop`

Where to put custom validator in Laravel? [duplicate]

This question already has answers here:
Laravel 4 custom validation rule - where to extend the validator?
(2 answers)
Closed 8 years ago.
This page isn't very specific on where one should place the custom validator. You can easily create one:
Validator::extend('foo', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
But where does this go? My gut feel tells me to create a "validators.php" file and include it via global.php. Is that the right place to put it?
You can put your custom validation rule on route.php

Resources