简体   繁体   中英

Laravel doesn't save one-to-many relation

Laravel doesn't save one-to-many relation

I tried using the push() and the add() but it just doesn't work even after using the save() method.

I attached the Agent and the Contract model the agent is supposed to have more than one contract or none at all this was my solution but the contracts doesn't get saved to the Agents !

//Agent class
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Agent extends Model
{
    protected $table = "agent";
    protected $fillable = [
        'agent_name', 'agent_website', 'agent_main_contact_name' ,'agent_moto','agent_business', 'tier', 'class', 'status', 'agent_contact_info', 'agent_image_path','contract'
    ];

    // protected static function boot()
    // {
    //     parent::boot();
    //     //fired whenever a new user is created
    //     static::created(
    //         function ($agent){
    //             $agent->contracts()->create([
    //                 'contract_name' => 'No Contract',
    //             ]
    //         );
    //         //Mail::to($agent->email)->send(new NewUserWelcomeMail());
    //         }
    //     );
    // }

    public function contracts(){
        return $this->hasMany(Contract::class)->orderBy('created_at', 'DESC');
    }

}
// Contract model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Contract extends Model
{
    protected $fillable = [
        'contract_name', 'contract_start_date', 'contract_end_date' ,'contract_status','contract_description'];
    protected $table = "contract";
    protected $guarded =[];
    public function agent()
    {
        return $this->belongsTo(Agent::class);
    }

}


//AgentController where the Agent is created
 public function store(Request $request)
    {
        //dd($request->contract);
        $this->validate($request, ['agentName' => 'required', 'agentBusiness' => 'required', 'status' => 'required']);

        $agent = new Agent();
        $agent->agent_name = $request->agentName;
        $agent->agent_website = $request->agentWebsite;
        $agent->agent_main_contact_name = $request->agentMainContactName;
        $agent->agent_moto = $request->agentMoto;
        $agent->agent_business = $request->agentBusiness;
        $tierId = $request->tier;
        $classId = $request->class;
        $statusId = $request->status;
        $agent->agent_contact_info = $request->agentContactInfo;
        $contractId= $request->contract;

        // $data = [
        //     'image' => '',
        // ];
        try
        {
            //dd($request->contract);
           // dd($contractId);
            $contract = Contract::find($contractId);
            //dd($contract);
            // $tier = Tier::find($tierId);
            // $class = Classes::find($classId);
            // $status = Status::find($statusId);
            $agent->contracts->push($contract);

            //dd($contract);


        }
        // catch(Exception $e) catch any exception
        catch(ModelNotFoundException $e)
        {

        }
        $tiers = Tier::select('tier_name')->where('id', $tierId)->get('tier_name');
        foreach($tiers as $tier) {
            $tierName = $tier->tier_name;
        }
        $classes = Classes::select('class_name')->where('id', $classId)->get();
        foreach($classes as $class) {
            $className = $class->class_name;
        }
        $statuss = Status::select('status_name')->where('id', $statusId)->get();

        foreach($statuss as $status) {
            $statusName = $status->status_name;
        }
        $agent->tier = $tierName;
        $agent->class = $className;
        $agent->status = $statusName;



        $agent->save();
        //dd($agent);

        session()->flash('Success', 'Agent Added Successfully');

        return back();

    }

this solved it for me //dd($request->contract); // dd($contractId); $agent->save(); $contract = Contract::find($contractId); //dd($contract); // $tier = Tier::find($tierId); // $class = Classes::find($classId); // $status = Status::find($statusId); $agent->contracts()->save($contract); $contract->save(); //dd($contract);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM