I'm getting
ErrorException in ProjectController.php line 41: Trying to get
property of non-object
I am calling delete_project() inside my controller but it seems Laravel is also calling the get_project($variable_here) method
ProjectsController
public function get_project($slug_name){
$project = Project::where('slug_name', $slug_name)->first();
if ($project->user_id == Auth::user()->id) {
return view('project', ['project' => $project]);
}else {
return redirect('console');
}
}
public function delete_project(){
}
Web routes
Route::get('/console', 'HomeController#index');
Route::get('project/{slug_name}', 'ProjectController#get_project');
Route::get('get_projects', 'UserController#get_projects');
Route::post('create_new_project', 'ProjectController#create_new_project');
Route::post('/delete_project', 'ProjectController#delete_project');
Solved by adding this input on my form to send a delete request
<input type="hidden" name="_method" value="delete">
And changing the delete_project route so I now have this on my web routes
Route::get('/project/{slug_name}', 'ProjectController#get_project');
Route::delete('/project/{slug_name}', 'ProjectController#delete_project');
Related
I'm using Laravel and I am currently implementing the 'destroy' method in a resource controller as part of CRUD. The method should delete the specified record in my database.
The method is called by my blade.php file, which uses a form with the DELETE method and route('my-resources.destroy', $my-resource->id). I want the controller to return a string such as 'Deleted successfully!' so that I can display that to the client.
Here is my code:
In views/book/edit.blade.php:
<form method="DELETE" action="{{ route('books.destroy', $book->id) }}">
<div class="form-item center">
<button type="submit" class="btn-danger">Delete</button>
</div>
</form>
and in BookController.php:
public function destroy($id)
{
$book = Book::find($id);
$book->delete();
return redirect()->away('https://www.google.com');
}
I put a redirect to google.com just to see if the redirect works, but it doesn't. When I click the 'Delete' button, the url changes from http://127.0.0.1:8000/books/1/edit to http://127.0.0.1:8000/books/1? with that question mark at the end. What am I doing wrong?
I have another question: if I want to return the status, should I use something like
Route::get('/', function () {
return 'Deleted successfully';
});
or
$request->session()->flash('status', 'Task was successful!');
/** and some random return statement **/
Thanks!
Forms do not support the DELETE method so you need to use the Laravel #method helper to tell Laravel you want to use the DELETE verb. Additionally, you need to include the csrf token that Laravel expects are part of preventing cross-site request forgeries.
<form action="{{ route('books.destroy', $book->id) }}" method="POST">
#csrf
#method("DELETE")
... // Button/link for submit this form
</form>
You may need to define your route so that it accepts a DELETE request, unless you have defined a resourceful route:
Route::delete('/books/{id}', 'BookController#destroy')
->name('books.destroy'); // Laravel 7
Route::delete('/books/{id}', [\App\Http\Controllers\BookController::class, 'destroy'])
->name('books.destroy'); // Laravel 8
If you're using resourceful routes, they will be made available for you already:
Route::resource('/books', 'BookController'); // Laravel 7
Route::resource('/books', \App\Controllers\Http\BookController::class); // Laravel 8
For your destroy method, set a flash message to be sent back:
public function destroy(Book $id)
{
$id->delete();
return redirect('/')->with('success', 'Book deleted');
}
Then flash the message in your view:
#if (session('success'))
<p>{{ session('success') }}</p>
#endif
am trying to store an ticket using store function in tickets Controller
// Create Ticket
$ticket=new Ticket;
$ticket->userName= $request->input('userName');
$ticket->userEmail= $request->input('userEmail');
$ticket->phoneNumber= $request->input('phoneNumber');
$ticket->regular_quantity= $request->input('regular_quantity');
$ticket->vip_quantity= $request->input('vip_quantity');
$ticket->event_id = $this->route('id');
$ticket->save();
return redirect('/');
}
This is the route
Route::post('ticketstore', 'TicketsController#store')->name('ticketstore');
The form action
<form action="{{route('ticketstore')}}" method="POST">
#csrf
am getting that error
So change this
$this->route('id');
with
$request->route('id');
calling it on this works within FormRequest.
--- EDIT
Now you are trying to get the ID of the event through the request but you are not passing it:
Route::post('ticketstore/{event}', 'TicketsController#store')->name('ticketstore');
Then in your route you should pass the event:
{{route('ticketstore', $event)}}
and you can get it using $request->route('event') or in the method signature like so:
public function store(Request $request, Event $event)
{
...
$ticket->event_id = $event->id;
...
}
Or if you have a dropdown with events in your view just get the event ID from the request $request->event_id;
There are routes
Route::get('posts', 'PostsController#index');
Route::get('posts/create', 'PostsController#create');
Route::get('posts/{id}', 'PostsController#show')->name('posts.show');
Route::get('get-random-post', 'PostsController#getRandomPost');
Route::post('posts', 'PostsController#store');
Route::post('publish', 'PostsController#publish');
Route::post('unpublish', 'PostsController#unpublish');
Route::post('delete', 'PostsController#delete');
Route::post('restore', 'PostsController#restore');
Route::post('change-rating', 'PostsController#changeRating');
Route::get('dashboard/posts/{id}/edit', 'PostsController#edit');
Route::put('dashboard/posts/{id}', 'PostsController#update');
Route::get('dashboard', 'DashboardController#index');
Route::get('dashboard/posts/{id}', 'DashboardController#show')->name('dashboard.show');
Route::get('dashboard/published', 'DashboardController#published');
Route::get('dashboard/deleted', 'DashboardController#deleted');
methods in PostsController
public function edit($id)
{
$post = Post::findOrFail($id);
return view('dashboard.edit', compact('post'));
}
public function update($id, PostRequest $request)
{
$post = Post::findOrFail($id);
$post->update($request->all());
return redirect()->route('dashboard.show', ["id" => $post->id]);
}
but when I change post and click submit button, I get an error
MethodNotAllowedHttpException in RouteCollection.php line 233:
What's wrong? How to fix it?
upd
opening of the form from the view
{!! Form::model($post, ['method'=> 'PATCH', 'action' => ['PostsController#update', $post->id], 'id' => 'edit-post']) !!}
and as result I get
<form method="POST" action="http://mytestsite/dashboard/posts?6" accept-charset="UTF-8" id="edit-post"><input name="_method" type="hidden" value="PATCH"><input name="_token" type="hidden" value="aiDh4YNQfLwB20KknKb0R9LpDFNmArhka0X3kIrb">
but why this action http://mytestsite/dashboard/posts?6 ???
Try to use patch instead of put in your route for updating.
Just a small tip you can save energy and a bit of time by declaring the Model in your parameters like this:
public function update(Post $id, PostRequest $request)
and get rid of this
$post = Post::findOrFail($id);
EDIT
You can use url in your form instead of action :
'url'=> '/mytestsite/dashboard/posts/{{$post->id}}'
Based on the error message, the most probable reason is the mismatch between action and route. Maybe route requires POST method, but the action is GET. Check it.
Try to send post id in hidden input, don't use smt like that 'action' => ['PostsController#update', $post->id]
It contribute to result action url.
I am having an issue with working with post data.
For example, if i have a simple little form :
<form action='/leads/getpost' method='POST'>
<input type='text' name='Domain'>
<input type='submit' name='submit'>
AND then collect the data and try echo it out :
public function getPost()
{
$formData = Request::all() ;
var_dump($formData);
//
}
I get an error : MethodNotAllowedHttpException in RouteCollection.php line 218:
If i do the same thing using GET it works fine.
I tried to edit the VerifyCsrfToken and added :
protected $except = [ 'leads/getpost'
//
];
Still not working.
Try This
Route:
Route::post('leads/getpost', 'nameofController#getPost');
Controller:
use Request;
public function getPost()
{
$formData = Request::all();
var_dump($formData); // or you could return it to the view
}
see docs here about old input
Route::post('/search/all/', function (Request $request) {
//...
$products = $query->paginate(15);
$data = ['products' => $products,
'oldinput' => $request->all()];
return view('inventory.search_products', $data);
});
in the view:
this works:
<input type="text" id="search_all" name="search_all" value="{{ $oldinput['search_all'] }}">
this is always empty:
<input type="text" id="search_all" name="search_all" value="{{ old('search_all') }}">
Just call flush in your controller then you can use old() helper function in your blade.
public function YourController(Request $request){
$request->flash();
return view('yourblade');
}
In blade file:-
<input id="lng" name="lng" value="{{old('lng')}}" type="hidden">
docs says you should flash() then call old() method.
flashing stores the previous request in the session. so it makes sense that old(search_all) doesn't work
I will suggest the following solution:
return view('inventory.search_products', $data)->withInput(\Input::all());
And in blade you can call as well \Input::old('search_all');.