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 2 years ago.
Improve this question
How would you set up a dynamic axios call in Vuex? For example /api/data/{id} where the id is linked to the user id from the laravel database
You can access the router dynamic parameters by doing: this.$route.params.id
Example to get a user information via axios:
<template></template>
<script>
export default {
async created() {
const userInformation = await axios.get(`user/${this.$route.params.userId}`);
}
}
</script>
This is assuming you have a route defined with the following:
const routes = [
{ path: 'user/:id', name: 'user-view', component: ComponentA }
]
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 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.
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);
});
}
}