Hey I am trying to update user profile by sending an id to controller. Every time I submit my form it gives me error missing argument. I have defined route and included id parameter in my controller function but then tooo I get the error.
Code for my controller is
public function update_avatar(Request $request,$id)
{
$this->validateInput($request);
$u = User::findOrFail($id);
dd($u);
//Handle the user upload of avatar
if($request->hasFile('avatar'))
{
$avatar = $request->file('avatar');
$filename = time() . '.' . $avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300,300)->save(public_path('/uploads/'.$filename));
}
$user = Auth::user();
$user->name = $request->name;
dd($user->name);
$user->email = $request->email;
if($request->password == null)
{
$user->password = $u->password;
}
$user->password = $request->password;
$user->avatar = $filename;
$user->role = 'admin';
$user->save();
return view('/adminPanel/adminProfile',array('user' => Auth::user()));
}
code of my view
<form class="form-horizontal" role="form" enctype="multipart/form-data" action="{{ route('profile', ['id' => $user->id]) }}" method="post">
And my route is
Route::post('/adminPanel/id/adminProfile',
'AdminProfileController#update_avatar')
->name('profile')->middleware('admin');;
In your route use. You missing parenthesis. When you pass id or any value using route, you have must use parenthesis for this.
Route::post('/adminPanel/{id}/adminProfile','AdminProfileController#update_avatar')->name('profile')->middleware('admin');
Related
Missing required parameter for [Route: blog.update] [URI: blog/{post}/update] [Missing parameter: post].
in routes :
Route::put('/blog/{post}/update', [BlogController::class, 'update'])->name('blog.update');
in BlogController :
`
public function update(Request $request,Post $post){
$request->validate([
'title' => 'required',
'image' => 'required | image',
'body' => 'required'
]);
$postId = $post->id;
$title = $request->input('title');
$slug = Str::slug($title,'-').'-'.$postId;
// $user_id = Auth::user()->id;
$body = $request->input('body');
//File upload
$imagePath = 'storage/'. $request->file('image')->store('postImages','public');
// $post = new Post();
$post->title = $title;
$post->slug = $slug;
// $post->user_id = $user_id;
$post->body = $body;
$post->imagePath = $imagePath;
$post->save();
return redirect()->back()->with('status', 'Post edited successfully');
dd('validation passed . You can request the input');
}
`
Please solve this issue
I want to update the post
If you are using the route() helper in your form. You can pass the parameter using it :
<form action="{{ route('blog.update', ['post' => $post_id]) }}">...</form>
which is the post is the parameter you name in the route.
Since you haven't posted your blade file so here's the full method:
in your route:
Route::put('/blog/{post}/update' , [BlogController::class, 'update']);
in your blade form make tag like this:
<form method="post" action="/blog/{{$post->id}}/update">
//This will show url like /blog/1/update
//For using PUT method add this:
{{ method_field('PUT') }}
// Do not forget to use #csrf
Then in your controller update function handle the request and $id like this:
public function update(Request $request, $id){
//Find post in your data with help of model
$post = \App\Models\Post::findOrFail($id);
//validate and update with the post instance of Post class
//Feel free to add many functions same as of your controller before i'm leaving empty
$post->update([
//Add fields for update
]);
}
Hope you all have a good day..
At first i have a form that user should fill before redirect to PayTabs page for payment
But i don't need the form to be inserted in database before success payment
public function index(Request $request): RedirectResponse
{
$response = $this->request(
url: 'https://secure-global.paytabs.com/payment/request',
payload: $this->transactionPayload(
amount: 0
)
);
Transaction::create([
'paytabs_transaction_reference' => $response->json()['tran_ref'] ?? null
]);
Cart::where('opened', '=', 0)->where('user_id', Auth::id())->update(['paytabs_transaction_reference' => $response->json()['tran_ref'] ?? null]);
$cart_id = Cart::latest()->first()->id;
$payment = new Payment;
$payment->cart_id = $cart_id;
$payment->country_id = $request->input('country_id');
$payment->delivery_id = $request->input('delivery_id');
$payment->address = $request->input('address');
$payment->street = $request->input('street');
$payment->home = $request->input('home');
$payment->email = $request->input('email');
$payment->save();
return redirect()->away($response['redirect_url']);
}
public function return(Request $request): string
{
$category = Category::all();
$products = Product::all();
$validSignature = $this->validateSignature($request->all());
if ($validSignature) {
if ($request->respStatus == 'A') {
$transaction = Transaction::where('paytabs_transaction_reference', $request->tranRef)->first();
$transaction->paid = true;
$transaction->save();
DB::table('carts')->where('user_id', Auth::id())->where('opened', 0 )->update(['opened' => 1]);
return view('index', compact('category', 'products'))->with('success', 'Payment has been done successfully. Thank you!') . $request->respMessage;
}
return view('index', compact('category', 'products')) . $request->respMessage;
} else {
return 'Invalid Transaction Signature';
}
}
Here the form will be inserted even if he back from the next page
<form role="form" action="{{ route('index') }}" method="post">
#csrf
.......
</form>
That's my form should be to INDEX function to redirect to payment page
You Need To Make A Validation To Your Form
I have a product that I want to edit. Please see the following code.
public function update(ProductRequest $request, Product $product)
{
$product->user_id = auth()->user()->id;
$product->title = $request->title;
$product->body = $request->body;
$product->price = $request->price;
if ($request->has('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/products'), $filename);
$product->image = $request->file('image')->getClientOriginalName();
}
$product->update();
$product->categories()->sync($request->category);
return redirect()->route('products.index');
}
product.blade.php
<form class="form-horizontal" action="{{ route('products.update', $product->id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('PATCH') }}
.
.
.
Everything changes except the image. I tried it, but, I did not succeed. What's the solution? I want to update the image in the database.
OK, I go to database now, I changed in image to picture, then I go my project, and I tested this again.
But it did not event for me.
I changed
public function update(ProductRequest $request, Product $product)
To
public function update(Request $request, Product $product)
To update a model, you should retrieve it, set any attributes you wish to update, and then call the save method. See the updates section in the documentation.
public function update(ProductRequest $request, Product $product)
{
$product->user_id = auth()->user()->id;
$product->title = $request->title;
$product->body = $request->body;
$product->price = $request->price;
if ($request->has('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/products'), $filename);
$product->image = $request->file('image')->getClientOriginalName();
}
$product->save();
$product->categories()->sync($request->category);
return redirect()->route('products.index');
}
Update 1
Assuming you have debugged the above and $request->file('image')->getClientOriginalName() is returning the expected value, it's possible that you are using field whitelisting on your model and haven't added the image field to the whitelist. Make sure that image is in the $fillable array on your model.
class Product extends Model
{
protected $fillable = [
'user_id',
'title',
'body',
'price',
...
'image'
];
Update 2
If $request->file('image') is returning null then that would suggest your form is not submitting the files. Ensure your <form> element has the enctype="multipart/form-data" attribute included in the tag.
Update 3
If your already including the required enctype="multipart/form-data" tags in the <form> element then I would suggest doing the following.
Place dd($request->all()); at the top of your update(ProductRequest $request, Product $product) method. Post the form and check the output.
If the file is not included in the output, change from using ProductRequest $request to the default Request $request (Illuminate\Http\Request) and try it again. There could be something in your ProductRequest class that is causing a problem.
As a small critique, you could improve your code in the following ways.
・Use hasFile() instead of has().
・Use $product-image = $filename instead of $product->image = $request->file('image')->getClientOriginalName();.
public function update(ProductRequest $request, Product $product)
{
$product->user_id = auth()->user()->id;
$product->title = $request->title;
$product->body = $request->body;
$product->price = $request->price;
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = $image->getClientOriginalName();
$image->move(public_path('images/products'), $filename);
$product->image = $filename;
}
$product->save();
$product->categories()->sync($request->category);
return redirect()->route('products.index');
}
make sure your has
enctype="multipart/form-data"
property
I'm building a gallery with multiple files functionality.
So far I'm having two issues, but let's paste my code first.
Controller:
public function store(Request $request)
{
$gallery = new GalleryImage();
if($request->hasFile('image')) {
$images = $request->file('image');
foreach($images as $image) {
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery->image = $image->move(public_path().'/uploads/temp/', $name);
//$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
}
}
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
return back()->with('success_message','Images has been uploaded!');
}
View blade:
<form action="{{ route('gallery-images.store') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<input type="hidden" name="gallery_id" value="{{ $gallery->id }}">
<input type="file" name="image[]" id="id_imageGallery" class="form-control" multiple="multiple" required>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Upload</button>
</div>
</form>
My first issue when I have my code like this and upload three files, it's successfully storing the files into /uploads/temp directory, but in my database I can see there's only one image uploaded, instead of three.
My second issue is when I change my code and use the commented part (because I want to store those three images into Storage):
$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
I'm getting this error:
Call to a member function storeAs() on array
How can I upload those multiple images into the Storage folder and record them all into the database?
-- EDIT --
I've solved my first issue!
I've simply put the save method and other details inside the loop. I've should have thought about this before :)
Here's my updated code:
public function store(Request $request)
{
if($request->hasFile('image')) {
$images = $request->file('image');
foreach($images as $image) {
$gallery = new GalleryImage();
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery->image = $image->move(public_path().'/uploads/temp/', $name);
//$gallery->image = $request->file('image')->storeAs('public/gallery-images', $name);
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
}
}
return back()->with('success_message','Images has been uploaded!');
}
Now only thing to do is how to store them into File Storage, instead of the public_path and avoid that error:
Call to a member function storeAs() on array
I've finally solved the issues and I'm posting it, so it may be useful for some.
Here's my code:
public function store(Request $request)
{
if($request->hasFile('image')) {
foreach($request->image as $image) {
$path = $image->getClientOriginalName();
$name = time() . '-' . $path;
$gallery = new GalleryImage();
$gallery->image = $image->storeAs('public/gallery-images', $name);
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
}
}
return back()->with('success_message','The images have been uploaded!');
}
Try to implement using the following code, it will give uploaded files url in serialized format
public static function upload_file($file){
$file_name = time();
$file_name .= rand();
$file_name = sha1($file_name);
if ($file) {
$ext = $file->getClientOriginalExtension();
$file->move(public_path() . "/uploads", $file_name . "." . $ext);
$local_url = $file_name . "." . $ext;
$s3_url = url('/').'/uploads/'.$local_url;
return $s3_url;
}
return "";
}
public static function upload_multiple_files($files){
if(is_array($files)){
$return_array = array();
foreach ($files as $file){
if(!empty($file)){
$return_array[] = self::upload_file($file);
}else{
$return_array[] = '';
}
}
return serialize($return_array);
}else{
return NULL;
}
}
//I have updated and commented your code. Try it, It should work.
public function store(Request $request)
{
if($request->hasFile('image')) {
$images = $request->file('image');
foreach($images as $image) {
$gallery = new GalleryImage();
// laravel auto manages unique name and extension of file.
$gallery->image = $image->store('gallery-images', 'public');
$gallery->gallery_id = $request->gallery_id;
$gallery->save();
}
}
return back()->with('success_message','Images has been uploaded!');
}
I have created a from with a user registration. When processing the form within the controller, I want the user to be logged in afterwards. Account creation works fine, but logging right afterwards fails:
function processForm($username,$first_name,$last_name,$email,$password,$groups) {
jimport('joomla.user.helper');
$app = JFactory::getApplication('site');
$user = new JUser();
$b= array();
$user->bind($b);
$user->username = $username;
$user->name = $first_name." ".$last_name;
$user->set('id',0);
$date =date("Y-m-d H:i:s");
$user->set('registerDate', $date);
$user->email = $email;
$user->set('password',JUserHelper::getCryptedPassword($password,JUserHelper::getSalt()));
$user->set('block', '0');
$user->save();
foreach ($groups as $group) {
JUserHelper::addUserToGroup($user->id, $group);
}
$app->login(array("username"=>$username,"password"=>$password));
}
Any idea?