-1

A method inside the controller in Laravel.

public function getValue()
{
     $dataA[] = TableA::all('name');
     $dataB[] = TableB::all('question');
     for ($i =0; $i <= count($dataA); $i++)
     {
         $data = DB::table($dataA[$i]->name)
             ->select(SELECT * from $dataA[$i]->name)
             ->where($dataB[$i]->question '!=' null)
             ->get();
     }
}

Got

ParseError
syntax error, unexpected '$dataA' (T_VARIABLE), expecting ')' 

Could someone please help to resolve the issue?

Thanks.

EricSchaefer
  • 22,338
  • 20
  • 63
  • 99
  • 2
    I'm no expert, but I think that query must be in quotes. Take a look at examples in the documentation: https://laravel.com/docs/8.x/queries#select-statements – Sergio Tulentsev Apr 19 '21 at 14:29

1 Answers1

0

You need to adjust your select, try using selectRaw() and adding the variable as shown below:

public function getValue()
    {
     $dataA[]=TableA::all('name');
     $dataB[] =TableB::all('question');
     for( $i =0 ;$i <= count($dataA); $i++ )
     {
       $data = DB::table($dataA[$i]->name)
        ->selectRaw('SELECT * from ?', [$dataA[$i]->name])
        ->where($dataB[$i]->question, '!=', null)
        ->get();
}}
Andrew
  • 1,148
  • 8
  • 21
  • changed SELECT to selectRaw. Now, the error 'syntax error, unexpected ''!='' (T_CONSTANT_ENCAPSED_STRING), expecting ')' ' occurs in where($dataB[$i]->question '!=' null). – kovida kumar Apr 19 '21 at 14:42
  • See the updated code, added commas to this line ```->where($dataB[$i]->question, '!=', null)```. – Andrew Apr 19 '21 at 14:45