Bonjour,

Voila je rencontre un petit problème avec mon code.

Ce que je fais

Décrivez ici votre code ou ce que vous cherchez à faire

Controller :
 public function save(CarsitterRequest $request, Carsitter $carsitter = null)
    {
        $creation = $carsitter ? false : true;
        if ($creation) {
            $carsitter = User::byEmail($request->email)->join('carsitters', 'carsitters.user_id', 'users.id')->select(['carsitters.*'])->first();

            if ($carsitter) {
                return redirect()->route('admin_edit_carsitter', ['carsitter' => $carsitter])->with('validation', 'Ce carsitter existe déjà');
            }
        }
        $validator = $this->getCarsitterValidator($request);

        if ($validator->fails()) {
            return redirect()->back()
                ->withInput()
                ->withErrors($validator);
        }
        $carsitter = Carsitter::edit([
            'email' => $request->input('email'),
            'firstname' => $request->input('firstname'),
            'lastname' => $request->input('lastname'),
            'phone' => $request->input('phone'),
            'personal_address_id'=>Address::updateOrCreateAddress($request->input("particular-personal-address_json")),
            'email_docusign' => $request->input('email_docusign'),
            'lang' => $request->input('lang'),
            'state' => $request->input('state'),
            'siret' => $request->input('siret'),
            'fuel_card_id' => $request->input('fuel_card_id'),
            'main_income' => $request->input('main_income'),
        ],
            $carsitter,
            false
        );
        $carsitter->save();

        if (!$carsitter->tookan_fleet_id) {
            $this->sendCarsitterToTookan($carsitter, $request);
        }
        return redirect()->route('admin_edit_carsitter', ['id' => $carsitter->id])->with("validation", "Le carsitter a bien été " . ($creation ? "ajouté" : "modifié"));
    }

    model : 

    public static function edit($properties, $carsitter = null, $autosave = true)
    {
        $properties = array_merge([
            'email' => null,
            'firstname' => null,
            'lastname' => null,
            'phone' => null,
            'email_docusign' => null,
            'personal_address_id'=>null,
            'lang' => null,
            'state' => null,
            'siret' => null,
            'fuel_card_id' => null,
            'main_income' => null,
        ], $properties);
        if (!$carsitter) {
            $user = User::byEmail($properties['email'])->select(['id'])->first();
            if (!$user) {
                $user = User::saveNewUser($properties['email']);
                $particular = $user->particular;
                $particular->personal_address_id = $properties['personal_address_id'];
                $particular->firstname = $properties['firstname'];
                $particular->lastname = $properties['lastname'];
                $particular->telephone = $properties['phone'];
                $particular->save();
            }
            $carsitter = new Carsitter();
            $carsitter->user_id = $user->id;
            $particular = $user->particular;

            }

        $carsitter->email_docusign = $properties['email_docusign'];
        $carsitter->lang = $properties['lang'];
        $carsitter->state = $properties['state'];
        $carsitter->siret = $properties['siret'];
        $carsitter->fuel_card_id = $properties['fuel_card_id'];
        $carsitter->main_income = $properties['main_income'];
        if ($autosave) {
            $carsitter->save();
        }
        return $carsitter;
    } 

    public function getPersoAddressAttribute()
    {
        return $this->user->particular->personal_address;
    }

    form.blade.php
    <tr>
                <td>Adresse perso</td>
                <td class="address-table-field">
                    <div class="google-auto-complete-component">
                        <input class="address form-control" name="particular-personal-address" value="{{$carsitter->personal_address ? $carsitter->personal_address->formatted_address: ""}}">
                        <input type="hidden" name="particular-personal-address_json" class="address-json" value="{{old("particular-personal-address_json",$carsitter->personal_address? json_encode($carsitter->personal_address->address_json) : "")}}"/>
                    </div>
                </td>
            </tr>

Ce que je veux

Je veux afficher mon formulaire add pour ajouter un carsitter tout les champs fonctionne mais le champs Adresse perso pose problème
Merci pour votre aide

Ce que j'obtiens

Trying to get property of non-object (View: /var/www/html/resources/views/admin/carsitters/form.blade.php) (View: /var/www/html/resources/views/admin/carsitters/form.blade.php)
/var/www/html/storage/framework/views/93e0f7538609bff1f521af6966ccb81dd9651794.php#48

4 réponses


Bonjour,
Si $user vaut false, comment veux-tu récupérer son id et particular ?

Merci pour ta réponse
cette ligne il faut la supprimer: $particular = $user->particular;
c'etais un test quand j'essaie de faire un vard_ump mais $user n'est pas à false la ligne est bien à l'extérieur de if (!$user)

tu sais tout fonctionne sauf quand ke carsitter est null ça marche pas ds l'edit ça marche mais dans le Add non

c'est réglé la variable est vide c'est pour ça