Я пытаюсь сохранить форму в Laravel 5. Каждая строка содержит 2 поля. Первое - это название пакета, а второе - цена пакета, основанная на 1, 3, 6 и 1 году.Однако я получаю следующее сообщение об ошибке:
"htmlspecialchars () ожидает, что параметр 1 будет строкой, задан массив (представление: I: \ xampp \ htdocs \ app \ resources \ views \ subscription \ 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();
}
}
Блейд View Checkout
<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 для клиента, чтобы завершить платеж.Однако он возвращает ошибку, как указано выше.