简体   繁体   English

Laravel 不保存一对多关系

[英]Laravel doesn't save one-to-many relation

Laravel doesn't save one-to-many relation Laravel 不保存一对多关系

I tried using the push() and the add() but it just doesn't work even after using the save() method.我尝试使用 push() 和 add() 但即使在使用 save() 方法后它也不起作用。

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 !我附上了代理和合同 model 代理应该有多个合同或根本没有合同这是我的解决方案,但合同没有保存给代理!

//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($request->contract); // dd($contractId); // dd($contractId); $agent->save(); $agent->保存(); $contract = Contract::find($contractId); $contract = Contract::find($contractId); //dd($contract); //dd($合约); // $tier = Tier::find($tierId); // $tier = Tier::find($tierId); // $class = Classes::find($classId); // $class = Classes::find($classId); // $status = Status::find($statusId); // $status = Status::find($statusId); $agent->contracts()->save($contract); $agent->contracts()->save($contract); $contract->save(); $合约->保存(); //dd($contract); //dd($合约);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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