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 3 years ago.
Improve this question
I am using Laravel's FormRequest for validation to make my code clean in controller and after validation, I want to add some data to request and then save it. So, I was just looking for a solution so that I do not have to add data to request in controller If just after validation in the same file there could be the extra function for modifying data and send to controller. It would have been better.
If you want to do something with request after it validated, you can use After Validation Hook, as apokryfos suggested.
But I think it is a bit more convinient to put that hook inside of your FormRequest descendant class.
Ancestor:
use Illuminate\Foundation\Http\FormRequest;
class AncestorRequest extends FormRequest
{
...
protected function getValidatorInstance()
{
return parent::getValidatorInstance()->after(function ($validator) {
$this->after($validator);
});
}
protected function after($validator)
{
//
}
}
Descendant:
class DescendantRequest extends AncestorRequest
{
...
public function after($validator)
{
// do your things
}
}
P. S. This solution I am using in Laravel 5.2. Here you can find more options.
Related
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 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
i'm trying check if the user is an Admin so i can restrict that URL from other users, so far i tried this code :
public function Adminp(){
$data = ['LoggedUserInfo' =>Utilisateurs::where('id','=',session('LoggedUser'))->first() ];
$utilisateurs = Utilisateurs::all();
$utilisateur->role = $_GET['role'];
if($utilisateur->role == 'Admin'){
return view('Admin.admin-dashboard', $data, compact('utilisateurs'));
}
else{
return abort(404);
}
}
all i get are errors
I am just guessing at what you are doing. I will assume you just want to see if the User that you retrieved from the database has the role you are looking for ... you should be using the authentication system for this and a Middleware to do the filtering, but
public function Adminp()
{
$user = Utilisateurs::findOrFail(session('LoggedUser'));
if ($user->role == 'Admin') {
return view('Admin.admin-dashboard', [
'LoggedUserInfo' => $user,
'utilisateurs' => Utilisateurs::all(),
]);
}
return abort(404);
}
Let me stress this again though, you should be using the Authentication system and a Middleware to do the filtering instead of reinventing the wheel.
$utilisateurs = Utilisateurs::all();
This will return you array of Utilisateurs and in the next line you are trying to get role property that will never exist in that array you need to get only one user not the array of all users.
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 1 year ago.
Improve this question
I’m using Laravel 8 with Sanctum.
Is there a way to create a migration, or what is the best practice, to add some default token every time I redeploy my project?
Thank you.
You can use User factory for this:
public function configure()
{
return $this->afterCreating(function (User $user) {
//You need to use some condition in user to determine, make token or not
if($user->isAdmin){
$user->tokens()->create([
'name' => 'default_token',
'token' => 'default_token_value',
'abilities' => '*',
]);
}
});
}
If user is admin - after creating - factory make default token for them.
Than, you can use factory in your seeder/tests/etc.
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
Thank you in advance,
I have one controller function like
public function storeBlog(Request $request)
{
// Here i am receiving file like $request->file('image');
}
Now I want to send that file to an API endpoint like
Http::post('http://example.com/v1/blog/store', $request->all());
I am getting all the request but not file, I know we need to pass POST data as a multipart but how that I don't know
can anyone help
You should use Http::attach to upload a file.
public function storeBlog(Request $request)
{
// check file is present and has no problem uploading it
if ($request->hasFile('image') && $request->file('photo')->isValid()) {
// get Illuminate\Http\UploadedFile instance
$image = $request->file('image');
// post request with attachment
Http::attach('attachment', file_get_contents($image), 'image.jpg')
->post('example.com/v1/blog/store', $request->all());
} else {
Http::post('http://example.com/v1/blog/store', $request->all());
}
}
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I have 2 dropdown in one of my web page build with CodeIgniter. First dropdown is Controller Name and second one is Method Name. I am able to pull all controllers and showing their names in the dropdown option. Now when user choose a controller name from first dropdown I want to show all methods name declared on that controller file in the second dropdown.
I have tried this below code..
foreach ($controllers as $key => $value) {
foreach(get_class_methods($value) as $methods){
echo $methods;
}
}
here $controllers is an array of all controller name.
above code print only current class methods.i want all the methods of all the controllers.
Any idea how to do that!
so
I've got a link which might give you list of all controllers and methods in those controllers. Hope this might help.
Reference Link
Create a main controller on application/core folder by name My_Controller
And the code for that is,
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Main_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function allMethods() {
print_r(get_class_methods($this));
}
}
?>
Which ever class you have to fetch the methods should extend this controller
class A extends Main_Controller { }
class B extends Main_Controller { }
class C extends Main_Controller { }
Now the method allMethods() will be inherited to all controllers
To get all functions from a controller,
Suppose user selects controller A,
Call function like this,,
Suppose user selects controller A,
a/allmethods
Suppose user selects controller B,
b/allmethods
.... so on
So finally,
Get all methods from a controller using,
userselectedcontroller/allMethods
Hope this helps you:)
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 years ago.
Improve this question
I'm just wondering how I'd be able to share information between service providers?
Service Providers are, actually, Service Bootstrappers for your services, if you need to share information between them it's because you need your services to talk to each other, probably, so you do that via the Application IoC Container:
class Service1Provider extends ServiceProvider {
public function register()
{
$this->app['service1'] = $this->app->share(function($app)
{
return new Service1;
});
}
}
class Service2Provider extends ServiceProvider {
public function register()
{
$service1 = $this->app['service1'];
$this->app['service2'] = $this->app->share(function($app) use ($service1)
{
return new Service2($service1);
});
}
}