0

I want to add insert data in three different tables at the same time. But once the data is inserted in the first table, I need to get that id and continue with that to insert data in the other two table. These are the applicant table, language table (each applicant may know more than one language) and skills table (also one or many).

Below is the code that I have been trying:

Models:

class Apply extends Eloquent {

protected $table = 'application';
protected $fillable = ['user_id', 'gender', 'dob', 'ic', 'marriage_status', 'phone', 'city', 'street', 'course', 'university',
    'academic_degree', 'uni_start_date', 'uni_end_date', 'addinfo', 'working_company', 'position', 'start_date', 'end_date', 'reason'];

public function lang() {
    return $this->hasMany('Lang');
}
}

class Lang extends Eloquent 
{
    protected $table = 'language';

public function apply(){
    return $this->belongsTo('Apply');
}
}

class Skills extends Eloquent 
{
    protected $table = 'skills';

public function apply(){
    return $this->belongsTo('Apply');
}
}

Controller:

public function createcv() {
    $input = Input::all();
    $rules = array('ic' => 'required',
        'phone' => 'required',
        'course' => 'required');
    $v = Validator::make($input, $rules);

    if ($v->passes()) {
        $apply = Apply::create(array('user_id' => Auth::user()->id, 'gender' => $input['gender'],
                    'dob' => $input['dob'], 'ic' => $input['ic'], 'marriage_status' => $input['mstatus'],
                    'phone' => $input['phone'], 'city' => $input['city'], 'street' => $input['street'],
                    'course' => $input['course'], 'university' => $input['institution'],
                    'academic_degree' => $input['adegree'], 'uni_start_date' => $input['uni-startdate'],
                    'uni_end_date' => $input['uni-enddate'], 'addinfo' => $input['addinfo'],
                    'working_company' => $input['jobinstitution'], 'position' => $input['jobposition'],
                    'start_date' => $input['startdate'], 'end_date' => $input['enddate'], 'reason' => $input['reason']));
        $apply->save();

        $app_id = $apply->id;
        $lang = Lang::create(array('app_id' => $app_id, 'lang' => $input['lang1'],
                    'reading' => $input['read1'], 'writing' => $input['write1'],
                    'speaking' => $input['speak1'], 'institution' => $input['inst1']
        ));
        $lang->save();

        $skills = Skills::create(array('app_id' => $app_id, 'prog_name' => $input['prog1'],
                    'level' => $input['level1'], 'institution' => $input['instprog1']));
        $skills->save();

        return Redirect::to('home');
    } else {
        return Redirect::to('create-cv')->withInput()->withErrors($v);
    }
}
2
  • What is your question? Commented Jul 18, 2017 at 19:05
  • @JoseRojas thanks for asking. I dont know how to add data on three tables same time. Commented Jul 18, 2017 at 19:39

3 Answers 3

1

If you need a record id to store other, these must not be saved at the same time, because when you save the data is when you can retrieve the id.

When you call the save method of the model you can retrieve the id of the record in the database and if your data has the same keys ($key => $value ) that the name of columns you could use the create method.

from the Laravel Page:

You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment.

Using The Model Create Method

// Create a new user in the database...
$user = User::create(array('name' => 'John'));

// Retrieve the user by the attributes, or create it if it doesn't exist...
$user = User::firstOrCreate(array('name' => 'John'));

// Retrieve the user by the attributes, or instantiate a new instance...
$user = User::firstOrNew(array('name' => 'John'));

So your code would look like this:

$apply = new Apply();
...
$apply->save();
$id_of_your_apply = $apply->id; 
Sign up to request clarification or add additional context in comments.

2 Comments

Than how do I apply this in my code // Create a new user in the database... $user = User::create(array('name' => 'John'));
can you have a look at the edited code. I tried to use the create method but I get the error //Call to undefined method Illuminate\Translation\Translator::create()
0

https://laravel.com/docs/4.2/eloquent#insert-update-delete

public function createcv(){
    $input = Input::all();

    $rules = array('ic' => 'required',
        'phone' => 'required',
        'course' => 'required');
    $v = Validator::make($input, $rules);

    if ($v->passes()) {
        $apply = Apply::create(array('user_id' => Auth::user()->id,'gender' => $input['gender'], 'dob' => $input['dob'], 'ic' => $input['ic'], 'marriage_status' => $input['mstatus'], 'phone' => $input['phone'],'city' => $input['city'],'street'=> $input['street'], 'course' => $input['course'], 'university' => $input['institution'], 'academic_degree' => $input['adegree'],'uni_start_date' => $input['uni-startdate'], 'uni_end_date' => $input['uni-enddate'],'addinfo' => $input['addinfo'], 'working_company' => $input['jobinstitution'],'position' => $input['jobposition'], 'start_date' => $input['startdate'],'end_date' => $input['enddate'], 'reason' => $input['reason']));
        $apply->save();

        **$app_id = $apply->id;**

        $lang = Lang::create(array('app_id' => $app_id,'lang' => $input['lang1'],
           'reading' => $input['read1'], 'writing' => $input['write1'], 
            'speaking' => $input['speak1'],'institution' => $input['inst1']
            ));
        $lang->save();

        $skills = Skills::create(array('app_id' => $app_id,'prog_name' => $input['prog1'], 'level' => $input['level1'], 'institution' => $input['inst1']));
        $skills->save();

        return Redirect::to('home');
}
else {
        return Redirect::to('create-cv')->withInput()->withErrors($v);
    }
}

1 Comment

It does not allow me to do so, I keep getting this error. //Call to undefined method Illuminate\Translation\Translator::create()
0

After studying the code l found out that l have used a Laravel keyword "Lang" which laravel does not allow you to work with. There are many other keyword like: Lang, Events etc. which cant be used. This code works for Adding data to multiple tables (laravel 4)

1 Comment

Yes you need to be cautious about using Laravel or Symfony base class names. They are mostly defined as an Alias at the bottom of your application.php file. They are usually defined in a namespace but aliases makes them easier to call without using namespaces. If you have to use Lang for your application, you can change the Lang=> 'Illuminate\Support\Facades\Lang' to something else. But be aware that once you change global aliases, all references to that alias has to be refactored

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.