vendredi 24 juin 2016

Laravel inserting into 2 tables

i have 2 tables user and tn_user, table user is a table containing information to log in, i made it by tutorial from https://laravel.com/ so basically it was automatically created, while tn_user is a table that i make by myself

USER TABLE image alt in case u can't see the atribut are id, name, email, password that the important things, email and password in this table is used to logging in

TN_USER TABLE image alt the atribut are cn_id, cv_name, cv_email, cn_phone, cv_position, cv_address, cv_country, cv_username, cv_password, cv_privileges, those are the important thing

based on the form below i want to insert username and password into table user and the rest into table tn_user and how do i do that? im pretty new to laravel so not really quite understand how, usually i use CI

image alt

UserController.php this is where the code i use to insert data i use json response to parse the data and not quite sure how to insert data into 2 tables little help here

public function createOrEdit(){
    //get current user
    $currentUserId = Auth::user()->id;

    $isUpdate = false;
    $id = Input::get('id');
    $user = new UserCompany;
    if($id != ""){
        $user = UserCompany::where('cn_id', '=', $id)->firstOrFail();
        $user->cv_updated_by = $currentUserId;
        $user->cv_updated_at = Carbon::now();
        $isUpdate = true;
    }else{
        $user->cv_created_by = $currentUserId;
        $user->cv_created_at = Carbon::now();
    }
    $user->cv_name = Input::get('name');
    $user->cv_position = Input::get('position');
    $user->cv_email = Input::get('email');
    $user->cn_phone = Input::get('phone');
    $user->cv_address = Input::get('address');
    $user->cv_username = Input::get('username');
    $user->cv_password = Input::get('password');
    $user->cv_country = Input::get('country');

    if($isUpdate){
        UserCompany::where('cn_id','=',$id)->update(['cv_updated_by' => $user->cv_updated_by,
            'cv_updated_at' => $user->cv_updated_at,
            'cv_name' => $user->cv_name,
            'cv_position' => $user->cv_position,
            'cv_email' => $user->cv_email,
            'cn_phone' => $user->cn_phone,
            'cv_country' => $user->cv_country,
            'cv_username' => $user->cv_username,
            'cv_password' => $user->cv_password,
            'cv_address' => $user->cv_address]);

    }else{
        $user->save();
    }

    $returnedData = UserCompany::all();

    $response = array(
            'content' => $returnedData,
            'status' => 'success',
        );

    return Response::json($response);
}

UserCompany.php is my model but since im new im not really understand how to use relationship yet

<?php namespace Activity;

use Illuminate\Database\Eloquent\Model;

class UserCompany extends Model {

protected $table = 'tn_user';
public $timestamps = false;

protected $fillable = [

];

/*public function usercompany(){
    return $this->belongsTo('Activity\user');
}*/

}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/28UYA61
via IFTTT

Aucun commentaire:

Enregistrer un commentaire