mardi 11 octobre 2016

associate() and 1tomany strange behavior

im using Laravel 5.3 and ive a simple controller with a "store" method, this is the "belongTo" side of the relations. The others 2 models contain correctly the "hasMany" function.

public function store(Request $request)
{

    $user_id = JWTAuth::parseToken()->authenticate()->id;
    if(!Vehicle::where('id', '=', $request->vehicle_id)->exists()){
        return $this->response->error('could_not_create_trip_errore_veicolo', 500);
    }
    if(!Ztl::where('id', '=', $request->ztl_id)->exists()){
        return $this->response->error('could_not_create_trip_errore_ztl', 500);
    }
    $request->request->add(['user_id' => $user_id]);

    $trip = new Trip($request->all());

    //$trip->user()->associate($request->user_id);
    //$trip->vehicle()->associate($request->vehicle_id);
    //$trip->ztl()->associate($request->ztl_id);

    if(true)
    {
        if($trip->save()){

            return $this->response->created();
        }else return $this->response->error('could_not_create_trip', 500);
    }else return $this->response->error('could_not_create_trip_current_user_Error', 500);

}

First question is: why if comment or uncomment the "associate" method, nothing changes. Do I need to put these on the controller or I've not weel understand what is the meaning of this method.

Second:

If I send to my controller some data, using form for testing, what is "required" is the 3 foreign keys. If I send a number that is not on my other "hasmany" table an error is rise, but if try to insert something like "2dsksk" where 2 is a correct ID of the "many" table and then a random string, the ID is taken by the insert as 2, this is correct? Validation take just the "correct" number part of the data...the question is, why? and this is secure?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire