lundi 4 janvier 2021

Laravel form request validation return error

i'm using laravel form request validation to validate a request from view to controller. i'm using php artisan make:request SpecializedRequest. but when the validation fail it doesn't return back and it gave error 422. i've looked up at laravel documentation and i don't really understand it. How do i make sure if the validation fail it return back to previous page with error message my Form Request validation

<?php

namespace Modules\Specialized\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Modules\Specialized\Entities\Specialized;

class SpecializedRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required',
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.required' => 'Nama Specialized cannot be empty',
        ];
    }

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
}

my controller

/**
     * Store a newly created resource in storage.
     * @param  SpecializedRequest $request
     * @return Response
     */
    public function store(SpecializedRequest $request)
    {
        $specialized = Specialized::create($request->validated());
        return ($specialized) ? back()->withSuccess('Data has been added') : back()->withError('Something wrong') ;
    }

my blade

<form action="" method="" class="form-horizontal">
    @csrf   
    <fieldset class="content-group">
        <div class="form-group">
            <label for="name" class="control-label col-lg-2">Name</label>
            <div class="col-lg-10">
                <input type="text" name="name" class="form-control" value="">
                <span style="color:red;">  </span>
            </div>
        </div>

    </fieldset>


    <div class="text-right">
        <button type="submit" class="btn btn-primary">Submit <i class="icon-arrow-right14 position-right"></i></button>
    </div>
</form>

tried this same result

public function store(Request $request)
    {
        $data = $request->validate([
            'name' => 'required',
        ]);
        $specialized = Specialized::create($data->validated());
        return ($specialized) ? back()->withSuccess('Data has been added') : back()->withError('Something wrong') ;
    }


via Chebli Mohamed

Getting 405 - Method Not Allowed on some requests while uploading images via BlueImp FileUpload

I am getting 405 - Method not allowed error for some requests when I try to upload images from BlueImp FileUpload plugin. This normally happens when I try to upload more than 6-7 images. These are the requests. Each request represent a separate file upload request

This is the request headers, request and response of one of the failed request



via Chebli Mohamed

Redirect to the another page in laravell app

I have the following code below in which at the end of code i want it to redirect it to another. So, how could i achieve that.

public function edit($id)
{
    $data['menu'] = 'request_payments';

    $data['request_payments'] = $request_payments = RequestPayment::find($id);

   ////// some code here
    return view('admin.RequestPayment.edit', $data);

   // at that point i want it to redirect me to return redirect('admin/request_payments');
}

i also want that edit function to redirect it to the following so how i could do it. Please help me.

 return redirect('admin/request_payments');


via Chebli Mohamed

Laravel 5: Performance with separate indexes in migration

I'm trying to create a new migration for my new table with 4 indexes but encountered some issue

enter image description here

So what I did is, I separated my indexes

enter image description here

Question: Is there any difference in performance if I separated the indexes ?



via Chebli Mohamed

dimanche 3 janvier 2021

I want to redirect afrter complete both insert from laravel request

so I have this store function at my Controller

public function store(Request $request)
{
    Penghuni::create([
        'nama_penghuni' => $request->nama_penghuni,
        'email' => $request->email,
        'phone' => $request->phone,
        'tower' => $request->tower,
        'no_unit' => $request->no_unit
    ]);

    User::create([
        'name' => $request->nama_penghuni,
        'email' => $request->email,
        'password' => Hash::make($request['password']),
        'role' => 'penghuni',
    ]);

    return redirect(route('penghuni.index'));
}

what I want is make sure both insert is success, because the current result I got is when Penghuni create is done but the user is fails it keeps getting redirected

hope someone can help, I use laravel 5.8

thank you



via Chebli Mohamed

Laravel auth doesn't work but all routes are properly created

I created routes and controllers for login and register, but when I try to go to either GET login or POST login, the pages don't show up, and no errors are logged in php_error.log (strange). I did make:auth and the routes and controllers get created, but I cannot get anything useful neither from GET /login or from POST /login requests. On GET on /login I get this error on the page

ErrorException in a19535dcda3ed1d86f3228aa05198ae78974da4f.php line 12:
Undefined variable: errors (View: /Applications/MAMP/htdocs/gos-twilio/server-laravel/resources/views/auth/login.blade.php)
in a19535dcda3ed1d86f3228aa05198ae78974da4f.php line 12
at CompilerEngine->handleViewException(object(ErrorException), '1') in PhpEngine.php line 44
at PhpEngine->evaluatePath('/Applications/MAMP/htdocs/gos-twilio/server-laravel/storage/framework/views/a19535dcda3ed1d86f3228aa05198ae78974da4f.php', array('__env' => object(Factory), 'app' => object(Application))) in CompilerEngine.php line 59

and no errors in php_error.log. On POST /login I can get my own output, from my custom method in AuthController but I doubt anything applies to it, such as automatic connection to the User model, login throttling etc, since I created my own method for this request inside the /http/controller/auth/AuthController.php called protected function login() What am I doing wrong? This is the output of php artisan route:list :

+--------+----------+-------------------------+----------------+-----------------------------------------------------------------+------------+
| Domain | Method   | URI                     | Name           | Action                                                          | Middleware |
+--------+----------+-------------------------+----------------+-----------------------------------------------------------------+------------+
|        | GET|HEAD | /                       | home           | Closure                                                         |            |
|        | GET|HEAD | home                    |                | App\Http\Controllers\HomeController@index                       | auth       |
|        | GET|HEAD | login                   |                | App\Http\Controllers\Auth\AuthController@showLoginForm          | guest      |
|        | POST     | login                   |                | App\Http\Controllers\Auth\AuthController@login                  | guest      |
|        | GET|HEAD | logout                  |                | App\Http\Controllers\Auth\AuthController@logout                 |            |
|        | POST     | password/email          |                | App\Http\Controllers\Auth\PasswordController@sendResetLinkEmail | guest      |
|        | POST     | password/reset          |                | App\Http\Controllers\Auth\PasswordController@reset              | guest      |
|        | GET|HEAD | password/reset/{token?} |                | App\Http\Controllers\Auth\PasswordController@showResetForm      | guest      |
|        | GET|HEAD | register                |                | App\Http\Controllers\Auth\AuthController@showRegistrationForm   | guest      |
|        | POST     | register                |                | App\Http\Controllers\Auth\AuthController@register               | guest      |
|        | POST     | token                   | token-generate | App\Http\Controllers\TokenController@generate                   |            |
+--------+----------+-------------------------+----------------+-----------------------------------------------------------------+------------+


via Chebli Mohamed

How to fetch distinct record From second table in left join

I have three table A,B,C And Structure Is Shown Below

                    Table A
              ----------------------
              |id | text_message   |
              ----------------------
              |1  | first Message  |
              ----------------------
              |2  | second Message |
              ----------------------

                    Table B
           -----------------------------------
           |id  | request_id | approved_status|
           ------------------------------------
           |101 |  1         |    2           |
           ------------------------------------
           |102 |   1        |     1          |
           ------------------------------------
           |103 |   2        |      2         |
           -------------------------------------
           |104 |   2         |     2          |
           -------------------------------------

                       Table C
           --------------------------------------
           |id   |  request_id | approved_status |
           ---------------------------------------
           | 501 |    1        |   1              |
           ----------------------------------------
           |502  |    2        |   2              |
           -----------------------------------------

1)Table B request_id column is foreign key reference Table A id column

2)Table C request_id column is foreign key reference Table A id column

3)Table A has one to many relationship with Table B

4)Table A has one to one relationship with Table C

My question is Below

How to write eloquent query so that We can get all record from Table A and Table C and distinct record i.e distinct request_id from Table B



via Chebli Mohamed