I have a problem to store data in a table. And after hours I still couldnt figure out, what the problem is. I would be so happy for some help!
WishlistController.php:
public function store($book_id)
{
$user_id=Auth::id();
$wishlist=new Wishlist;
$wishlist->book_id=$book_id;
$wishlist->user_id= $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id->id)
->with('success', 'Buch gewünscht');
The Model:
class Wishlist extends Model
{
public $table = 'wishlist';
public $fillable = ['book_id','user_id',];
the view.blade:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
<form action="someaction" method="POST">
<input type="hidden" name="book_id" value="{{$book->id}}"/>
</form>
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD.store', 'WishlistController#store');
When I check the table, nothing new is added.
Its frustrating :-(
Just change post to get in this line:
Route::post('wishlistCRUD.store', 'wishlistCRUD#store');
Try to do this. In your Controller, you don't need $book_id as a parameter because you can get it from the $request:
public function store(Request $request)
{
$user_id = Auth::id();
$book_id = $request->book_id;
$wishlist = new Wishlist;
$wishlist->book_id = $book_id;
$wishlist->user_id = $user_id;
$wishlist->save();
return redirect()->route('wishlistCRUD.show' , $book_id)
->with('success', 'Buch gewünscht');
}
Don't forget to add use Illuminate\Http\Request;
And then in your blade, you don't need to set a route in an anchor, because it is already defined in the form. And you don't need two forms, because Form::open already does that:
{!! Form::open(array('route' => 'wishlistCRUD.store', 'method'=>'POST')) !!}
{{ csrf_field() }}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
{!! Form::submit('wünschen') !!}
{!! Form::close() !!}
This should work.
You have created POST method in your web.php for adding book to wish list, in your blade template you need to submit hidden form on click of link.
in your web.php
Route::get('wishlistCRUD/book/{book_id}','WishlistController#get_book_by_id')->name('get_book_by_id');
Route::post('wishlistCRUD.store', 'WishlistController#store')->name('store');
In your blade template
<a class="btn btn-primary" href="javascript:void(0)" onclick="event.preventDefault();document.getElementById('addToWishlist').submit();">wünschen</a>
<form style="display:none" id="addToWishlist" method="POST" action="{{ route('store')}}">{{csrf_field()}} <input type="hidden" name="bookid" value="{{$book->id}}" /> </form>
in your controller
public function store(Request $request){
$bookID= $request->input('bookid');
$wishlist=new Wishlist();
$wishlist->book_id= $bookID
$wishlist->user_id= Auth::user()->id;
$wishlist->save();
return redirect()->route('get_book_by_id', ['book_id' => $bookID]);
}
public function get_book_by_id(Request $request,$book_id){
// find book by ID;
$book=Book::find($book_id);
// book found
if($book){
return view('book')->with('book',$book);
}else{
// book not found , redirect to 404 page or home page
return redirect('/');
}
}
In blade, change the
<a class="btn btn-primary" href="{{ route('wishlistCRUD.store',$book->id) }}">wünschen</a>
to a <button class="btn btn-primary" type="submit">wünschen</button>
Delete the tag form, it's not necesary. You declared it with the {!! form:open... !!}
You are trying to access to route('wishlistCRUD.store',$book->id) by GET with the <a></a>
Try this:
the view.blade:
{!! Form::open(['url' => 'wishlistCRUD/store', 'method' => 'POST']) !!}
<input type="hidden" name="book_id" value="{{$book->id}}"/>
<button class="btn btn-primary" type="submit">wünschen</button>
{!! Form::close() !!}
the Route:
Route::post('wishlistCRUD/store', 'WishlistController#store');
Related
working with Laravel 6 project and I have following CommentController,
public function update(Request $request, $id)
{
$comment = Comment::find($id);
$this->validate($request, array('comment' => 'required'));
$comment->comment = $request->comment;
$comment->save();
Session::flash('success','Comment Created');
return redirect()->route('posts.show', $comment->post->id);
}
public function delete($id)
{
$comment = Comment::find($id);
return view('comments.delete')->withComment($comment);
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$comment = Comment::find($id);
$post_id = $comment->post->id;
$comment->delete();
Session::flash('success','Deleted Comment');
return redirect()->route('posts.show', $post_id);
}
and My routes are as following
Route::delete('comments/{id}', ['uses' => 'CommentsController#destroy', 'as' => 'comments.destroy']);
Route::get('comments/{id}/delete', ['uses' => 'CommentsController#delete', 'as' => 'comments.delete']);
but when I try to delete comment got following validation error message
The comment field is required.
how could I fix this problem here?
edit.blade.php
#section('content')
<div class="col-sm-6">
<form action="{{ route('comments.destroy', $comment->id) }}" method="post">
#csrf
{{ method_field('PUT') }}
<button type="submit" class="btn btn-danger btn-block">Delete</button>
</form>
</div>
</div>
</div>
#endsection
Because HTML forms can't send PUT, PATCH and DELETE requests, we must "spoof" the request and include an input field that tells Laravel which type it is sending. You can read about that here.
Technically, this means inserting the following for your delete form.
<input type="hidden" name="_method" value="delete">
Laravel comes with different helpers that help you achieve this. In your update form, you have already done this by adding the method_field('PUT');. Here, you are instructing Laravel that this is a PUT request, and we need to do this too in your delete form.
The {{ method_field('PUT') }} simply needs to change to {{ method_field('DELETE') }} or you can use the built-in #method('delete')
Like this
#section('content')
<div class="col-sm-6">
<form action="{{ route('comments.destroy', $comment->id) }}" method="post">
#csrf
#method('delete')
<button type="submit" class="btn btn-danger btn-block">Delete</button>
</form>
</div>
</div>
</div>
#endsection
You can inspect your form HTML in the browser and notice the hidden input field added automatically.
$this->validate($request, array('comment' => 'required'));
In your blade view, if you are not posting a comment in your form, you will be facing this error The comment field is required
Make sure you are posting a comment, or any typo's.
Posting your blade view would help us alot more.
I'm just trying to make a simple form that deletes a model record from the database. I've done it before, but the following code only flashes the success message and redirects. The record never leaves the db.
Here is my form:
#foreach ($users as $user)
<li class="list-group-item {{($loop->iteration % 2 == 0) ? 'even' : 'odd'}}">
<h3>{{$user->first_name}} {{$user->last_name}}</h3>
<b>AccessID: </b> {{$user->id}} <br />
<b>Created: </b> {{$user->updated_at}}
<form action="{{ url('/users/delete', ['id' => $user->id]) }}" method="post">
<input type="hidden" name="_method" value="delete" />
{!! csrf_field() !!}
<button type="submit">Delete</button>
</form>
</li>
#endforeach
Here is my controller method:
public function destroy(User $user)
{
$user->delete();
Session::flash('action', 'Deleted');
Session::flash('status', $user->first_name . $user->last_name .'s access successfully removed.');
return redirect('/users');
}
Here is my route: Route::delete('/users/delete/{id}', 'UserController#destroy');
The answer was that my route parameter needed to be named user for laravel to recognize that it should expect a user object. I had {id} instead of {user}
I cannot display session value in my view. I only want to display it to see if it's correctly set inside the controller. Is it correctly set in controller? How can I check?
I have this in view:
<div class="panel panel-success">
<form action="{{ route('get_table') }}" method="POST">
{{ csrf_field() }}
#foreach($tables as $data)
<button type="submit" name="tables" class="btn btn-primary" value="{{ $data->id }}">
{{$data->name }}
</button>
#endforeach
</form>
{{ Session::get('table_id') }}
</div>
This in ListController:
public function index() {
$tables = DB::table('tables')->get();
return view('welcome', compact('tables'));
}
public function getTable(Request $request) {
$table_id = $request->get('tables');
$request->session()->put('table_id', $table_id);
}
And this in web.php routes:
Route::get('/', 'ListController#index')->name('get_table');
Route::post('/', 'ListController#getTable');
I even tried
public function getTable(Request $request) {
$request->session()->put('table_id', '1234');
}
Nothing shows up in the view at {{ Session::get('table_id') }}
What am I doing wrong?
You can try this:
public function getTable(Request $request) {
$table_id = $request->get('tables');
return redirect()->back()->with('table_id',$table_id);
}
if you want to redirect to specific route then:
public function getTable(Request $request) {
$table_id = $request->get('tables');
return redirect()->route('RouteName')->with('table_id',$table_id);
}
and then in view:
#if(Session::has('table_id'))
{{ Session::get('table_id') }}
#endif
Hope you got your answer
I have a problem with my routes in Laravel (v. 4.2)..
View:
{{ Form::open(['method' => 'DELETE', 'route' => ['admin.users.destroy', $user->user_id]]) }}
<td><button type="submit" style="...">Delete</button></td>
{{ Form::close() }}
Route:
Route::resource('admin/users', 'App\Controllers\Admin\UserIndexController');
Controller:
public function show() {
echo "show";
}
public function destroy() {
echo 'destroy';
}
When the button is clicked though it always prints out "show". Why is this?
HTML forms cannot make PUT, PATCH, or DELETE requests so you need to spoof it with Laravel.
Add this to your form...
<input type="hidden" name="_method" value="delete" />
I believe this generally isn't an issue with newer versions of Laravel since the Form builder has been removed from the core and is now being managed by LaravelCollective which will handle adding this input automatically.
https://laravel.com/docs/4.2/html#opening-a-form
Edited Answer:
Routes.php
Route::resource('admin/users', 'UserIndexController');
{{ Form::open(['method' => 'DELETE', 'route' => ['admin.users.destroy', $user->user_id]]) }}
<td><button type="submit" style="...">Delete</button></td>
{{ Form::close() }}
public function show() {
echo "show";
}
public function destroy() {
echo 'destroy';
}
I am getting "destroy" as output.
Destroy method not working
I am trying to delete an image from my database but I am receiving the following error:
Undefined variable: portfolio (View: C:\MyProjects\bubblehouseProject\resources\views\admin\portfolio\addPortfolio.blade.php)
I believe there is something wrong with the logic I am trying to implement in my Laravel application. I am using Eloquent ORM.
Controller:
public function Destroy($id) {
$portfolio = new Portfolio();
$portfolio::find($id);
$portfolio->delete();
return redirect('addPortfolio')->with('delete', 'The image has been successfully deleted!');
}
Route:
Route::get('addPortfolio/{id}', 'AddPortfolioController#Destroy');
View:
<form action="{{ route('addPortfolioController.Destroy', $portfolio->id) }}">
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
{{ method_field('DELETE') }}
</form>
I am not entirely sure that this is the right way to delete data from the database.
Route
Route::delete('addPortfolio/{id}', 'AddPortfolioController#Destroy')->name('portfolio.destroy');
View
<form action="{{ route('portfolio.destroy', $portfolio->id) }}">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<input type="submit" value="Delete" class="btn btn-danger btn-block" onclick="return confirm('Are you sure to delete?')">
</form>
Controller
public function destroy($id)
{
if(Portfolio::destroy($id)) {
return redirect('addPortfolio')->with('success', 'The image has been successfully deleted!');
} else {
return redirect('addPortfolio')->with('error', 'Please try again!');
}
}