0

I am trying to get the query string params. I have tried everything I can find on SO as well as general Google searches such as follows.

URL = http://example.com?companyId=1&subcompany=3;
Then in my controller:
$companyId = $request->query('companyId');
$subcompany = $request->query('subcompany);

So, if I go to mysite.com/api/getcompany?companyId=1&subcompany=3, I see exactly what I expect. I even get back the results that I expect to get. The problem is that then when this passes back to Vue, it is not at all what I expect. Instead, I get a list of all companies that are in the database with a null value for companyId and null value for subcompany. In looking at Telescope, the query that is actually processed is

select * from `prods` where
  (
    `companyId` = '..'
    and `subCompany` = ''
  )
  and `products`.`deleted_at` is null

Yet, in my controller method this is what I am passing in

    $products = Prod::where([
        ['companyId', '=', $companyId ],
        ['subcompany','=', $brandId]
                               ])
        ->get();

In my Vue component, this is how I am making the call,

axios.get("api/getProds").then(res=>{
  console.log(res.data);
});

So, not actually passing in the companyId as I don't have it available in the JS. Instead, I need to extract it from the query string. I am obviously missing something but I can't figure out what it is. Can someone see what is wrong with my code?

Premisoft
  • 177
  • 12
  • Do you mean getting a url param from a browser url to your vue component? – mamaye Feb 08 '20 at 20:27
  • @codervine, Yes, that is what I was trying to do, but wound up solving it differently. Instead, I passed the companyId and subcompany to the Vue component from my blade file as I instantiated the component. – Premisoft Feb 10 '20 at 00:08

1 Answers1

0

you can pass parameter like below

axios.get("api/getProds",{params:{companyId:this.companyId ,subcompany:this.brandId}}).then(res=>{
  console.log(res.data);
});

note: this.companyId,this.subcompany are the props you are creating it in the vue

publick function getProds(Request $request){
 $products = Prod::where('companyId',$request->companyId)->where('subcompany',$request->brandId)->get();
}
OmarSafi
  • 178
  • 1
  • 10