Laravel不会保存一对多关系

时间:2019-10-14 07:36:28

标签: php laravel save one-to-many

Laravel不会保存一对多关系

我尝试使用push()和add(),但即使使用save()方法也无法正常工作。

我附加了Agent和该代理应该具有多个合同或根本没有合同的合同模型 这是我的解决方案,但是合同没有保存到代理商!

//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();

    }

1 个答案:

答案 0 :(得分:1)

这为我解决了 // 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);