When we create new FormRequest we have method
public function authorize()
{
return 1;
}
My question is, can I have here something from form, any eloquent model object?
Like
public function authorize(Posts $post)
?
I realized that I can read request data here by eg $this->content
But Can I send here whole object?
Let's asumme we have post
collection in view.
I have form
If I could have something like
<form method="post" action="{{ route('temat.post.like', $post) }}">
And in this route's controller method public function post_like(LikePost $request)
And then in authorize method called class LikePost extends FormRequest
have this $post
like public function authorize(Posts $post)
I don't know if it is even possible to get it somehow in authorize method, I looked for this and I don't think is it possible but I may be wrong, that's why I ask
I mean to have in this authorize()
method Eloquent Model fetched by this parameter, just like Controllers do.
eg
public function authorize(Users $user)
url: /like/15
Fetched User Model by id 15
and in method we can use it.
Like
public function authorize(Users $user)
{
return $user->status;
}
Where $user is User Eloquent Model
Not sure what do you want to achieve. Request classes should not resolve models since it's not their responsibilities.
If you need that, I'd recommend to create a FormRequest singleton with getters, that will recognize your model bindings.
You have to register model bindings to resolve specific model on method - request param must be related with concrete class
FormRequest::bind('user_id', function ($value) {
return User::where('id', $value)->firstOrFail();
});
Now you have two options:
__call() is triggered when invoking inaccessible methods in an object context.
you have to wrap you bindings like so
class FormRequest {
public funciton authorize(User $user);
}
class FormRequestCaller {
private $formRequest;
public function __call($name, $arguments) {
$entity = FormRequest::resolve($name, $arguments);
return $this->$name($entity);
}
public static function resolve($bindingName, $data) {
return call_user_func_array($bindings[$bindingName], $data);
}
}
-
class FormRequest {
public funciton authorize() {
$user = $this->getUser();
}
public function __call($name, $arguments) {
return FormRequest::resolve(ucfirst(str_replace('get','',$name), $arguments));
}
public static function resolve($bindingName, $data) {
return call_user_func_array($bindings[$bindingName], $data);
}
}
Of course, this should has to be adjusted, but you should get the point.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.