How valid is this query in laravel 5.6

0
$id_program_= DB::table('Programs')->select('id_Program')
         ->where("name","=",$row['name'])
         ->first('id_Program')->id_Program 

Erro: Trying to get property 'Program_id' of non-object

    
asked by Israel gtz 21.09.2018 в 20:40
source

3 answers

1

If, as it seems, you want to get the value of a column only ( id_Program ).

The documentation says the following in the section entitled: Retrieving A Single Row / Column From A Table :

  

If you do not even need a full row, you can extract a single   value of a record using the value method. This method   will return the value of the column directly:

$email = DB::table('users')->where('name', 'John')->value('email');

So, to get the value of the id_Program column, you can do this simply:

$id_program_= DB::table('Programs')->
         ->where('name',$row['name'])
         ->value('id_Program'); 
    
answered by 21.09.2018 в 20:55
0
$id_program_= DB::table('Programs')->select('id_Program')<br/>
         ->where("name","=",$row['name'])<br/>
         ->first();

To get the result it would be $id_program->id_Program;

    
answered by 23.09.2018 в 18:19
0

You can try this.

$sql= DB::table('Programs')
->select('id_Program')
->where("name","=",$row['name'])
->get(); 

$id_Program = $sql->first()['id_Program'];
    
answered by 24.09.2018 в 21:49