Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

noblemfd's avatar

store Eloquent Master Detail on same page

I have two models,blade page and controllers I have for implementing the master-detail page in Laravel-5.8. I would like to know how I can store detail part to database.

Master Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Order  extends Model
{
    protected $fillable = [
        'id',
        'name',
      ];

public function detail()
{
    return $this->hasMany('App\OrderDetail','order_id','id');
}   

Detail Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class OrderDetail extends Model
{
   protected $fillable = [
        'id',
        'order_id',
        'item',
        'total',
    ];
}

create.blade view

     <div class="row">
         <div class="clearix"></div>
        <div class="col-md-12">
            <div class="tile">
                <h3 class="tile-title">Order</h3>
                <div class="tile-body">
                    <form  method="POST" action="{{route('order.store')}}">
                        @csrf
                        <div class="form-group col-md-8">
                            <label class="control-label">Name</label>
                            <input name="name" class="form-control @error('name') is-invalid @enderror" type="text" placeholder="Enter Name">
                            @error('name')
                            <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>

                    <table class="table table-bordered">
                        <thead>
                        <tr>
                            <th scope="col">Item Name</th>
                            <th scope="col">Total</th>
                            <th scope="col"><a class="addRow"><i class="fa fa-plus"></i></a></th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr>
                            <td><input type="text" name="item[]" class="form-control qty" ></td>
                            <td><input type="text" name="total[]" class="form-control price" ></td>
                            <td><a   class="btn btn-danger remove"> <i class="fa fa-remove"></i></a></td>
                         </tr>
                        </tbody>
                    </table>

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

@endsection



<script type="text/javascript">
    $(document).ready(function(){
        $('.addRow').on('click', function () {
            addRow();

        });

        function addRow() {
            var addRow = '<tr>\n' +
'                                <td><input type="text" name="item[]" class="form-control item" ></td>\n' +
'                                <td><input type="text" name="total[]" class="form-control total" ></td>\n' +
'                                <td><a   class="btn btn-danger remove"> <i class="fa fa-remove"></i></a></td>\n' +
 '                             </tr>';
            $('tbody').append(addRow);
        };

        $('.remove').live('click', function () {
            var l =$('tbody tr').length;
            if(l==1){
                alert('you cant delete last one')
            }else{

                $(this).parent().parent().remove();

            }

        });
    });

</script>

Controller

class OrderController extends Controller
{

    public function store(Request $request)
    {
        $order = new Order();
        $order->name = $request->order;
        $order->save();

         return redirect('order/'.$order->id)->with('message','Order created Successfully');
    }
}

I have successfully stored the Order. How do I loop the OrderDetail into it. Both for store and update?

Thanks

0 likes
5 replies
jlrdw's avatar

See https://laracasts.com/discuss/channels/tips/invoice-system-database-schema

Bring up the invoice, master and then add the line items detail.

Alt image

Just example data.

A little JS and ajax you can do a popup edit, or use a modal.

Alt image

I'd keep things simple and stick with one to many relations for business apps.

I'd advise getting a good visual query designer, and see:

https://www.mysqltutorial.org/

Just examples. As far as saving you will find a section under relationships in the documentation that covers saving related models. You are basically using the foreign keys.

But the example is pretty good that's in the eloquent relationships chapter.

Basically you find the parent then save the child, or regular SQL you find a parent, get the ID, and the saved foreign key in the child is parent ID. Just basic one to many relation.

noblemfd's avatar

@jlrdw - What you have shown me is get request. It's not posting anything to the database. I need Master detail dynamic data input. Thanks

jlrdw's avatar

I have the add new button which is a post request that's done behind the scenes using Ajax.

And an edit is using put request.

You just have to work out your Ajax.

In the add new it's a div that's not shown until needed. You could even do it in a pure CSS modal.

MichalOravec's avatar
Level 75

When you have relationship hasMany name it as details and not detail

class OrderController extends Controller
{
    public function store(Request $request)
    {
        $order = Order::create([
            'name' => $request->name
        ]);

        foreach ($request->items as $key => $item) {
            $order->details()->create([
                'item' => $item,
                'total' => $request->totals[$key]
            ]);
        }

        return redirect("order/{$order->id}")->with('message', 'Order created Successfully');
    }
}

Also if you have an array of items name it items[] and not item[]

1 like
noblemfd's avatar

@michaloravec - Kindly pardon me, if I want to do the same thing for update, how do I go about that

class OrderController extends Controller
{
  public function update(Request $request, $id)
  {
     $order = new Order::findOrFail($id);
     $order->name = $request->order;
     $order->save();


    return redirect("order/{$order->id}")->with('message', 'Order updateded Successfully');
  }
}

Please or to participate in this conversation.