skip to Main Content

In Laravel Controller file, I have code like this

 $WhiteListEmail = new WhiteListEmail();         
 dd($WhiteListEmail); 

and in chrome browser, it returns

^ SunnyVisionWpCustomSearchModelsWhiteListEmail {#1021
  #connection: "phish"
  #table: "white_list_email"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:1 [
    0 => "email"
  ]

Does
#connection: "phish"
describe it really connected to a database or it just represent
I set this attribute in the model.php file?

I tried to google, but not much news on that.

2

Answers


  1. Fill your table with a single record or more than try this code :

     $WhiteListEmail = WhiteListEmail::get();         
     dd($WhiteListEmail); 
    

    explaination :
    the first row is to get all data that exist in your table, and the second row is to show the data(s) using dd. if you can find the data in the output, then it’s already connected and ready to use.

    note :
    laravel model can be connected automatically if you named your model using the singular form of the table name. eg :

    1. table name = ’employees’
    2. model name = ‘Employee’

    in Laravel naming conventions, model name best written using Camel Case while table name best written using lower case with undescore "_" as word separator.

    Here’s a reference if you want to learn more about Laravel Naming Conventions

    Sorry for my bad english.

    Login or Signup to reply.
  2. use ->exists property to check if a model has been persisted on the database

    $WhiteListEmail = new WhiteListEmail();         
    dd($WhiteListEmail->exists); 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search