Laravel multiple data insert into database part 4


OrderController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Orders;
use App\Items;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $orders=Orders::all();
        return view('front_page.view',compact('orders'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data=$request->all();
        $lastid=Orders::create($data)->id;
        if(count($request->product_name) > 0)
        {
        foreach($request->product_name as $item=>$v){
            $data2=array(
                'orders_id'=>$lastid,
                'product_name'=>$request->product_name[$item],
                'brand'=>$request->brand[$item],
                'quantity'=>$request->quantity[$item],
                'budget'=>$request->budget[$item],
                'amount'=>$request->amount[$item]
            );
        Items::insert($data2);
      }
        }
        return redirect()->back()->with('success','data insert successfully');
    }

   

    public function items($id)
    {
        $items=Items::where('orders_id','=',$id)->get();
        return view('front_page.items',compact('items'));
    }
}

Route:
Route::get('/','FrontpageController@home');
Route::post('/orders','OrderController@store');
Route::get('/orders','OrderController@index');
Route::get('/items/{id}','OrderController@items');

view.blade.php:
<!DOCTYPE html>
<html>
<head>
    <title>Multiple data send</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">
</script>
</head>
<body>
<div class="container">
 <table class="table table-bordered">
     <thead>
         <tr>
             <th>SL</th>
             <th>Customer Name</th>
             <th>Order Id</th>
         </tr>
     </thead>
     <tbody>
      @foreach($orders as $key=>$data)
         <tr><td>{{++$key}} </td>
             <td>{{$data->customer_name}} </td>
             <td><a href="/items/{{$data->id}}">{{$data->id}} </a></td>
         </tr>
         @endforeach
     </tbody>
 </table>
</div>
</body>
</html>

items.blade.php:

@extends('layouts.master')
@section('title','view items')
@section('content')
<table class="table table-bordered">
    <thead>
        <tr>
            <th>SL</th>
            <th>Product Name</th>
            <th>Brand</th>
            <th>Quantity</th>
            <th>Budget</th>
            <th>Total Amount</th>
        </tr>
    </thead>
    <tbody>
    @foreach($items as $key=>$item)
        <tr>
            <td>{{++$key}} </td>
            <td>{{$item->product_name}} </td>
            <td>{{$item->brand}} </td>
            <td>{{$item->quantity}} </td>
            <td>{{$item->budget}} </td>
            <td>{{$item->amount}} </td>
        </tr>
    @endforeach
    </tbody>
   
</table>
@endsection
master.blade.php:


<!DOCTYPE html>
<html>
<head>
    <title>@yield('title')</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">
</script>
</head>
<body>
<div class="container">
    @yield('content')
</div>
</body>
</html>


 # if you insert unique product name, this validation code below


  public function store(Request $request)

    {
       $this->validate($request,[
            'product_name'=>'required|unique:items',
            'quantity'=>'required|max:10',
        ]);

        $data=$request->all();
        $lastid=Orders::create($data)->id;
        if(count($request->product_name) > 0)
        {
        foreach($request->product_name as $item=>$v){
            $data2=array(
                'orders_id'=>$lastid,
                'product_name'=>$request->product_name[$item],
                'brand'=>$request->brand[$item],
                'quantity'=>$request->quantity[$item],
                'budget'=>$request->budget[$item],
                'amount'=>$request->amount[$item]
            );
        Items::insert($data2);
      }
        }
        return redirect()->back()->with('success','data insert successfully');
    }

3 comments:

  1. Dear Brother,
    Could you please give me the update code of Controller?
    please I need your update code.if possible please give me at my mail.
    shah.masum2@gmail.com

    Regards
    Masum

    ReplyDelete
  2. why i am getting only one set of data plzz rep

    ReplyDelete
  3. count(): Parameter must be an array or an object that implements Countable

    ReplyDelete

Powered by Blogger.