Dynamic Data和Grammar :: columnize()必须是Laravel中的类型数组

时间:2017-11-25 08:10:36

标签: php laravel laravel-5

我已经得到了一个数组的形式,但是当我提交数据时,某些内容仍然显示为错误。这是我的商店功能:

public function store(Shipment $request)
{

    $this->validate(request(), [

        'pro_number' => 'required',
        'shipment_origin' => 'required'

    ]);

    $user_id = Auth::id();

    $input = $request->all();

    //Save Initial Shipment Data
    $shipment = new Shipment();
    $shipment->pro_number = request('pro_number');
    $shipment->shipment_origin = request('shipment_origin');
    $shipment->date = request('date');
    $shipment->due_date = request('due_date');
    $shipment->tractor_id = request('tractor_id');
    $shipment->trailer_id = request('trailer_id');
    $shipment->driver_id = request('driver_id');
    $shipment->notes = request('notes');
    $shipment->shipper_no = request('shipper_no');
    $shipment->ship_to = request('ship_to');
    $shipment->ship_from = request('ship_from');
    $shipment->bill_to = request('bill_to');
    $shipment->bill_type = request('bill_type');
    $shipment->load_date = request('load_date');
    $shipment->shipment_status = 0;
    $shipment->created_by = $user_id;

    $shipment->save();

    //Save Shipment Details
    $data = array();
        foreach ($request->get('shipment_details') as $shipmentDetails){
            $shipmentDetails = new Shipment_Detail(); //Added this recently
                $piecesNumber = $shipmentDetails['piecesNumber'];
                $piecesType = $shipmentDetails['piecesType'];
                $rateType = $shipmentDetails['rateType'];
                $charge = $shipmentDetails['charge'];
                $weight = $shipmentDetails['weight'];
                $hazmat = $shipmentDetails['hazmat'];
                $description = $shipmentDetails['description'];

    $shipment->shipment_details()->save($shipment_detail);
        }
    //Return to Register
    return redirect('/shipments');

}

但每当我去保存它时,我得到以下回复:

  

类型错误:传递给Illuminate \ Database \ Grammar :: columnize()的参数1必须是数组,给定的字符串,在... \ truckin \ vendor \ laravel \ framework \ src \ Illuminate \ Database \中调用在第131行查询\ Grammars \ Grammar.php

我认为错误源于我节省的shipping_details中的某个地方,尽管我并不完全确定在哪里。我可能只是忽略了一些简单的东西,这对我来说是一个新的领域,我还没有掌握这些东西。

更新

如果有帮助,请参阅下面的调试器页面中的POST数据:

_token  

"vrTs9ilq6TjZeOBL9SUjVjBtIHOxEKNfZgAO1YxM"

shipment_origin     

"1"

pro_number  

"212"

date    

"2017-11-24"

due_date    

"2017-12-24"

bill_to     

"1"

ship_to     

"1"

ship_from   

"4"

bill_type   

"1"

shipper_no  

"2122"

load_date   

"2017-11-24"

driver_id   

"1"

tractor_id  

"1"

trailer_id  

"2"

shipment_details    

array:7 [▼
  "piecesNumber" => array:2 [▼
    0 => "1"
    1 => "1"
  ]
  "piecesType" => array:2 [▼
    0 => "1"
    1 => "1"
  ]
  "rateType" => array:2 [▼
    0 => "2"
    1 => "1"
  ]
  "weight" => array:2 [▼
    0 => "1100"
    1 => "120"
  ]
  "hazmat" => array:2 [▼
    0 => "0"
    1 => "0"
  ]
  "description" => array:2 [▼
    0 => "desc1"
    1 => "desc2"
  ]
  "charge" => array:2 [▼
    0 => "1.00"
    1 => "2.00"
  ]
]

fuel_rate_type  

""

fuel_rate_amount    

""

fuel_rate_total     

""

permit_rate_type    

""

permit_rate_amount  

""

permit_rate_total   

""

other_rate_description  

""

other_rate_amount   

""

other_rate_total    

""

notes   

"notes"

comments    

""

更新

Grammar::columnize()的引用来自Laravel附带的以下函数:

public function columnize(array $columns)
{
    return implode(', ', array_map([$this, 'wrap'], $columns));
}

更新

装运控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Shipment;
use Carbon\Carbon;
use App\Origin;
use App\Employee;
use App\Equipment;
use App\Customer;
use App\Shipment_Type;
use App\Notifications\FreightBillNew;
use App\User;
use App\Shipment_Detail;
use App\Shipment_Details_Rate_Type;
use App\Shipment_Details_Piece_Type;
use Illuminate\Support\Facades\Auth;

class ShipmentController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function index()
    {

        $shipments = Shipment::paginate(15);
        return view('shipments.index', compact('shipments'));
    }



    public function archive()
    {
        $shipments = Shipment::latest()->filter(request(['month', 'year']))->get();
//      $shipmentarchives = Shipment::shipmentarchives();

        return view('archives.shipments', compact('shipments', 'shipmentarchives'));
        }



//      $shipments = Shipment::latest()//new request
//          
//          
//          ->filter(request(['month', 'year']))//new request
//          ->get();//new request

//      $shipments = Shipment::latest();
//      
//      
//      if ($month = request('month')) {
//          $shipments->whereMonth('status_completed', Carbon::parse($month)->month);
//      
//      }
//      if ($year = request('year')) {
//          $shipments->whereYear('status_completed',$month);
//      
//      }
//      
//      $shipments = $shipments->get();

//      $shipmentarchives = Shipment::selectRaw('year(status_completed) year, monthname(status_completed) month, count(*) completed')
//          ->groupBy('year','month')
//          ->orderByRaw('min(status_completed) desc')
//          ->where('status', '=', 0)
//          ->get()
//          ->toArray();
//      
//      return view('archives.shipments', compact('shipments','shipmentarchives'));


    public function show(Shipment $shipment)
    {
//      $this->authorize('view', $shipment);

        $shipment_details = $shipment->shipment_details;

        return view('shipments.show', compact('shipment','shipment_details'));
    }

    public function create()
    {
        $origins = Customer::where('origin','1')->get();
        $billtypes = Shipment_Type::all();
        $ratetypes = Shipment_Details_Rate_Type::all();
        $piecetypes = Shipment_Details_Piece_Type::all();
        $tractors = Equipment::where('unit_type','1')->get();
        $trailers = Equipment::where('unit_type','2')->get();
        $drivers = Employee::all();
        $customers = Customer::orderBy('customer_name','ASC')->get();
        $shipto = Customer::pluck('company_name','id')->toArray();
        $shipfrom = Customer::pluck('company_name','id')->toArray();
        return view ('shipments.create', compact ('origins','drivers','tractors','trailers','customers','billtypes','ratetypes','piecetypes'));
    }
    public function store(Shipment $request)
    {

        $this->validate(request(), [

            'pro_number' => 'required',
            'shipment_origin' => 'required'

        ]);

        $user_id = Auth::id();

        $input = $request->all();

        //Save Initial Shipment Data
        $shipment = new Shipment();
        $shipment->pro_number = request('pro_number');
        $shipment->shipment_origin = request('shipment_origin');
        $shipment->date = request('date');
        $shipment->due_date = request('due_date');
        $shipment->tractor_id = request('tractor_id');
        $shipment->trailer_id = request('trailer_id');
        $shipment->driver_id = request('driver_id');
        $shipment->notes = request('notes');
        $shipment->shipper_no = request('shipper_no');
        $shipment->ship_to = request('ship_to');
        $shipment->ship_from = request('ship_from');
        $shipment->bill_to = request('bill_to');
        $shipment->bill_type = request('bill_type');
        $shipment->load_date = request('load_date');
        $shipment->shipment_status = 0;
        $shipment->created_by = $user_id;

        $shipment->save();

        //Save Shipment Details
//       $data = array();

        foreach ($request->get('shipment_details') as $shipmentDetails) {
            $shipmentDetail = new ShipmentDetail();
            $shipmentDetail->pieces_number = $shipmentDetails['piecesNumber'];
            $shipmentDetail->pieces_type = $shipmentDetails['piecesType'];
            $shipmentDetail->rate_type = $shipmentDetails['rateType'];
            $shipmentDetail->charge = $shipmentDetails['charge'];
            $shipmentDetail->weight = $shipmentDetails['weight'];
            $shipmentDetail->hazmat = $shipmentDetails['hazmat'];
            $shipmentDetail->description = $shipmentDetails['description'];

            $shipment->shipment_details()->save($shipment_detail);
            }
        //Return to Register
      return redirect('/shipments');

    }
}

货件型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Shipment extends Model
{
    protected $fillable = [
        'pro_number', 
        'shipment_origin',
        'notes',
        'shipment_status',
        'due_date',
        'date',
        'description',
        'driver_id',
        'tractor_id',
        'trailer_id',
        'shipper_no',
        'ship_to',
        'ship_from',
        'bill_to',
        'bill_type',
        'load_date'
    ];

    public function customers()
    {
        return $this->belongsToMany(Customer::class);
    }

    public function origin() 
    {
        return $this->hasOne('App\Origin', 'id','shipment_origin');
    }    
    public function shipment_details(){
        return $this->hasMany('App\Shipment_Detail');
    }
}

Shipment_Detail Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class shipment_detail extends Model
{
    protected $fillable = [
        'shipment_id',
        'pieces_number',
        'pieces_type',
        'rate_type',
        'weight',
        'hazmat',
        'description',
        'charge'
    ];

    public function shipment()
    {
        return $this->belongsTo('App\Shipment');
    }
}

更新了建议的更改

public function store(Request $request)
    {

        $this->validate(request(), [

            'pro_number' => 'required',
            'shipment_origin' => 'required'

        ]);

        $user_id = Auth::id();

        $input = $request->all();

        //Save Initial Shipment Data
        $shipment = new Shipment();
        $shipment->pro_number = request('pro_number');
        $shipment->shipment_origin = request('shipment_origin');
        $shipment->date = request('date');
        $shipment->due_date = request('due_date');
        $shipment->tractor_id = request('tractor_id');
        $shipment->trailer_id = request('trailer_id');
        $shipment->driver_id = request('driver_id');
        $shipment->notes = request('notes');
        $shipment->shipper_no = request('shipper_no');
        $shipment->ship_to = request('ship_to');
        $shipment->ship_from = request('ship_from');
        $shipment->bill_to = request('bill_to');
        $shipment->bill_type = request('bill_type');
        $shipment->load_date = request('load_date');
        $shipment->shipment_status = 0;
        $shipment->created_by = $user_id;

        $shipment->save();

        //Save Shipment Details
        for ($i = 0; $i < count($shipment_details); $i++) { 
            Shipment_Detail::create([ 
                'shipment_id' => $request->shipment_id, 
                'pieces_number' => $shipment_details['piecesNumber'][$i], 
                'pieces_type' => $shipment_details['piecesType'][$i],
                'rate_type' => $shipment_details['rateType'][$i],
                'charge' => $shipment_details['charge'][$i],
                'weight' => $shipment_details['weight'][$i],
                'hazmat' => $shipment_details['hazmat'][$i],
                'description' => $shipment_details['description'][$i] ]);
            }
        //Return to Register
      return redirect('/shipments');

    }

0 个答案:

没有答案