edit.blade.php :
{!! Form::model($post, array('method'=>'PATCH','url'=>'/posts/{post}'.$post->id)) !!}
{{ method_field('PATCH') }}
{{csrf_field()}}
<label for="title">Titolo</label>
<input type="text" class="form-control" id="title" name="title" value="{{$post->title}}">
<label for="body">Corpo</label>
<textarea id="body" name="body" class="form-control" value="{{$post->body}}">
</textarea>
routes/web.php:
Route::get('/', 'PostsController#index')->name('home');
Route::get('/posts/create', 'PostsController#create');
Route::post('/posts', 'PostsController#store');
Route::get('/posts/{post}', 'PostsController#show');
Route::get('/posts/tags/{tag}', 'TagsController#index');
Route::post('/posts/{post}/comments','CommentsController#store');
Route::get('/posts/{id}/edit', 'PostsController#edit');
Route::get('/edit/{post}', 'PostsController#update');
Route::patch('/post/{post}', 'PostsController#update');
Route::get('/register', 'RegistrationController#create');
Route::post('/register', 'RegistrationController#store');
Route::get('/login', 'SessionsController#create');
Route::post('/login', 'SessionsController#store');
Route::get('/logout', 'SessionsController#destroy');
postController:
public function edit( Post $post )
{
return view('posts.edit', compact('post'));
}
public function update(Request $request, Post $post)
{
Post::where('id', $post)->update($request->all());
return redirect('/home');
}
You're using model binding here, so change it to:
public function update(Request $request, Post $post){
$post->update($request->all());
return redirect('/home');
}
Also, change URL to:
'url' => '/posts/' . $post->id
Also, remove these fields, because {!! Form::model() !!} automatically insert those for you:
{{ method_field('PATCH') }}
{{ csrf_field() }}
Change:
Post::where('id', $post)->update($request->all());
To:
Post::where('id', $post->id)->update($request->all());
new route:
Route::patch('/posts/{id}/edit/{post}', 'UpdateController#update');
{!! Form::model($post, array('method'=>'POST','url'=>'/posts/{id}/edit'. $post->id)) !!}
I think /posts/{id}/edit not working
Related
EDIT FORM
<div class="form-group mb-3">
<label>Country:</label>
<div class="col-sm-4">
<select id="country-dd" name="country[]" class="js-example-basic-multiple form-control" multiple="multiple">
#foreach($countries as $country)
<option value="{{$country->id }}" {{in_array($country->id, explode(',',$user->country)) ? 'selected' : '' }}> {{$country->name}}</option>
#endforeach
</select>
</div>
</div>
CONTROLLER
public function updateuser(Request $request, $id)
{
// dd($request->all());
$request->validate([
'name'=>'required',
'email'=>'required|email|unique:users',
'country'=>'required',
'state'=>'required',
'city'=>'required',
'role_id'=>'required'
]);
$name = $request->name;
$email=$request->email;
$country=implode(',',$request->country);
$state=implode(',',$request->state);
$city=implode(',',$request->city);
$role_id=$request->role_id;
User::whereId($id)->update($request->all());
return redirect()->route('viewuser');
}
ROUTE
Route::put('/updateuser/{id}',[UserController::class,'updateuser'])->name('updateuser');
If I update a record. It just reloads the page and there is no error. Please correct me if I am doing wrong. Thanks in advance.
Try in the CONTROLLER:
public function updateuser(Request $request, $id)
{
$request->validate([
'name'=>'required',
'email'=>'required|email|unique:users',
'country'=>'required',
'state'=>'required',
'city'=>'required',
'role_id'=>'required'
]);
$user = User::findOrFail($id);
$user->name = $request->name;
$user->email=$request->email;
$user->country=implode(',',$request->country);
$user->state=implode(',',$request->state);
$user->city=implode(',',$request->city);
$user->role_id=$request->role_id;
$user->update();
return redirect()->route('viewuser');
}
Try in the form:
<form action="{{ route('updateuser',$data->id) }}" method="POST" enctype="multipart/form-data">
#method('PUT')
#csrf
ALL YOUR INPUTS HERE
<div class="card-footer">
<button type="submit" class="btn btn-primary mt-1 pr-4 pl-4">Update</button>
</div>
</form>
Route
Route::match(['put', 'patch'], 'updateuser/{id}', [UserController::class,'updateuser'])->name('updateuser');
I would like to make a form.
After submitting the data,Redirect to the same page and reflect a change.
I thought return redirect should be a good way.
but it seems need to fetching the DB.
because 'ErrorException
Trying to get property 'id' of non-object (View:'error happens.
writing $user = \DB::table('users')->where('id', $request->id)... twice
is redundancy and cheesy.
Is there any good way to implement this.
class CertainController extends Controller
{
public function index(Request $request)
{
$user = \DB::table('users')->where('id', $request->id)->first();
$data = ['user' => $user];
return view('user.detail',$data);
}
public function update(Request $request)
{
\DB::table('users')
->where('id', $request->id)
->update([
$request->name => $request->value
]);
return redirect(route('user.detail', [
'user_id' => $request->id,
]));
}
}
web.php
Route::get('/user_detail', 'CertainController#index')->name('user.detail');
Route::get('/user_detail/update', 'CertainController#update')->name('user.detail.update');
blade
<form method ="GET" action={{ route('user.detail.update')}}>
<div class="form-group row">
<label>name</label>
<div class="col-md-6">
<input type = "hidden" name ="id" value="{{ $user->id }}"/>
<input type = "hidden" name = "column" value="name">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ $user->name }}" required autocomplete="name">
#error('')
{{ $message }}
#enderror
<button type = "submit" class ="button">submit</button>
</div>
</div>
</form>
You are looking for id in the index method,
$request->id
^^
But you are sending user_id from update method.
'user_id' => $request->id,
^^^^^
In a simple way, you can just do
return back();
here, back() is a helper function, which redirect back to where it came from.
I'm new to laravel,i want to use the same form for add and edit.I created an form and form insertion is ok but i need to use the same form edit based on the id selected.When click the edit icon i want to direct the same page displaying the contents to edit.So give me idea for implementing this.
<form method="POST" action="/categoryinsert">
<input type = "hidden" name = "_token" value = "<?php echo csrf_token(); ?>">
<div class="card-body">
<div class="form-group">
<div class="col-md-4">
<label for="exampleInputEmail1">Category</label>
<input type="text" class="form-control" name="category" id="category" placeholder="Enter Category">
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
// To create a new user in controller
public function create()
{
// user/createOrUpdate.blade.php view
return View::make('user.createOrUpdate');
}
// To update an existing user
public function edit($id)
{
$user = User::find($id);
// user/createOrUpdate.blade.php view
return View::make('user.createOrUpdate')->with('user', $user);
}
Add/edit in view with the help of user model
#if(isset($user))
{{ Form::model($user, ['route' => ['updateroute', $user->id], 'method' => 'patch']) }}
#else
{{ Form::open(['route' => 'createroute']) }}
#endif
{{ Form::text('fieldname1', Input::old('fieldname1')) }}
{{ Form::text('fieldname2', Input::old('fieldname2')) }}
{{ Form::submit('Save', ['name' => 'submit']) }}
{{ Form::close() }}
// To create a new user in controller
public function create()
{
// user/createOrUpdate.blade.php view
return view('user.createOrUpdate')->with([
'view_type' => 'create',
]);
}
// To update an existing user
public function edit($id)
{
$user = User::find($id);
// user/createOrUpdate.blade.php view
return view('user.createOrUpdate')->with([
'view_type' => 'edit',
'user' => $user
]);
}
<form action="{{ ( $view_type == 'edit' ? route('example', $id) : route('control.mentors.store')) }}" role="form" method="post" name="frmDetail">
I'm trying to create a search function in Laravel and its returning me with "undefined variable: posts" when I do foreach on my view.
My code:
Post Model
class Post extends Model {
protected $fillable = [
'creator',
'post_url',
'books',
'likes',
'created_at'
];
public function user() { return $this->belongsTo(User::class); }
}
Homeview:
<form action="{{ url('/search') }}" method="get">
<input type="text" class="search-text form-control form-control-lg" name="q" placeholder="Search" required>
</form>
Controller:
public function search($keyword)
{
$result = Post::where('books', 'LIKE', "'%' . $keyword . '%'")->get();
return view('/search', ['posts' => $result]);
}
Route:
Route::get('/search/{keyword}', 'SearchController#search');
Searchview:
#foreach($posts as $post)
<div class="post">{{ $post->id }}</div>
#endforeach
What am I doing wrong here?
This might help you out.
Homeview.blade.php
<form action="/search" method="POST">
#csrf // include your csrf token
<input type="text" class="search-text form-control form-control-lg" id="q" name="q" placeholder="Search" required>
</form>
Searchview.blade.php
<!-- or did you return a collection? -->
#if( $posts->count() > 1 )
<!-- then loop through the posts -->
#foreach( $posts as $post )
<div class="post"> {{ $post->id }} </div>
#endforeach
#else
#if( !empty($posts) )
<div class="post"> {{ $post->id }} </div>
#endif
#endif
Routes/web.php
Route::post('/search', 'PostsController#show')->name('posts.show');
PostsController
use App\Post;
public function show( Request $request )
{
$result = Post::where("books", "LIKE", "%{$request->input('q')}%")->get();
// Uncomment the following line to see if you are returning any data
// dd($result);
// Did you return any results?
return view('searchview', ['posts' => $result]);
}
The reason it wasn't working,
Route::get('/search/{keyword}', 'SearchController#search');
In your route file you were looking for a {keyword} that was never passed by the form. Your form action is action="{{ url('/search') }}". A get variable will not be picked up by a route and if it was you called the input 'q' anyway.
So then in your controller you were looking for the keyword being passed that is never passed in.
public function search($keyword)
Instead the correct thing to do is pass in the Request object like so
public function search(Request $request)
Then use $request->input('q') to retrieve the passed value through your form.
In your example $keyword would always have been blank.
Corrected code
Homeview:
<form action="{{ url('/search') }}" method="get">
<input type="text" class="search-text form-control form-control-lg" name="q" placeholder="Search" required>
</form>
Controller:
public function search(Request $request)
{
$result = Post::where('books', 'LIKE', "%{$request->input('q')}%")->get();
return view('/search', ['posts' => $result]);
}
Route:
Route::get('/search', 'SearchController#search');
Searchview:
#foreach($posts as $post)
<div class="post">{{ $post->id }}</div>
#endforeach
try:
return view('/search')->with('posts', $result);
Or even better with dinamic vars.
return view('/search')->withPosts($result);
I'm receiving the error "MethodNotAllowedHttpException
No message" on submit of my user's form, which is meant to update the user's table. I have two post forms on the same page and two post routes, would that have something to do with it?
I will include all the routes and another form that might be conflicting with it.
web.php
Route::get('profile','userController#profile');
Route::post('profile', 'userController#update_avatar');
Route::post('profile-update', 'userController#update_account'); //this ones not working
userController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
use Image;
class UserController extends Controller
{
//
public function profile()
{
return view('profile', array('user' => Auth::user()) );
}
public function update_avatar(Request $request)
{
if($request->hasFile('avatar')){
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save( public_path('/uploads/avatars/' . $filename) );
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return view('profile', array('user' => Auth::user()) );
}
public function update_account(Request $request, $id) //the function with the error
{
User::update([
'id' => Auth::user()->id,
'name' => $request->name,
'email' => $request->email
]);
return redirect('/profile');
}
}
profile.blade.php
<img src="/uploads/avatars/{{ $user->avatar }}" style="width:150px;height:150px;float:left;border-radius:50%;margin-right:25px">
<h2>{{ $user->name }}'s Profile</h2>
<form enctype="multipart/form-data" action="/profile" method="post">
<label>Update Profile Image</label>
<input type="file" name="avatar">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class=" btn btn-sm btn-light" style="color:#2b2b2b;" value="Update Image">
</form>
<form method="post" action="/profile-update"> <!-- The form with the error -->
{{ method_field('put') }}
{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT" />
<label>Username</label>
<input type="text" name="name" class="form-control" value="{{ $user->name }}">
<label>Email</label>
<input type="email" name="email" class="form-control" value="{{ $user->email }}">
<input type="submit" id="update-account" class="btn btn-success" value="Update">
</form>
try this method:
public function update_account(Request $request, $id)
{
$user = User::find($id)
$user->name = $request->name;
$user->email = $request->email;
$user->update();
return redirect('/profile');
}
You don't have any route which can handle the PUT request for "profile-update". In your form you have defined the following function.
{{ method_field('put') }}
This helper function generate an hidden input field which will be used by Laravel to process the current request only as PUT.
To make this work, you either have to make your make your request POST by removing the above helper function or change your route method to PUT.
Route::put('profile-update', 'userController#update_account');
For those that might need the same answer, to fix this I had to play about with it for quite some time and used bits from the suggested answers to solve the issue completely.
I changed the route method to put in web.php.
Replaced my update_account function with #TonzFale answer but replaced $user = User::find($id)with $user = User::find(Auth::user()->id);.