search

Home  >  Q&A  >  body text

Assign data to PHP variables

<p>I want to use the same method to get the data of two variables, but it doesn’t happen</p> <pre class="brush:php;toolbar:false;">function display(){ $datas=Req::all(); $products = Product::all(); return view ('products.dispaly')->with('products', $products,'datas',$datas); }</pre> <p>Here I want to get the data from the table and assign them to two separate variables $datas and $products? What is the correct approach? </p>
P粉256487077P粉256487077488 days ago519

reply all(1)I'll reply

  • P粉718165540

    P粉7181655402023-09-06 10:57:43

    You must use view like this:

    return view(
        'products.dispaly',
        [
            'products' => $products,
            'datas' => $datas,
        ]
    );
    

    Another method that is exactly the same, just less verbose, is to use Compact:

    return view('products.dispaly', compact('products', 'datas'));
    

    reply
    0
  • Cancelreply