我正在尝试在Laravel 5中保存表格,每行包括2个字段,第一个是包裹名称,第二个字段是基于1个月,3个月,6个月和1年的包裹价格。但是我收到以下错误:
“ htmlspecialchars()期望参数1为字符串,给定数组 (视图:I:\ xampp \ htdocs \ app \ resources \ views \ subscriptions \ checkout.blade.php)“
该视图显示字段,但未将输入保存到数据库中。
包控制器
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\models\Package;
use App\models\Package as AppPackage;
use App\models\Subscription;
use App\User;
use App\Invoice;
use App\Item;
use Auth;
use Lang;
use DB;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Input;
class PackagesController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$packages = AppPackage::orderBy('id', 'ASC')->get();
$packages->item_name = $packages->pluck('item_name')->all();
$packages->amount = $packages->pluck('amount')->all();
session()->put('info', Lang::get('messages.packages_info_warning'));
return view('packages.index')->with('packages', $packages);
}
public function save_packages(Request $request)
{
$this->validate($request, [
//'item_name' => 'required|string|max:255',
//'amount'=> 'required|int'
'item_name' => 'required',
'amount'=> 'required'
]);
$packages = DB::table('package')->get();
$id = null;
$id = $packages->first();
if(is_null($id)) {
ksort($packages);
foreach ($packages as $package) {
$packages_name[0] = $request->item_name[0];
$packages_name[1] = $request->item_name[1];
$packages_name[2] = $request->item_name[2];
$packages_name[3] = $request->item_name[3];
$packages_amount[0] = $request->amount[0];
$packages_amount[1] = $request->amount[1];
$packages_amount[2] = $request->amount[2];
$packages_amount[3] = $request->amount[3];
DB::table('package')->insert([
['item_name' => $packages_name[0], 'amount' => $packages_amount[0], 'id' => 1],
['item_name' => $packages_name[1], 'amount' => $packages_amount[1], 'id' => 2],
['item_name' => $packages_name[2], 'amount' => $packages_amount[2], 'id' => 3],
['item_name' => $packages_name[3], 'amount' => $packages_amount[3], 'id' => 4],
]);
}
return redirect()->back();
}
if(!is_null($id)) {
$id1 = $packages[0]->id;
$id2 = $packages[1]->id;
$id3 = $packages[2]->id;
$id4 = $packages[3]->id;
$packages_name[0] = $request->item_name[0];
$packages_name[1] = $request->item_name[1];
$packages_name[2] = $request->item_name[2];
$packages_name[3] = $request->item_name[3];
$packages_amount[0] = $request->amount[0];
$packages_amount[1] = $request->amount[1];
$packages_amount[2] = $request->amount[2];
$packages_amount[3] = $request->amount[3];
DB::table('package')->where('id', $id1)->update(array('item_name' => $packages_name[0], 'amount' => $packages_amount[0]));
DB::table('package')->where('id', $id2)->update(array('item_name' => $packages_name[1], 'amount' => $packages_amount[1]));
DB::table('package')->where('id', $id3)->update(array('item_name' => $packages_name[2], 'amount' => $packages_amount[2]));
DB::table('package')->where('id', $id4)->update(array('item_name' => $packages_name[3], 'amount' => $packages_amount[3]));
}
return redirect()->route('admin.packages.index')->with('alert-success', ' Success');
}
public function get_packages(Request $request)
{
$package_id = $request->get('id');
$packages=AppPackage::where('id','=',$id)->get();
return response()->json(['response' => $packages]);
}
}
包装型号
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Package extends Model
{
protected $table = 'package';
protected $primaryKey = 1;
public $incrementing = false;
protected $fillable = [
'id', 'item_name', 'amount'
];
public function teacher(){
return $this->belongsTo('App\models\Teacher');
}
public function subscription(){
return $this->belongsTo('App\models\Subscription');
}
public function get_packages(){
return $packages = Package::whereHas('package', function($query){
$query->where('item_number', '=', $this->id);
})->get();
}
}
结帐查看刀片
<div class="form-group">
<label for="packages">Select A Package:</label>
<select name="packages" class="form-control" style="width:250px">
<option value="">--- Select Package ---</option>
@foreach ($packages as $key => $value)
<option value="1">{{$packages->item_name}} - {{$packages->amount}}</option>
@endforeach
</select>
</div>
我正在尝试在下拉菜单中获取所有可用的软件包及其价格,并自动将选择发送的选项值作为参数发送给Paypal,以使客户完成付款。但是,它返回了如上所述的错误。