Laravel datatables raw columns - laravel

i have a code like this to display an action button for datatables
->addColumn('action', function () {
return '<form id="delete" action="{{ route(' . 'admin.posts.destroy' . ', $model) }}" method="POST">
#csrf
#method("DELETE")
Edit
<input type="submit" class="btn btn-danger" value="Delete">
</form>';
})
But the #csrf and #method("DELETE") become a string/text (not method). I tried to append {{ }} in #csrf and #method("DELETE") but it doesn't work. How to change that text to method in blade templates without make a new view for action button like that?
Thank you!

#csrf replace with <input type="hidden" name="_token" value="{{ csrf_token() }}"> and
#method("DELETE") with <input type="hidden" name="_method" value="delete">

Related

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

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
]);

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>

The GET method is not supported for this route. Supported methods: PATCH. when doing update

i want to submit for updating the data, i already use the patch method, but it keep telling me that the get method is not supported
the edit formedit.info.blade
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="patch" enctype="multipart/form-data">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
the route list
web.php
//info penting
route::get('/info-admin','InfosController#index')->middleware('auth','admin')->name('admin.info-admin');
route::get('/tambah-info','PagesController#tambah')->middleware('auth','admin')->name('info.add');
route::patch('/update-info/{info}/update','InfosController#update')->name('info.update');
route::get('/edit-info/{info}/edit','InfosController#edit')->middleware('auth','admin')->name('info.edit');
route::delete('/info/{id}','InfosController#destroy')->middleware('auth','admin')->name('info.destroy');
here's for update logic
InfosController
public function update(Request $request, Info $info) {
Info::where('id', $info->id)->update([
'judul' => $request->judul,
'konten' => $request->konten,
'image' => $request->image,
]);
return redirect('')->route('admin.info-admin')->with('success', 'Successful');
}
what did i do wrong?
HTML5 forms only supports GET, POST and DIALOG method only. so using PATCH won't work.
you have to add it inside the from
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PATCH')
this link will help you to understand form methods.
You have to change the method to POST. Because in some browser PUT/PATCH is not suported
<form action="{{ route('info.update', ['info' => $info->id]) }}" method="post" enctype="multipart/form-data">
<input type="hidden" name="_method" value="patch">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

laravel Route::resource not working for delete

Laravel Route::resource('countries', 'CountriesController'); now working for DELETE
Working only as (separately)Route::delete('/countries/{country}/delete', 'CountriesController#destroy');
<div class="row">
<div class="col-12">
<h1>Details for {{ $country->countryName }}</h1>
<p>Edit</p>
<form action="/countries/{{ $country->id }}/delete" method="post">
<input name="_method" type="hidden" value="DELETE">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete1</button>
</form>
</div>
</div>
not working, I'm stuck at url:
http://192.168.1.7:8000/countries/6/delete
saying '404|not found'
You don't need to append /delete in the URL.
Try this:
<form action="/countries/{{ $country->id }}" method="post">
#method('DELETE')
#csrf
<button type="submit" class="btn btn-danger">Delete1</button>
</form>
As mentioned by #nakov, you can also remove the hidden input field. The blade directive #method('DELETE') is sufficient to do the job.
Hope it helps!

How to add delete confirmation in laraadmin modules?

I am using laraadmin in one of my application but module generated by admin not confirming when deleting any record. Please some one help me How to add delete confirmation in laraadmin modules?
I do not know how to work with laraadmin but You can do that by java script:
Blade:
#foreach (items as item)
<form class="delete" action="{{ route('item.destroy', $item->id) }}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input type="submit" value="Delete">
</form>
#endforeach
JS:
<script>
$(".delete").on("submit", function(){
return confirm("Do you want to delete this item?");
});
</script>
And if you need preety nice confirmation design You can use Sweet Alert JS Library:
https://sweetalert.js.org/guides/

Resources