My error is:
FatalErrorException in 077cf636f32dba5a90c4b83021f7bfea049823d7.php line 0: Method Illuminate\View\View::__toString() must not throw an exception
My route:
Route::get('/delete-status/{status_id}', [
'uses' => 'Classroom#getDeleteStatus',
'as' => 'Status.delete',
'middleware' => 'auth'
]);
My controller:
public function getDeleteStatus($status_id)
{
$status = Status::where('id', $status_id)->first();
$status->delete();
return redirect()->route('class')->with(['message' => 'Successfully deleted!']);
}
My view:
<div style="text-align: right">
Edit ||
Delete
</div>
What should I do?
I think your route should be delete. Route::delete
Route::delete('/delete-status/{status_id}', [
'uses' => 'Classroom#getDeleteStatus',
'as' => 'Status.delete',
'middleware' => 'auth']);
What I did way back then is a Resource controller, but you can use Route::delete
you can check more about resource controller here: https://laravel.com/docs/5.2/controllers
in your view:
<form action="action('Classroom#getDeleteStatus', {{$status_id}})" method="POST">
<input type="hidden" name="_method" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-xs btn-danger pull-left"><i class="fa fa-trash"></i></button>
</form>
then in your controller
public function getDeleteStatus($status_id)
{
$status = Status::where('id', $status_id)->first();
$status->delete();
return redirect()->route('class')->with(['message' => 'Successfully deleted!']);
}
yes i fixed my problem...
my view should be
<div style="text-align: right">
Edit ||
Delete
Related
First, I've checked other question topic, but couldn't find the solution.
when I try to post my form. I am getting this error.
The POST method is not supported for this route. Supported methods:
GET, HEAD.
Form:
<div class="card-body">
<form action="{{route('profile.update', ['id' => $id])}}" method="post">
#csrf
#put
<div class="form-group">
<label for="location">Location</label>
<input class="form-control" type="text" name="location" value="{{$info->location}}">
</div>
<div class="form-group">
<label for="about">About</label>
<textarea name="about" id="about" rows="10" cols="50" class="form-control">{{$info->about}}</textarea>
</div>
<div class="form-control">
<p class="text-center">
<button class="btn btn-primary btn-md" type="submit">Update Your Info</button>
</p>
</div>
</form>
</div>
Routes:
Route::group(["middleware" => "auth"], function(){
route::get("/profile/edit", [
"uses" => "ProfilesController#edit",
"as" => "profile.edit"
]);
route::get("/profile/{slug}", [
"uses" => "ProfilesController#index",
"as" => "profile"
]);
route::put("/profile/update/{id}", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
});
in controller:
public function update(Request $request, $id)
{
dd($request->all());
}
From your question, i can understand that you're trying to update a profile using POST method or may be PUT method earlier. Since, the resource you are editing is unique, you're not passing any parameters for the controller to find that single resource so as to update it.
therefore modify your your route like
route::put("/profile/update/{id}", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
And your form like
<form action="{{route('profile.update', ['id' => $id])}}" method="post">
#csrf
#method('put')
You'll need to pass the ID of the profile you want to update as parameter
then at the controller
public function update(Request $request, $id){
//edit the profile with id = $id
}
You have an error in your form definition
<form class="{{route('profile.update', ['id' => $id])}}" method="post">
should be
<form action="{{route('profile.update', ['id' => $id])}}" method="post">
Since you made a form for PUT request, you have to change
route::post("/profile/update/profile", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
to this
route::put("/profile/update/profile", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
Here is the correction in your provided example.
In form route('profile.update', ['id' => {here you have to place id of record which you want to update}]).
View File
$info->id])}}" method="post">
<div class="form-group">
<label for="location">Location</label>
<input class="form-control" type="text" name="location" value="{{$info->location}}">
</div>
<div class="form-group">
<label for="about">About</label>
<textarea name="about" id="about" rows="10" cols="50" class="form-control">{{$info->about}}</textarea>
</div>
<div class="form-control">
<p class="text-center">
<button class="btn btn-primary btn-md" type="submit">Update Your Info</button>
</p>
</div>
</form>
</div>
In Route
Route::group(["middleware" => "auth"], function(){
route::get("/profile/{slug}", [
"uses" => "ProfilesController#index",
"as" => "profile"
]);
route::get("/profile/edit/profile", [
"uses" => "ProfilesController#edit",
"as" => "profile.edit"
]);
route::post("/profile/update/profile/{id}", [
"uses" => "ProfilesController#update",
"as" => "profile.update"
]);
});
In Controller
public function update(Request $request, $id)
{
dd($id, $request->all());
}
Laravel basics:
I have the following routes:
Route::group(['prefix' => 'pps', 'as' => 'pps.', 'middleware' => ['auth']], function (){
Route::get('/index', 'PPS\PPSController#index')->name('index');
/**
* Templates
*/
Route::group(['prefix' => 'templates', 'as' => 'templates.', 'middleware' => ['auth']], function (){
Route::get('/', 'PPS\Template\TemplateController#index')->name('index');
/**
* Sequence group
*/
Route::group(['prefix' => 'sequenceGroup', 'as' => 'sequenceGroup.', 'middleware' => ['auth']], function (){
Route::get('/', 'PPS\Template\SequenceGroupController#index')->name('index');
Route::get('/create', 'PPS\Template\SequenceGroupController#create')->name('create');
Route::post('/store', 'PPS\Template\SequenceGroupController#store')->name('store');
Route::get('/edit/{sequenceGroup}', 'PPS\Template\SequenceGroupController#edit')->name('edit');
Route::put('/update/{sequenceGroup}', 'PPS\Template\SequenceGroupController#update')->name('update');
Route::delete('/delete/{sequenceGroup}', 'PPS\Template\SequenceGroupController#delete')->name('delete');
});
/**
* Sequence template
*/
Route::group(['prefix' => 'sequenceTemplates', 'as' => 'sequenceTemplates.', 'middleware' => ['auth']], function (){
Route::get('/{sequenceGroup}', 'PPS\Template\SequenceTemplateController#index')->name('index');
Route::get('/create/{sequenceGroup}', 'PPS\Template\SequenceTemplateController#create')->name('create');
Route::post('/store', 'PPS\Template\SequenceTemplateController#store')->name('store');
Route::get('/edit/{sequenceTemplate}', 'PPS\Template\SequenceTemplateController#edit')->name('edit');
Route::put('/update/{sequenceTemplate}', 'PPS\Template\SequenceTemplateController#update')->name('update');
Route::delete('/delete/{sequenceTemplate}', 'PPS\Template\SequenceTemplateController#delete')->name('delete');
});
});
});
When i update the sequence group, everything works well.
But when i will update the sequence template, laravel goes allways to edit method and not to the update method.
Here my form:
<form action="{{ route('pps.templates.sequenceTemplates.update', $sequenceTemplate->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('put') }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="control-label">#lang('pps.name')</label>
<input type="text" name="name" id="name" class="form-control" value="{{ old('name', $sequenceTemplate->name) }}">
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
<div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
<label for="description" class="control-label">#lang('pps.description')</label>
<input type="text" name="description" id="description" class="form-control" value="{{ old('description', $sequenceTemplate->description) }}">
#if ($errors->has('description'))
<span class="help-block">
<strong>{{ $errors->first('description') }}</strong>
</span>
#endif
</div>
<button type="submit" class="btn btn-primary">#lang('pps.save')</button>
</form>
The controller:
public function edit(SequenceTemplate $sequenceTemplate)
{
return view('pps.template.sequenceTemplate.edit', compact('sequenceTemplate'));
}
public function update(UpdateSequenceTemplateRequest $request, SequenceTemplate $sequenceTemplate)
{
$sequenceTemplate->update($request->except('_token', '_method'));
return redirect()->route('pps.templates.sequenceTemplate.index')->withSuccess(__('sequenceTemplateUpdated'));
}
The request:
<?php
namespace App\Http\Requests\PPS\Template;
use Illuminate\Foundation\Http\FormRequest;
class UpdateSequenceTemplateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'sequence_group_id' => 'required|integer',
'name' => 'required|string|min:3',
];
}
}
What is wrong? i do not find the bug.
When you fill the form and press submit button, Laravel validates the data and redirects you back because there is no sequence_group_id in the form and the field is required:
'sequence_group_id' => 'required|integer',
And you don't see any error message because you're not trying to display it for sequence_group_id. To test it put this to the top of the form:
Errors: {{ dump($errors->all()) }}
And try to submit the form.
I have Form in Laravel and when i submit the form to redirect another page("action = panel") with inputs's value. but problem is that when i enter in another's link it displays error. what is wrong?
This is form
this is another page when submit form
this is error when i enter in link again
this is form code:
<form action="{{route('adminPanel')}}" class="form" method="POST">
<p>Name:</p>
<input type="text" name="name"><br>
<p>Password:</p>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Enter As Admin" class="submit">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
this is routes:
Route::get('/', [
'uses' => 'AdminController#getAdminIndex',
'as' => 'index.admin'
]);
Route::post('/panel', [
'uses' => 'AdminController#getAdminPanel',
'as' => 'adminPanel'
]);
this is controller:
class AdminController extends Controller
{
public function getAdminIndex(){
return view('admin/index');
}
public function getAdminPanel(Request $request){
return view('admin/admin', ['name' => $request->name]);
}
}
this is because when you enter an address in address bar, your are actually sending a get request. but you've defined your route with post method!
to fix this you can use any:
Route::any('/panel', [
'uses' => 'AdminController#getAdminPanel',
'as' => 'adminPanel'
]);
and in controller:
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
public function getAdminIndex(){
return view('admin/index');
}
public function getAdminPanel(Request $request){
$name = $request->name ?: Auth::user()->name;
return view('admin/admin', ['name' => $name]);
}
}
Try to use the following statement in form as {{ csrf_field() }}
Sometimes routes may create you an issue. Try below snippet.
<form action="{{route('adminPanel')}}" class="form" method="POST">
<p>Name:</p>
<input type="text" name="name"><br>
<p>Password:</p>
<input type="password" name="password"><br>
<input type="submit" name="submit" value="Enter As Admin" class="submit">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Correct your Routes as below and try.
Route::post('/adminPanel', ['uses' => 'AdminController#getAdminPanel', 'as' => 'adminPanel' ]);
I have the view:
<form class="text-center" action="{{route('PostComment')}}" method="POST">
<div class="form-group">
<textarea class="form-control" name="Comment" id="exampleTextarea" placeholder="Write down your thought here..." rows="4"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
And also the route:
Route::post('/Comment', [
'uses' => 'CommentController#Comment',
'as' => 'PostComment',
'middleware' => 'auth'
]);
And the controller(not so important):
public function Comment(Request $request)
{
$this->validate($request, [
'Comment' => 'required|min:10|max:100',
]); // validation of comment
$NewComment = new Comment();
$NewComment->user_id = Auth::user()->id;
$NewComment->text = $request['Comment'];
$NewComment->save();
return redirect()->route('Debate');
}
My question is that when you submit the form data and you are not logged in, you have to log in but the form isn't submitted?(edited:using laravel auth)
How to make this thing work, you fill the textarea, you forgot to log in, you log in and the form is submited?
I am building an application using Laravel 5.2. I'm trying to create an Edit modal using jquery. However I am getting a 500 internal server error, every time I try to update a record in the database. On further investigation using Firebug I get the error:
"Creating default object from empty value"...
These are the relevant code blocks.
Route.php - The edit route is what I'm trying to access from my modal
<?php
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
})->name('home');
Route::post('/signup', [
'uses' => 'UserController#postSignup',
'as' => 'signup']);
Route::post('/signin', [
'uses' => 'UserController#postSignin',
'as' => 'signin']);
Route::get('/logout', [
'uses' => 'UserController#getLogout',
'as' => 'logout']);
Route::get('/dashboard', [
'uses' => 'PostController#getDashboard',
'as' => 'dashboard',
'middleware' => 'auth'
]);
Route::post('/createpost', [
'uses' => 'PostController#CreatePost',
'as' => 'createpost',
'middleware' => 'auth']);
Route::get('/delete-post/{post_id}', [
'uses' => 'PostController#getDeletePost',
'as' => 'post.delete',
'middleware' => 'auth']);
Route::post('/edit', [
'uses' => 'PostController#getEditPost',
'as' => 'edit'
]);
});
PostController.php
public function getEditPost(Request $request)
{
$this->validate($request, [
'body' => 'required'
]);
$post = Post::find($request['postid']);
$post->body = $request['body'];
$post->update();
return response()->json(['new_body' => $post->body], 200);
}
The Javascript file with the click event, app.js. I am printing a message to the console upon successful update of the database
var postId = 0;
$('.post').find('.interaction').find('.edit').on('click', function (event) {
event.preventDefault();
var postBody = event.target.parentNode.parentNode.childNodes[1].textContent;
postId = event.target.parentNode.dataset['postid'];
$('#post-body').val(postBody);
$('#edit-modal').modal();
});
$('#modal-save').on('click', function () {
$.ajax({
method: 'POST',
url: url,
data: {body: $('#post-body').val(), postId: postId, _token: token}
}).done(function (msg) {
console.log(JSON.stringify(msg));
});
});
This is my view page
dashboard.blade.php
#extends('layouts.master')
#section('content')
#include('includes.message-block')
<section class="row new-post">
<div class="col-md-6 col-md-offset-3">
<header><h3>What do you have to say?</h3></header>
<form action="{{ route('createpost') }}" method="post">
<div class="form-group">
<textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your Post"></textarea>
</div>
<button type="submit" class="btn btn-primary">Create Post</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
</div>
</section>
<section class="row posts">
<div class="col-md-6 col-md-offset-3">
<header><h3>What other people say...</h3></header>
#foreach($posts as $post)
<article class="post" data-postid="{{ $post->id }}">
<p>{{ $post->body }}</p>
<div class="info">
Posted by {{ $post->user->first_name }} on {{ $post->created_at }}
</div>
<div class="interaction">
Like |
Dislike
#if(Auth::user() == $post->user)
|
Edit |
Delete
#endif
</div>
</article>
#endforeach
</div>
</section>
<div class="modal fade" tabindex="-1" role="dialog" id="edit-modal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Edit Post</h4>
</div>
<div class="modal-body">
<form>
<div class="form-group">
<label for="post-body">Edit the Post</label>
<textarea class="form-control" name="post-body" id="post-body" rows="5"></textarea>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" id="modal-save">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script>
var token = '{{ Session::token() }}';
var url = '{{ route('edit') }}';
</script>
#endsection
Please can anyone help me to see what I'm missing here? Thanks
It looks like you get null on $post = Post::find($request['postid']);.
Do some checks before trying to update the model.
You can use ::findOrFail() or check if !is_null($post).
Also you should use $request->input('postid').