Laravel validation weird behavior - laravel

i'm validating a form with this:
public function update(UpdateExamenRequest $examenRequest, UpdateResultadoRequest $resultadoRequest)
{
$examenRequest->validated();
$resultadoRequest->validated();
This are the rules in UpdateExamenRequest
public function rules()
{
$this->redirect = url()->previous();
return [
'Paciente_id' => 'required|string|max:10',
'Examen_id' => 'required|string|max:10',
'Nombre' => 'required|string|max:50',
'Descripcion' => 'nullable|string',
];
}
public function messages()
{
return [
'Paciente_id.required' => __('Paciente_id.required'),
'Examen_id.required' => __('Examen_id.required'),
'Nombre.required' => __('Nombre.required'),
'Nombre.string' => __('Nombre.string'),
'Nombre.max' => __('Nombre.max'),
'Descripcion.string' => __('Descripcion.string'),
];
}
My routes:
Route::get('/', function () {
return view('auth.login');
})->middleware('guest');;
Route::get('/welcome', function () {
return view('welcome');
})->name('welcome');
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
require __DIR__ . '/auth.php';
Route::resource('pacientes', PacientesController::class)->middleware('auth');
Route::resource('examenes', ExamenesController::class)->middleware('auth');
Route::resource('resultados', ResultadoController::class)->middleware('auth');
i'm having a situation, and i've really don't know whats happening. The validation sends me to the show view, that i haven't created yet, there is a video: https://youtu.be/S-PtTdUH13Y

Related

Can Laravel route model binding work with nesting for middlewares?

I'm running this code to use my project policy for middlewares. It works as long as {project} is "last in the chain". Is there any way to make it work for deeper levels too?
Route::middleware(['web', 'auth:sanctum', 'verified'])->group(function () {
//...
Route::prefix('project/{project}')->middleware('can:view,project')->group(function () { // This works, but not if I go one more level after this...
Route::get('/', function (Project $project) {
return view('projects::show', [
'project' => $project,
]);
})->name('project');
Route::prefix('settings')->middleware('can:update,project')->group(function () {
// I get 403 here and I don't think I even get through the first middleware...
});
});
});
Either this
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::get('/', function (Project $project) {
return view('projects.show', [
'project' => $project,
]);
});
Route::group([
'prefix' => 'settings',
], function (Project $project) { // this is the one line I am not sure of
// some routes
})->can('update', Project::class);
})->can('view', Project::class);
or
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::get('/', function (Project $project) {
return view('projects.show', [
'project' => $project,
]);
});
Route::group([
'prefix' => 'settings',
], function () use ($project) { // this is the one line I am not sure of
Route::get('/', function (Setting $setting) use ($project) {
return view('project.settings.show', [
'project' => $project,
'settings' => $settings
]);
});
})->can('update', Project::class);
})->can('view', Project::class);
Way I think it should be done
use App\Model\Project;
Route::group([
'prefix' => 'project/{project::id}',
], function () {
Route::middleware(['can:update, project'])->group(function () {
Route::get('settings', function() {
// do something
})
});
})->can('view', Project::class);

Laravel broadcasting a Presence Channel using pusher unable to pass parameters

I've followed the documentation at https://laravel.com/docs/6.x/broadcasting step by step and make sure I copy and paste to be certain I don't make any mistake. I'm able to broadcast just fine and everything is working just fine. Because I'm unable to pass attributes, people in different roomId are counted as if they are all in the same room.
Here is the live example:
https://prayershub.com/worship/82 - 82 is the worship_id I would like to pass to:
Broadcast::channel('worship_presence_channel.{id}', function ($id) {
if(Auth()->check())
{
$profile = Auth()->user()->Profile;
$user = Auth()->user();
$data = [
'id' => $user->id,
'name' => $user->name,
'username' => $user->username,
'avatar' => config('app.storage').$profile->profile_image,
'url' => $profile->profile_url,
'favorite_bible_verse' => $profile->favorite_bible_verse
];
return $id;
}
});
From:
Echo.join(`worship_presence_channel.${id}`)
.here((users) => {
worshipers=users;
joinUser(worshipers);
$('.group-count').html(worshipers.length);
console.log(users);
})
.joining((user) => {
worshipers.push(user);
popupNewUser(user);
joinUser(worshipers);
$('.group-count').html(worshipers.length);
})
.leaving((user) => {
worshipers = worshipers.filter(function(obj) {
return (obj.id !== user.id);
});
popupLeaveUser(user);
joinUser(worshipers);
$('.group-count').html(worshipers.length);
});
I also have an event which seems to be unneccassary but it lools like this:
public function broadcastOn()
{
return new PresenceChannel('worship_presence_channel.58');
}
public function broadcastAs()
{
return 'worship_presence_channel.58';
}
Can anyone please, tell me what i'm doing wrong or if I get the whole thing just wrong. Please help!
I've figured it out, I've change the echo codes above to this:
Broadcast::channel('worship_presence_channel.{id}', function ($user, $id) {
if(Auth()->check())
{
$profile = $user->Profile;
$data = [
'id' => $user->id,
'name' => $user->name,
'username' => $user->username,
'avatar' => config('app.storage').$profile->profile_image,
'url' => $profile->profile_url,
'favorite_bible_verse' => $profile->favorite_bible_verse,
'worships_id' => $id
];
return $data;
}
});
I'm passing 2 parameters $user, $id and it works just as the doc said it would!!!

Laravel Vue DELETE method not supported for this route. Supported methods: GET, HEAD

I have the following code in my actions.js.
export const deleteUser = ({ dispatch }, payload) => {
console.log("El numero de id es js: ", payload.id);
return axios
.delete(window.urls.user.delete(payload.id))
.then(respone => {
dispatch("fetchUsers");
return Promise.resolve();
})
.catch(error => {
return Promise.reject(error);
});
};
api.php
Route::delete('/{user}', [
'uses' => 'UserController#destroy',
'middleware' => 'permission:user.delete',
'can:delete,user'
]);
config.blade.php
user: {
index : '/user',
store: '/user',
update: function(userid){
return '/user/' + userid
},
delete: function(id){
return '/user/' + id;
}
},
Controller
public function destroy(Request $request, $id)
{
$user = User::find($id);
$user->delete();
}
I think the problem is your user route definition. Assuming you don't use route grouping instead of
Route::delete('/{user}', [
'uses' => 'UserController#destroy',
'middleware' => 'permission:user.delete',
'can:delete,user'
]);
you should try:
Route::delete('/user/{user}', [
'uses' => 'UserController#destroy',
'middleware' => 'permission:user.delete',
'can:delete,user'
]);

Laravel Authentication and routing with socialite

Somebody please help me. I am trying to implement Socialite Authentication, everything works fine but routes under Auth is not working.They are redirect back to login page.What is problem with my Auth or Routes. My Route and Controller files are below
Route::group(['middleware' => ['web']], function () {
Route::get('facebook',array(
'as' => 'facebook',
'uses' =>'FacebookController#redirectToProvider'
));
Route::get('Callback', 'FacebookController#handleProviderCallback');
});
Route::group(['middleware' => ['web','auth']], function () {
Route::get('play', function () {
return view('landing');
});
Route::get('go', function () {
return view('landing');
});
});
Controller
public function handleProviderCallback()
{
try
{
$user = Socialize::with('facebook')->user();
}
catch (Exception $e)
{
return Redirect::to('login');
}
$authUser = $this->findOrCreateUser($user);
Auth::login($authUser, true);
$user = Auth::user();
return Redirect::to('play');
}
private function findOrCreateUser($facebookUser)
{
if ($authUser = User::where('id', $facebookUser->getId())->first())
{
return $authUser;
}
return User::create([
'id' => $facebookUser->getId(),
'name' => $facebookUser->getName(),
'email' => $facebookUser->getEmail(),
'gender' => $facebookUser->user['gender'],
'avatar' => $facebookUser->getAvatar(),
'avatar_original' => $facebookUser->avatar_original,
'verified' => $facebookUser->user['verified']
]);
}

Config oauth2 for dingo/api Laravel 4.2 has an error Argument 1 passed to

I am BeanNguyen.
I am a beginer with Laravel framework. So i want to build a webservice RestAPI (laravel 4.2).
I use https://github.com/dingo/api and oauth2 lucadegasperi/oauth2-server-laravel to protect my api. But when i complete all config files and i use Postman ( https://www.getpostman.com/ ) to send request.
I have a error :
*ErrorException (E_UNKNOWN)
Argument 1 passed to Dingo\Api\Auth\LeagueOAuth2Provider::__construct() must be an instance of League\OAuth2\Server\ResourceServer, instance of LucaDegasperi\OAuth2Server\Authorizer given, called in /home/vagrant/Code/webservice/app/config/packages/dingo/api/config.php on line 110 and defined*
So please help me to fix this problem :). This is my config files:
app\routes.php
Route::api('v1', function () {
Route::group(['prefix' => 'protected', 'protected' => true, 'providers' => 'oauth'], function () {
Route::post('user', function () {
$user = API::user();
return $user;
});
});
Route::group(['prefix' => 'unprotected', 'protected' => false], function () {
});
});
app\config\packages\dingo\api\config.php
'auth' => [
'basic' => function ($app) {
return new Dingo\Api\Auth\BasicProvider($app['auth']);
},
'oauth' => function ($app) {
$provider = new Dingo\Api\Auth\LeagueOAuth2Provider($app['oauth2-server.authorizer'], false);
$provider->setUserCallback(function($id) {
return User::find($id);
});
$provider->setClientCallback(function($id) {
return Client::find($id);
});
return $provider;
}
],
app\config\packages\lucadegasperi\oauth2-server-laravel\oauth2.php
'grant_types' => [
'password' => [
'class' => 'League\OAuth2\Server\Grant\PasswordGrant',
'access_token_ttl' => 604800,
// the code to run in order to verify the user's identity
'callback' => function($username, $password){
$credentials = [
'email' => $username,
'password' => $password,
];
if (Auth::once($credentials)) {
return Auth::user()->id;
} else {
return false;
}
}
],
],
and this is my problem:
ErrorException (E_UNKNOWN)
Argument 1 passed to Dingo\Api\Auth\LeagueOAuth2Provider::__construct() must be an instance of League\OAuth2\Server\ResourceServer, instance of LucaDegasperi\OAuth2Server\Authorizer given, called in /home/vagrant/Code/webservice/app/config/packages/dingo/api/config.php on line 110 and defined
Please help me :), thank you very much :)
My config (app\config\packages\dingo\api\config.php) looks like this which is pretty different than yours. On Laravel 4.2.
'auth' => [
'oauth' => function ($app) {
$provider = new Dingo\Api\Auth\LeagueOAuth2Provider($app['oauth2-server.authorizer']->getChecker());
$provider->setUserResolver(function ($id) {
return User::find($id);
});
$provider->setClientResolver(function ($id) {
return Client::find($id);
});
return $provider;
}
]

Resources