laravel post from view does not call controller method - laravel

I am trying to send a post request from my view but somehow this request is not passing to my controller method while other controller methods are working..
my routes:
Route::get('/executes', 'ExecuteController#index')->name('execute.index');
Route::post('/executes', 'ExecuteController#store')->name('execute.store');
Route::get('/executes/create', 'ExecuteController#create')->name('execute.create');
my view:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-6">
<h2>Create Execute</h2>
<form method="post" action="/executes" enctype="multipart/form-data" class="pt-4">
#csrf
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name">
#error('name')
<p class="pt-3 text-danger">
{{ $message }}
</p>
#enderror
</div>
<div class="input-group mb-3">
<div class="input-group-prepend">
<label class="input-group-text" for="map">Map</label>
</div>
<select class="custom-select" id="map" name="map">
<option selected>Choose...</option>
#foreach($maps as $map)
<option value="{{ $map->id }}">{{ $map->name }}</option>
#endforeach
</select>
#error('map')
<p class="pt-3 text-danger">
{{ $message }}
</p>
#enderror
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
#endsection
my controller:
class ExecuteController extends Controller
{
public function create()
{
return view('execute/create', ['maps' => Map::all()]);
}
public function store()
{
// is not even getting here
dd('test');
}
}
any ideas why my method is not getting called?

Could you try to change this
FROM
action="/executes"
TO
action="{{route('/executes')}}"
OR
action="{{url('/executes')}}"
Hope it helps

You're using named routes, so refer to their names when building a URL instead.
Example:
action={{url('execute.store')}}

Please use {{ url('ROUTE_NAME')}} in action or where you want to give link

Use route in the form action as following
action="{{route('execute.store')}}"

guys thank you for your replies but the problem was on the frontend, I had a js included outside the html tag in my layout, somehow that messed up my store method, by fixing that it now works

Related

unable to submit form tag in laravel

unable to call post URL from form tag. When I click on the submit button it is going to a different URL name viewstudentmarks.
<form method="post" style="padding-top:30px;" action="{{ route('updatestudentmark',['id' => $stuid]) }}">
{{csrf_field()}}
<input type="hidden" name="type" value="{{$examtype}}">
<div class="row text-center">
<div class="col-lg-12 col-md-12 col-sm-12">
<div class="row">
<div class="col-lg-12">
<div class="form-group">
</div>
</div>
</div>
</div>
</div>
#foreach($extra as $detail)
<a class="btn btn-success" href='{{ url("viewstudentmarks/{$detail->class_id}") }}'>Back</a>
#endforeach
<input type="submit" class="btn btn-primary">
</form>
and web.php
Route::post('updatestudentmark/{id}','MarksRecordController#update')->name('updatestudentmark');
Route::get('viewhealthdetails/{id}','HealthDetailsController#viewstudents')->name('viewhealthdetails');
In form tag add {{ route('') }} name:
<form method="post" style="padding-top:30px;" action="{{ route('updatestudentmark',['id' => $stuid]) }}">
{{csrf_field()}}
</form>
Also, check your controller code. In the controller, Have you call the redirect method or not.
Thanks
it's much better to use name routes like:
Route::post('updatestudentmark/{id}','MarksRecordController#update')->name('test.route');
and in blade do something like this in action form:
{{route('test.route',['id' => $stuid])}}

419 Sorry, your session has expired. Please refresh and try again.This error appears when i submit the form

I got this error even when I give {{ csrf_field() }} during form submission.
What could be the issue?
<form action="{{ route('e-account-post') }}" method="POST" id="e-account" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="row">
<div class="form-group col-lg-6 col-md-6 col-xs-12">
<div class="field-label">Full Name <span class="required">*</span></div>
<input type="text" class="form-control" name="full_name" value="" placeholder="Full Name">
</div>
<div class="col-12 text-center">
<button class="btn credit-btn mt-30" type="submit">Send</button>
</div>
</form>
below is route
Route::post('/e-account','FrontController#e_account_action')->name('e-account-post');
below is controller
public function e_account_action(Request $request)
{
dd($request->all());
}
Laravel 5.7 and the project is live.
There is no problem in local. I setup the project in live mode through GitLab.

Edit function not working in CRUD form (Laravel)

I've created a CRUD form called 'Groups'. The Create and Delete functions work, but the 'Edit' throws up an error:
"Undefined variable: group (View:
C:\wamp64\www\sites\jointpromote2\resources\views\groups\edit.blade.php
The error points to this line of code:
<input type="text" class="form-control" name="group_name" value="<?php echo e($group->group_name); ?>" />
I've tried several things but nothing fixes the issue.
Here is my http/controllers/GroupController.php
{
$event = Group::find($id);
return view('groups.edit', compact('group'));
}
Here's my database/migrations/create_groups_table.php
public function down()
{
Schema::dropIfExists('groups');
}
And here's the edit.blade.php
#section('main')
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h1 class="display-3">Update a Group</h1>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
<br />
#endif
<form method="post" action="{{ route('groups.update', '$group->id') }}">
#method('PATCH')
#csrf
<div class="form-group">
<label for="group_name">Group Name:</label>
<input type="text" class="form-control" name="group_name" value="{{ $group->group_name }}" />
</div>
<div class="form-group">
<label for="group_description">Group Description:</label>
<input type="text" class="form-control" name="group_description" value="{{ $group->group_description }}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
#endsection
You are using "compact" method in wrong way.
https://www.php.net/manual/en/function.compact.php
try with this
{
$event = Group::find($id);
return view('groups.edit', compact($event));
}
You're passing wrong variable from your GroupController to your edit.blade.php view. Change your GroupController to this:
{
$group= Group::find($id);
return view('groups.edit', compact('group'));
}
All should be working now.
Your variable name is event but you are compacting group and using group variable in blade. Change the variable name in controller and everything will be fine.
public function edit($id) {
$group = Group::find($id);
return view('groups.edit', compact('group'));
}
You write action is in the wrong way. Try copy and paste below code
<form method="post" action="{{ route('groups.update',
$group->id) }}">
You have to remove '.' from $group->id

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');
}

Strange view error on Laravel

I have some cotroller like this:
Route::get('/article/create', 'ArticlesController#create');
and here's my ArticlesController#create:
public function create(){
return view('articles.create',
[
'title'=>'Add Artikel',
'username'=>'Whatever Myname',
'status' => 'Offline'
]
);
}
when i trying to access blog.dev/article/create i got this strange errors:
"Trying to get property of non-object (View: E:\xampp\htdocs\blog\resources\views\articles\single.blade.php)"
how come i can get this kind of error when my view is pointing at articles.create but the error is at single.blade.php which it suppose for ArticlesController#view?
this is what in create.blade.php:
#extends('admin.layout')
#section('content')
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Horizontal Form</h3>
</div>
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/articles">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input class="form-control" id="title" name="title" placeholder="Judul Artikel">
</div>
</div>
<div class="form-group">
<label for="content" class="col-sm-2 control-label">Content</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="content" name="content"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
#endsection
and here's in my single.blade.php :
#extends('admin.layout')
#section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
<hr>
#foreach($article->comments as $comment)
<blockquote>
<p>{{ $comment->comment }}</p>
<small>{{ $comment->created_at->diffForHumans() }}</small>
</blockquote>
#endforeach
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/article/{{ $article->id }}/comment">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="comment" name="comment"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
#endsection
and here's my router:
Route::get('/articles', 'ArticlesController#index')->name('home');
Route::post('/articles', 'ArticlesController#save');
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
Route::post('/article/{article}/comment', 'CommentsController#save');
Route::get('/register', 'RegistrationController#create');
Route::post('/register', 'RegistrationController#store');
I try to change the create function by pointing into another view, but still it showing same error and pointing at single.blade view.
I delete all code at single.blade and write 'test' text, i don't get any errors. but i'm pointing my controller for viewing into create.blade not sigle.blade
After seeing you routes the problem is in this two routes :
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
In this case Laravel will consider the create in your path blog.dev/article/create as an id parameter of the view route here => /article/{id}.
So as a solution you should simply inverse the two routes :
Route::get('/article/create', 'ArticlesController#create');
Route::get('/article/{id}', 'ArticlesController#view');
You need to find what's in the file by reading more than just that line. Maybe you're including that file in the articles.create? Maybe you're accessing a variable that doesn't exist whenever you load the page. Edit your answer with more of the error and what's inside both blades and we can pinpoint what's wrong.

Resources