0

I'm attempting to create a simple data display form in Laravel to display contact details given phone number. Phone number is passed in to the form from the URL (route). If the URL is does not include a phone number all the fields of the form should be empty string. To achieve this I just check if the URL is called without a phone number ($number) and if so, create an associative array with the correct keys and empty strings as values

class InboundCallController
{
    public function inboundCall($number=null){
        if($number==null) {
            $contact=array(
                "id"=>"",
                "registration_no"=>"",
                "title"=>"",
                "firstname"=>"",
                "lastname"=>"",
                "primary_contact"=>"",
                "secondary_contact"=>"",
                "email"=>"",
                "gender"=>"",
                "address_line1"=>"",
                "address_line2"=>"",
                "city_state_province"=>"",
                "zip"=>"",
                "country"=>"");

            return view('InboundCall')
                ->with('number',null)
                ->with('contact',$contact);
        }

        $contact=$this->retrieveContact($number);

        return view('InboundCall')
            ->with('number',$number)        //CLI passed from soft-phone
            ->with('contact',$contact)    //Contact details retrieved from db
    }

    private function retrieveContact($number){
        return DB::table('contacts')    //Retrieve contact
            ->where('primary_contact',$number)
            ->first();
    }

}

Trying to get property of non-object (View: /var/www/html/csp/resources/views/InboundCall.blade.php)

On line 37 of the blade I have this:

value="{{$contact->primary_contact}}"
PrashanD
  • 2,353
  • 3
  • 26
  • 51

2 Answers2

3

That's the idea.. If $number is null then you send an associative array to the view, which is not of type object.. It's an array so you can't access the values via ->.. Try accessing the data like this instead in the view, add @ to void promoting error, when $contact is null

value="{{ @$contact['primary_contact'] }}" 

EDIT(for others in the same trouble):

You might also want to consider not sending an object if you have $number and an array otherwise.. You might send a new Contact where Contact is the name of the model and maybe initialize the fields of Contact as empty or make a static method Contact::emptyContact that returns an object of type Contact so you don't have to check the type of data inside the view

csanonymus
  • 1,348
  • 1
  • 12
  • 36
  • To make the `$contact->primary_contact` syntax work irrespective of whether $number is null or not, I cast $contact to an object `(object)$contact` before returning it with the view. – PrashanD Aug 01 '17 at 07:50
  • Or yea, like this.. Although I don't like casting things too much in php :)) – csanonymus Aug 01 '17 at 07:53
  • In which case `$contact=new stdClass; $contact->id=""; contact->registration_no="";` is also valid – PrashanD Aug 01 '17 at 07:58
  • Laravel/PHP novice question: What is the significance of the @$ notation? I just use $ and it works fine – PrashanD Aug 01 '17 at 08:01
  • @PrashanD you can find detailed info [here](https://stackoverflow.com/questions/3551527/at-symbol-before-variable-name-in-php-post) Basically, if somehow `$contact` is null, it will prevent the script from crashing, from what I understand, I don't really know either, never used it... Somebody suggested an edit and I just accepted it :)) – csanonymus Aug 01 '17 at 08:03
  • PHP has a way to suppress and silently ignore errors. :o. It's like asking for trouble – PrashanD Aug 01 '17 at 08:11
  • In a way, yes. It might cause some unexpected behavior.. Check the official docs [here](http://php.net/manual/en/language.operators.errorcontrol.php) – csanonymus Aug 01 '17 at 08:13
1
value="{{$contact->primary_contact}}"

here $contact is an array not object use it like this

value="{{$contact['primary_contact']}}"
Masud Miah
  • 208
  • 1
  • 4