lundi 25 janvier 2016

Laravel FormRequest - Handling Failure Response

If I inject the FormRequest into the Controller method, such as:

public function createTask(CreateTaskRequest $request)
{
    // ...
}

I can validate all the data inside the CreateTaskRequest as usual using the rules() method. But how can I handle the response myself when the validation fails? There are cases for our API where I want to return an XML response, so I need some sort of way to access the error bag and output all the errors in the XML response.



via Chebli Mohamed

Laravel 5 - inserting old data into form via variable

I have a slight problem. I have a system whereby I can drag and drop my own forms. The html code for a form is saved in my database. When it comes to the edit page, I do something like the following

{!! Form::model($project->document, [
    'class'=>'form-horizontal',
    'method' => 'PATCH',
    'route' => ['projects.documents.update', $project, $document->id]
]) !!}


{!! $documentData->documentData !!}

<div class="form-group">
    {!! Form::submit('Save Data', ['class' => 'btn btn-primary']) !!}
</div>

{!! Form::close() !!}

$documentData->documentData contains the html code for this particular form.

Now my problem is, $documentData->form_data contains the old inputs for this form.

Is there any way to get this old input into the form, the way I am currently handling things?

Thanks



via Chebli Mohamed

Laravel Select records in specific year

I'm new to Laravel and struggling to identify how I can select all records created in 2015 using the created_at field provided by the timestamp.

The model I am using is Blog:

$posts = Blog::latest()->get();

Example date in database:

2015-01-25 12:53:27

Could anyone shed some light?

Thanks!



via Chebli Mohamed

how to validate input array with a same name in laravel controller

I have a problem when a i try to insert record into table like this .

$data = array();
        foreach($input['user_id'] as $key => $user_id) {
            $data[$key]['user_id'] = $user_id;
        }
        foreach($input['start_on'] as $key => $start_on) {
            $data[$key]['start_on'] = $start_on;
        }
        $this->validate($request, [
            'time_start' => 'required|date',
            'time_end' => 'required|date|after:time_start',
        ]);
        $rules = [
            'start_on' => 'required|date|after:time_start|before:time_end|different:start_on'
        ];

        // Iterate and validate each question
        foreach ($data as $value)
        {
            $validator = Validator::make( $value, $rules );

            // if ($validator->fails()) return $validator->messages()->all();
        }

I want to check start_on not equals another start_on.So what can i do?



via Chebli Mohamed

Laravel Storage can't delete file

I can't delete the file uploaded to storage/app/

ErrorException in Local.php line 244:unlink(...app\6d951c00-c27c-11e5-99ea-f72a932e14c5.zip): Permission denied

I have given full control - full read, write, modify permission to user for the storage folder. What more can be the cause?



via Chebli Mohamed

laravel 5.1 using middleware to measure datasize of incoming and outgoing request

i am trying to measure the data size of all incoming and out going request using a middleware in laravel. This will help me know the internet data my application is using. the problem is that i cannot get access to data being sent from my server to the client or browser and i think it is because i am new to laravel. if using a middleware is not possible , an event would also be appreciated although i have a lot of routes thereby making the "events" a tedious process. When i log the log the request, i don't get the data i want. how to get the size of the data into kilobytes will also be helpful.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Response;
use Log;


class MeasureDataSizeMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request);
    }

    public function terminate($request, $response)
    {
// measure the size
    //store the data into a database of incoming and outgoing request.
     //Log::info('app.requests', ['request' => $request, 'response' => new Response($response)]);

       }
    }



via Chebli Mohamed

How to Handle Response Validator Laravel in Android using Retrofit

I have response error(default validator message Laravel) like this:

{
  "email": [
    "The email has already been taken."
  ],
  "no_hp": [
    "The no hp field is required."
  ],
  "type": [
    "The type field is required."
  ]
}

I getting error with code:

RestClient.get().register(name, email, password, nohp, type).enqueue(new Callback<Register>() {
            @Override
            public void onResponse(Response<Register> response) {
                hideDialog();
                // success!
                if (response.isSuccess()) {
                    // request successful (status code 200, 201)
                    Register register = response.body();

                    String email = register.getEmail();
                    Log.d("Respon", "Respon: "+email);
                } else {
                    // response received but request not successful (like 400,401,403 etc)
                    //Handle errors
                    Register register = response.body();

                    String email = register.getEmail();
                    Log.d("Respon", "Responnya: "+email);
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d("Respon", "Error");
                hideDialog();
            }
        });
    }

Register is object which in name, email, password, etc..

How to handle response it?



via Chebli Mohamed