ErrorException Undefined array key 0 (View: C:\Users\Teacher\CNHSSystem\resources\views\section\index.blade.php) - laravel

Hello everyone I was trying to pass value from my resource section blade
<form action="POST" action="{{ ('/import-students') }}">
{{ csrf_field() }}
<div class="form-group">
#foreach($programs as $program)
#if($section->program_id == $program->id)
<input type="hidden" name="strand" value="{{ $program->strand }}">
#endif
#endforeach
<input type="hidden" name="section" value="{{ $section->section_name }}">
<input type="hidden" name="grade_level" value="{{ $section->grade_level }}">
<input type="hidden" name="semester" value="{{ $section->semester }}">
<input type="hidden" name="school_year" value="{{ $section->school_year }}">
</div>
<button type="submit" name="submit">
add
</button>
<!-- <input type="submit" class="text-primary" name="submit"> -->
</form>
to an import blade from another resource
<form method="POST" action="{{ ('/upload-students') }}">
{{ csrf_field() }}
<div class="form-group">
<input type="hidden" name="strand" value="{{ request('strand') }}">
<input type="hidden" name="section" value="{{ request('section') }}">
<input type="hidden" name="grade_level" value="{{ request('grade_level') }}">
<input type="hidden" name="semester" value="{{ request('semester') }}">
<input type="hidden" name="school_year" value="{{ request('school_year') }}">
<input type="file" name="file" class="form-control" />
<button type="submit" class="btn btn-primary mt-1">Save</button>
</div>
</form>
the problem is that the action from the section blade doesn't seem to read the controller
public function importStudents(Request $request)
{
// dd($request);
$strand = $request->input('strand');
$section = $request->input('section');
$grade_level = $request->input('grade_level');
$semester = $request->input('semester');
$school_year = $request->input('school_year');
return view('student.import', $strand, $section, $grade_level, $semester, $school_year);
}
Here's my web route
Route::post('/import-students', [StudentController::class, 'importStudents']);
I'am hoping for your help maam/sirs
and a very big THANK YOU in advance for the help that I can receive maam/sirs.

First, correct forms:
<form action="POST" action="{{ ('/import-students') }}">
this form has two action (method="post" not action) etc. Open this page: https://laravel.com/docs/9.x/blade#forms and see how to write it correctly.
Second, in view('view', $data) the second parameter should contain data (https://laravel.com/docs/5.0/views). So:
return view('student.import', [
'strand' => $strand,
'section' => $section,
'grade_level' => $grade_level,
'semester' => $semester,
'school_year' => $school_year
]);

Related

How to update or create at same time in laravel

``I had one page on that I'm Showing data from two table with left join and also edit button in front of every column when user click to edit button ,it goes a different page where user can update input fields but at the same time i want if some field is not showing data we can create data only when both the id of tables are matching
By this i can easily update but i can't create it shows error
Attempt to assign property "SchemeId" on null
Controller
public function SchConfigrationUpdate(Request $request, $SchemeId)
{
$scheme = tbl_schemeconfigration::find($SchemeId);
$scheme->SchemeId = $request->input('SchemeId');
$scheme->MerchantCode = $request->input('MerchantCode');
$scheme->BankAccountNumber = $request->input('BankAccountNumber');
$scheme->BankAccountIFSC = $request->input('BankAccountIFSC');
$scheme->save();
return redirect()
->back()
->with('success', 'Scheme Update Successfully');
}
**view**
<form action="{{ url('SchConfigration-update/' . $user->SchemeId) }}" method="post"
enctype="multipart/form-data">
#csrf
#method('PUT')
<input type="hidden" name="SchemeId" value="{{ $user->SchemeId }}">
<div class="form_row">
<div class="form_item">
<label>Scheme Name</label>
<input type="text" id="SchemeId" name="SchemeId"
placeholder="Enter scheme name" class="form-control" required
value="{{ $user->SchemeId }}">
{{-- value="{{ request()->SchemeId }}" --}}
{{-- if we want to pass scheme name just pass $user->SchemeName --}}
</div>
<div class="form_item">
<label>Merchant Code</label>
<input type="text" id="MerchantCode" name="MerchantCode"
placeholder="Enter Merchant Code" class="form-control" required
value="{{ $user->MerchantCode }}">
</div>
</div>
<div class="form_row">
<div class="form_item">
<label>Bank Account Number</label>
<input type="text" id="BankAccountNumber" name="BankAccountNumber"
placeholder="Bank account Number" maxlength="17" class="form-control" required
value="{{ $user->BankAccountNumber }}">
</div>
<div class="form_item">
<label>Bank Account IFSC</label>
<input type="text" id="BankAccountIFSC" name="BankAccountIFSC"
placeholder="Bank account IFSC" maxlength="11" class="form-control" required
value="{{ $user->BankAccountIFSC }}">
</div>
</div>
<div class="btn_row">
<input type="submit" value="submit" class="primary_btn">
<a class="btn btn-primary" href="{{ url('SchConfigration') }}"
role="button">Back</a>
</div>
</form>
`
Try This
public function SchConfigrationUpdate(Request $request, $SchemeId)
{
$scheme = tbl_schemeconfigration::updateOrCreate(
['SchemeId' => $request->input('SchemeId')],
[
'MerchantCode' => $request->input('MerchantCode'),
'BankAccountNumber' => $request->input('BankAccountNumber'),
'BankAccountIFSC' => $request->input('BankAccountIFSC')
]
);
return redirect()
->back()
->with('success', 'Scheme Update Successfully');
}

Laravel 6 The POST method is not supported for this route. Supported methods: GET, HEAD

I try to create edit account user form. But i got an error with The POST method is not supported for this route. Supported methods: GET, HEAD notification. Here is my view blade:
Blade.php
<form action="{{ route('account_update.user') }}" method="POST">
#csrf
<input type="hidden" name="id" value="{{ $account->id }}" required>
<input type="text" name="name" value="{{ $account->name }}" required>
<button type="submit">Save</button>
</form>
Here is my routes:
Web.php
Route::post('account/update', 'AccountController#account_update')->name('account_update.user');
And here is my controller
Controller.php
public function account_update(Request $request)
{
DB::table('users')->where('id',$request->id)->update([
'name' => $request->name
]);
return redirect()->route('account.user');
}
Can anyone help me how to fix it?
Use Like that
<form action="{{ url('account/update') }}" method="POST">
#csrf
<input type="hidden" name="id" value="{{ $account->id }}" required>
<input type="text" name="name" value="{{ $account->name }}" required>
<button type="submit">Save</button>
</form>

Getting values to pass in Payfast

I'm trying to intergrate Payfast with my laravel project. The problem I'm having is that the data isn't being passed to Payfast so Payfast keeps giving me an error saying The supplied variables are not according to specification:
Here is my code in my controller
public function payPayfast(Request $request)
{
$data = [
'merchant_id' => $request->merchant_id,
'merchant_key' => $request->merchant_key,
'return_url' => $request->return_url,
'cancel_url' => $request->cancel_url,
'm_payment_id' => $request->m_payment_id,
'amount' => $request->amount,
'item_name' => 'Test Item From Controller',
'item_description' => 'This is a test product',
'email_confirmation' => '1',
'confirmation_address' => '',
'payment_method' => $request->payment_method,
'signature' => $request->signature,
];
return redirect()->to('https://sandbox.payfast.co.za/eng/process')->with('data', $data);
}
here is my code in my confirmation.blade.php
<div class="content_wrapper">
<h1>Order Confrimation</h1>
<div class="row">
<div class="col-lg-12">
<form action="{{ route('payfast.payPayfast') }}" method="POST">
#csrf
<input type="hidden" name="merchant_id" value="11111111">
<input type="hidden" name="merchant_key" value="2222222222222">
<input type="hidden" name="return_url" value="{{ route('payfast.success') }}">
<input type="hidden" name="cancel_url" value="{{ route('payfast.cancel') }}">
<input type="hidden" name="notify_url" value="{{ route('payfast.notify') }}">
<input type="hidden" name="m_payment_id" value="01AB">
<input type="hidden" name="amount" class="completePrice" value="{{ $total }}">
<input type="hidden" name="item_name" value="Test Item">
<input type="hidden" name="item_description" value="A test product">
<input type="hidden" name="email_confirmation" value="1">
<input type="hidden" name="confirmation_address" value="test#test.com">
<input type="hidden" name="payment_method" value="eft">
<?php
$success = url('payfast-success');
$cancel = url('payfast-cancel');
$notify = url('payfast-notify');
$original_str = getAscii('merchant_id=11111111&merchant_key=2222222222222&return_url='.$success.'&cancel_url='.$cancel.'&notify_url='.$notify.'&m_payment_id=01AB&amount='.$totalPrice.'&item_name=Test Item&item_description=A test product&email_confirmation=1&confirmation_address=test#test.com&payment_method=eft');
$hash_str = hash('MD5', $original_str);
$hash = strtolower($hash_str);
?>
<input type="hidden" name="signature" value="{{ $hash }}">
<button class="btn btn-dark" type="submit"><img src="{{ asset('img/eft-payfast.jpg') }}"></button>
</form>
</div>
</div>
</div>
The with function adds the data to the session, but PayFast expects the data to be in the url, e.g. https://sandbox.payfast.co.za/eng/process?amount=100&merchant_id=12&merchant_key=23&item_name=cool.
It doesn't seem like the URLGenerator provides an easy way to turn an array into query parameters. You could simply construct the query using string concatenation or have a look at this answer which includes a function to construct such a path.

Laravel | Delete function - how to delete photo from calendar's event

How can I remove photo from calendar's event in edit calendar's event view? In list of events I did delete method and it works. Now when I try to do the same in edit.blade.php it gives error:
Call to a member function photos() on null
I have two tables in relationship one calendar to many photos, file upload works, but I stucked on edit part.
Look at my controller function:
public function deletePhoto(CalendarRepository $calRepo, $id)
{
$calendars = $calRepo->find($id);
$calendars->photos($id)->delete();
return redirect()->action('CalendarController#edit');
}
and here is fragment of edit.blade.php:
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
#foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}" style="width:100px; height:auto;"/>
</div>
<i class="fas fa-times"></i>Remove
</div>
#endforeach
</div>
</div>
I need to remove photo from Photo table and redirect to edit.blade.php (about the specific event id of the calendar)
Thanks for any help.
EDIT:
<div class="card-body">
<form action="{{ action ('CalendarController#editStore')}}" method="POST" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<input type="hidden" name="id" value="{{ $calendar->id }}"/>
<input type="hidden" name="_token" value="{{csrf_token() }}"/>
<div class="form-group">
<label for="photo">Photo:</label>
<div class="row">
#foreach(($calendar->photos) as $photo)
<div class="col-md-3">
<div class="admin-thumbnail">
<img class="img-responsive" src="/storage/{{ $photo->filename }}"/>
</div>
<form method="POST" action="{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}">
#csrf
#method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
</div>
#endforeach
</div>
</div>
<div class="form-group">
<label for="header">Header</label>
<input type="text" class="form-control" name="header" value="{{ $calendar->header }}"/>
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="{{ $calendar->description }}"/>
</div>
<div class="form-group">
<label for="date">Date</label>
<input type="date" class="form-control" name="date" value="{{ $calendar->date }}"/>
</div>
<input type="submit" value="Save" class="btn btn-primary"/>
</form>
</div>
You use the same $id to find the photo and the calendar instance.
GET request is not recommended for deleting a resource, so a better approach would be in your routes you can have something like this:
Route::delete('photo/{photo}', 'PhotosController#delete')->name('photo.delete');
Then in your view, you should surround the button with a Form, for example:
<form method="POST" action="{{ route('photo.delete', $photo) }}">
#csrf
#method("DELETE")
<a onClick="return confirm('Are you sure?')"><i class="fas fa-times"></i>Remove</a>
</form>
Then your confirm function in JS should submit the form if the user accepts to delete the photo. And also remember to return false as default in the confirm function so it does not submits the form by default.
Your controller will then be:
public function delete(Photo $photo)
{
$photo->delete();
return redirect()->back();
}
--- EDIT
Route::delete('calendar/{calendar}/photo/{photo}', 'CalendarController#deletePhoto')->name('photo.delete');
and the action in the form can be:
{{ route('photo.delete', ['calendar' => $calendar, 'photo' => $photo]) }}
The method in the controller:
public function deletePhoto(Calendar $calendar, Photo $photo)
{
$calendar->photos()->where('id', $photo->id)->delete();
return redirect()->action('CalendarController#edit');
}

Laravel 5.4 - Updating a resource

I'm building a blog post to learn Laravel 5.4 and am struggling to find any examples of how to update a Post anywhere.
My form is as follows
<form method="POST" action="/posts/{{ $post->id }}/edit">
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
My Routes are as follows
Route::get('/posts/{post}/edit', 'PostsController#edit');
Route::patch('/posts/{post}', 'PostsController#update');
And my controller methods are
public function edit( Post $post )
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post )
{
Post::where('id', $post)->update($request->all());
return redirect('home');
}
I get a MethodNotAllowedHTTPException error but am not sure which part / parts of this I am getting wrong.
I'm assuming it must be the point at which I'm using the PATCH function, or potentially just the way I'm mass-assigning the new values. Any help would be greatly appreciated.
you should use
{{ method_field('PATCH') }}
as your form field
and change action to
/posts/{{ $post->id }}
like this:
<form method="POST" action="/posts/{{ $post->id }}">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="exampleInputPassword1" value="{{ $post->title }}" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</form>
There are a couple of things you have missed out.
Firstly, as #Maraboc pointed out in the comments you need to add method spoofing as standard HTML forms only allow for GET and POST methods:
<input type="hidden" name="_method" value="PATCH">
or
{{ method_field('PATCH') }}
https://laravel.com/docs/5.4/routing#form-method-spoofing
Then you will also need to omit the "edit" uri in your forms action:
<form method="POST" action="/posts/{{ $post->id }}">
or
<form method="POST" action="{{ url('posts/' . $post->id) }}">
https://laravel.com/docs/5.4/controllers#resource-controllers
(scroll down a little bit to the Actions Handled By Resource Controller section)
You also may find it helpful to watch https://laracasts.com/series/laravel-5-from-scratch/episodes/10
Hope this helps!
When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.

Resources