简体   繁体   中英

Laravel record display from database

I have this leftJoin in my Laravel project

products = DB::table('products')
->leftJoin('destinations','products.id','=','destinations.product_id')
->get();

Tables: Product: id,name Destinations:id,product_id,destination,quantity,target_person

but i don't knwo how to display leftJoin,(i just want to display name form product and quantity,destination ,target_person from destinations) and i want to add new record This is my form :

 <form action="." method="POST" id="formid">
    {{ csrf_field() }}
       <div class="form-group">
           <label for="first_name">Product name</label>
           <input class="form-control" name="name" >
       </div>
       <div class="form-group">
           <label for="last_name">Quantity</label>
           <input  class="form-control"  name='quantity'>
      </div>
      <div class="form-group">
          <label for="last_name">Destination<label>
          <input  class="form-control"   name='Destination'>
      </div>
      <div class="form-group">
          <label for="last_name">Target Perosn</label>
          <input  class="form-control"   name='target_person'>
     </div>

  <button type="submit" class="btn btn-primary">Submit</button>
</form>

i never do this,please help me this,im beginner in this.

As you have mentioned, I just want to display name from product and quantity,destination ,target_person from destinations . You have to use table alias like:

products = DB::table('products as t1')
->leftJoin('destinations as t2','t1.id','=','t2.product_id')
->select('t1.name', 't2.quantity', 't2.destination', 't2.target_person')
->get();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM