Brendan
Brendan

Reputation: 107

Insert into DB based on a Join -> Laravel

I am working on a roll format for a laravel project.

I have successfully created the roll, and can update the roll status in the roll table which looks like this

|id|roll_id|member_id|status|created_at|updated_at|

One of my status is "Present/Voucher" using funds off a Voucher

I have the Voucher Table

|id|member_id|voucher|date|balance|created_at|updated_at|

When a user clicks on the present/voucher icon in the roll, the following is called in the controller

public function voucher($id)
{    
    $r = Roll::find($id);

    if ($r != null)
 {
    $r->status = "V";
    $r->save();

     return redirect(action('RollController@index'))->with ('success', 'Member Paid with Vocuher');
 }
 return redirect(action('RollController@index'));    
}

This works perfect, what I would like to know is how to insert a record into my Voucher table, while I can find the id record in the roll table, I need to pull that member_id so I can add this into the voucher table

For example if the Roll Record looks like this

|id|roll_id|member_id|Status|
|20|2|13|V|

I need to grab 13 are the Member_ID from roll record 20, so I can insert in the vouchers table

|id|member_id|voucher|date|balance
|2|13|Weekly Fees|currdate|-10

Please note the 10 is also stored in a settings table, so if I can grab from that table that would be great

Settings Table:

|id|Setting|Value|
|1|weekly Subs|10|

Thanks

Upvotes: 0

Views: 87

Answers (1)

Kit Loong
Kit Loong

Reputation: 373

You could first define model relationship.

Member

class Member extends Model {
    public function vouchers()
    {
        return $this->hasMany('App\Voucher');
    }
}

Roll

class Roll extends Model {
    public function member()
    {
        return $this->belongsTo('App\Member');
    }
}

Voucher

class Voucher extends Model {
    public function member()
    {
        return $this->belongsTo('App\Member');
    }

    public function setting()
    {
        return $this->belongsTo('App\Setting');
    }
}

Then you can execute fluently in controller, you should also use lockForUpdate and DB::transaction

public function voucher($id)
{    
    DB::transaction(function () use ($id) {
        $roll = Roll::lockForUpdate()->find($id);

        if ($roll != null) {
            $roll->status = "V";
            $roll->save();

            $voucher = new Voucher();
            $voucher->member_id = $roll->member_id;
            ...

            // Correct me if I am wrong, form your table structure it looks like you linked table voucher and setting with 'value'
            // I strongly recommend you to have setting_id as foreign key in table voucher
            // So you could do 
            // $setting = Setting::where('Setting', 'weekly Subs')->where('Value', 10)->first();
            // $voucher->setting_id = $setting->id;
            $voucher->save();

            return redirect(action('RollController@index'))->with ('success', 'Member Paid with Vocuher');
        }
    }
    return redirect(action('RollController@index'));    
}

Upvotes: 1

Related Questions