Where to put custom validator in Laravel? [duplicate] - laravel-4

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

Related

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 can add Laravel Helper in Vue js Inertia JS [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 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

Laravel 8 validation for image can get old input after symbolic link [duplicate]

This question already has answers here:
How to set a value to a file input in HTML?
(10 answers)
Laravel 5.1: keep uploaded file as old input
(3 answers)
Closed 1 year ago.
Using Laravel 8, I thought when I used to return a form on a validation error, I could load all the old inputs back into the view using the {{old('fieldname')}} helper.
However, that has stopped returning the inputs for my file upload input boxes, everything else returns to the form as normal, except the image inputs.
$validatedData = $request->validate([
'front_image' => 'mimes:jpeg,png,jpg,gif|max:3000',
'back_image' => 'mimes:jpeg,png,jpg,gif|max:3000|required',
'indentured' => 'required|in:yes,no',
'trade_selection' => ''
]);
When I use {{ old('front_image) }} nothing is reported. Weird thing is I can receive the errors for the input, and if I allow the form to push through I can see the files with dd($request) under files.

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 - Get only attributes data [duplicate]

This question already has answers here:
Laravel Getting attributes data
(2 answers)
Closed 2 years ago.
I am currently working on Laravel 6.13. I am using eloquent join to fetch the data from multiple tables.
Here is my working code:
$ToGetDefaultAddress = TbPersonaddress::join('tb_cities AS TbCitie','TbCitie.n_CityId_PK', '=', 'tb_personaddresses.n_CityId_FK')
->join('tb_counties AS TbCountie','TbCountie.n_CountyId_PK', '=', 'tb_personaddresses.n_CountyId_FK')
->join('tb_states AS TbState', 'TbState.n_StateId_PK','=' ,'tb_personaddresses.n_StateId_FK')
->select('tb_personaddresses.n_PersonAddressesId_PK','tb_personaddresses.n_PersonId_FK',
'tb_personaddresses.s_AddressTypeCode','tb_personaddresses.s_HouseNo',
'tb_personaddresses.s_HouseDirection1','tb_personaddresses.s_StreetName',
'tb_personaddresses.s_HouseType','tb_personaddresses.s_HouseDirection2',
'tb_personaddresses.s_AddressLine1','tb_personaddresses.s_AddressLine2',
'tb_personaddresses.s_AddressLine3','tb_personaddresses.s_AddressLine4',
'tb_personaddresses.n_CountyId_FK','tb_personaddresses.n_CityId_FK',
'tb_personaddresses.n_StateId_FK','tb_personaddresses.n_CountryId_FK',
'tb_personaddresses.s_PostalCode','tb_personaddresses.n_Zipcodes_FK',
'tb_personaddresses.s_Latitude','tb_personaddresses.s_Longitude',
'tb_personaddresses.s_CountryName','tb_personaddresses.s_Description',
'tb_personaddresses.d_EffectiveDateFrom','tb_personaddresses.d_EffectiveDateTo',
'tb_personaddresses.s_IsDefaultAddress','tb_personaddresses.n_CreatedUser',
'tb_personaddresses.d_CreatedDate','tb_personaddresses.n_UpdatedUser',
'tb_personaddresses.d_UpdatedDate','tb_personaddresses.n_EditVersion',
'TbCitie.s_CityCode','TbCitie.s_CityName','TbCountie.s_CountyCode',
'TbCountie.s_CountyName','TbState.s_StateCode','TbState.s_StateName')->first();
I checked using dd() for output. I want to get only attributes data as specified in image. How can I get those ?
Tried solutions from following link:
Laravel Getting attributes data
But those are not working.
Here's what I tried from link after above code:
$DefaultAddress = $ToGetDefaultAddress->all()->toArray();
dd(":::::::::::To get default address::::::::",$DefaultAddress);
But it returns blank output.
You should chain the toArray() function at the end of your database call to get only the attributes (and any loaded relations).

Resources