There is nothing changed when I'm updating my record. Before this I don't put $id in the function but after make some research, I need to put it and also put in the route. Still, I do not have any idea know where is my mistakes.
web.php
Route::get('/administrator','AdminController#index');
Route::post('/admin/create','AdminController#create');
Route::get('/admin/{id}/edit', 'AdminController#edit');
Route::post('/admin/{id}/update', 'AdminController#update');
Route::post('/admin/delete', 'AdminController#destroy');
AdminController
public function edit($id) {
$admin = Admin::find($id);
return view('admins.edit',['admins'=>$admin]);
}
public function update(Request $request, $id) {
$admins = Admin::find($id);
$admins->name = $request->name;
$admins->branch = $request->branch;
$admins->email = $request->email;
if ($request->hasFile('avatar')){
$filename = $request->avatar->getClientOriginalName();
$request->avatar->storeAs('images/'.$request->email,$filename,'public');
$admins->avatar = $request->email.'/'.$filename;
}
else
{
$admins->avatar='default.png';
}
$admins->save();
return redirect('/administrator')->with('success','Your details are updated!');
profile.blade.php
<h4 class="heading"><b><center>Admin's Details</center></b></h4>
<ul class="list-unstyled list-justify"><center>
<li><b>ID: </b>{{$admins->id}}</li>
<li><b>Name: </b>{{$admins->name}}</li>
<li><b>Branch: </b>{{$admins->branch}}</li>
<li><b>Email: </b>{{$admins->email}}</li></center>
</ul>
<div class="text-center">Edit Profile</div>
edit.blade.php
<div class="panel-body">
<form action="/admin/{id}/update" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div>
<div class="form-group hidden">
<label for="id">id</label>
<input type="text" class="form-control" name="id" value="{{$admins->id}}"/>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{$admins->name}}"/>
</div>
<div class="form-group">
<label for="address">Branch</label>
<input type="text" class="form-control" name="branch" value="{{$admins->branch}}" />
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" name="email" value="{{$admins->email}}"/>
</div>
<div class="form-group">
<label for="avatar">Avatar</label>
<input type="file" name="avatar" class="form-control-file" value="{{$admins->avatar}}">
</div>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
I hope someone can help me solve this problem.
add name() to route then call in view
Route::post('/admin/{id}/update', 'AdminController#update')->name('admin.update');
in blade
<form action="{{route('admin.update',$admins->id)}}" method="post" enctype="multipart/form-data">
web.php
Route:: resources ('admin', 'AdminController');
edit.blade.php
<div class="panel-body">
<form action="{{route('admin.update',$id)}}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div>
<div class="form-group hidden">
<label for="id">id</label>
<input type="text" class="form-control" name="id" value="{{$admins->id}}"/>
</div>
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" value="{{$admins->name}}"/>
</div>
<div class="form-group">
<label for="address">Branch</label>
<input type="text" class="form-control" name="branch" value="{{$admins->branch}}" />
</div>
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" name="email" value="{{$admins->email}}"/>
</div>
<div class="form-group">
<label for="avatar">Avatar</label>
<input type="file" name="avatar" class="form-control-file" value="{{$admins->avatar}}">
</div>
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
AdminController
public function update(Admin $admin, Request $request)
{
$admin->update([$this->requestValidate($request)]);
$this->storeImage($admin);
return redirect(route('admin.index'));
}
public function requestValidate($request)
{
return $request->validator([
'name' => 'string | required',
'branch' => 'string |required',
'email' => 'string | required | email'
]);
}
public function storeImage($request)
{
if ($request->hasFile('avatar')) {
$filename = $request->avatar-
>getClientOriginalName();
$request->avatar->storeAs('images/' . $request-
>email, $filename, 'public');
$request->avatar = $request->email . '/' . $filename;
} else {
$request->avatar = 'default.png';
}
}
Use this stardant with validate maybes it helps you...
Related
I tried to edit the products in my store. On request, missing $request->file('image');
I'm attaching the source code below, I really don't know why I don't receive the image in the request, because I think it's correct what I did
My form :
<form method="POST" action="{{ route('products.update', $product->id)}}" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput1">Product slug</label>
<input type="text" name="product_slug" value="{{$product->product_slug}}" class="form-control" id="exampleFormControlInput1" placeholder="Enter slug">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput111">Product title</label>
<input type="text" name="product_title" value="{{$product->product_title}}" class="form-control" id="exampleFormControlInput111" placeholder="Enter slug">
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput2">Product category</label>
<input type="text" name="product_category" value="{{$product->product_category}}" class="form-control" id="exampleFormControlInput2" placeholder="name#example.com">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput3">Product brand</label>
<input type="text" name="product_brand" value="{{$product->product_brand}}" class="form-control" id="exampleFormControlInput3" placeholder="name#example.com">
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput22">Product display</label>
<input type="text" name="product_display" value="{{$product->product_display}}" class="form-control" id="exampleFormControlInput22" placeholder="name#example.com">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput34">Product ram</label>
<input type="text" name="product_ram" value="{{$product->product_ram}}" class="form-control" id="exampleFormControlInput34" placeholder="name#example.com">
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput33">Product os</label>
<input type="text" name="product_os" value="{{$product->product_os}}" class="form-control" id="exampleFormControlInput33" placeholder="name#example.com">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput333">Product camera</label>
<input type="text" name="product_camera" value="{{$product->product_camera}}" class="form-control" id="exampleFormControlInput333" placeholder="name#example.com">
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group">
<label for="exampleFormControlInput8">Product price</label>
<input type="text" name="product_price" value="{{$product->product_price}}" class="form-control" id="exampleFormControlInput8" placeholder="name#example.com">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="exampleFormControlFile1">Change product photo</label>
<input type="file" name="image" value="{{$product->product_image}}" class="form-control-file" id="exampleFormControlFile1">
<img src="/storage/img/tech/{{$product->product_image}}" style="width:300px" alt="product_image">
</div>
</div>
</div>
<div class="form-group">
<label for="short_description">Short description</label>
<textarea class="form-control" name="about_product" id="short_description" rows="10">{{$product->about_product}}</textarea>
</div>
<div class="form-group">
<label for="long_description">Long description</label>
<textarea class="form-control" name="product_description" id="long_description" rows="10">{{$product->product_description}}</textarea>
</div>
<input type="submit" value="Edit" class="btn btn-success" name="submit">
Go back
</form>
If I remove the part with the image, the code is perfectly functional
My function in controller(type resource)
public function update(Request $request, $id)
{
dd($request->all());
$request->validate([
'product_slug' => 'required|max:100',
'product_title' => 'required|max:100',
'product_category' => 'required|max:100',
'product_brand' => 'required|max:100',
'product_image' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
'product_display' => 'required',
'product_camera' => 'required',
'product_ram' => 'required',
'product_os' => 'required',
'product_price' => 'required|max:100',
'about_product' => 'required',
'product_description' => 'required'
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'storage/img/tech/';
$profileImage = $image->getClientOriginalName();
$image->move($destinationPath, $profileImage);
$input['image'] = $profileImage;
} else {
unset($input['image']);
}
$product = Product::find($id);
$product->product_slug = $request->get('product_slug');
$product->product_title = $request->get('product_title');
$product->product_category = $request->get('product_category');
$product->product_brand = $request->get('product_brand');
$product->product_display = $request->get('product_display');
$product->product_ram = $request->get('product_ram');
$product->product_camera = $request->get('product_camera');
$product->product_os = $request->get('product_os');
$product->product_price = $request->get('product_price');
$product->product_image = $profileImage;
$product->about_product = $request->get('about_product');
$product->product_description = $request->get('product_description');
$product->update();
return redirect('/admin/products')->with('success', "product updated!");
}
The core issue here is that value="{{ $product-> product_image }}" is not valid. <input type="file"> doesn't support that, as the image needs to be directly uploaded from the User's machine, and unless a file is selected and uploaded, $request->file('image') will be null.
To handle this cleaner, use some conditional logic in the Controller:
First, upload the image and set a reference to the file:
$profileImage = null;
if ($image = $request->file('image')) {
$destinationPath = 'storage/img/tech/';
$profileImage = $image->getClientOriginalName();
$image->move($destinationPath, $profileImage);
}
Next, set the $product->product_image based on the value of $profileImage:
$product = Product::find($id);
...
if ($profileImage)
$product->product_image = $profileImage;
}
A ternary or null-coalesce statement can be used, but it'll look a bit weird:
$product->product_image = $profileImage ? $profileImage : $product->product_image;
// OR
$product->product_image = $profileImage ?? $product->product_image;
Both of these cases will set $product->product_image to $profileImage, or the existing value of $product->profile_image if nothing is provided (that will be an existing image or null)
I'm new with laravel and now i making some small project. I have a form, after the submit button pressed i got this error message "Sorry, the page you are looking for could not be found."
Is there anything wrong about my code?
Please help me to fix this issue, so i can continuing the project.
Thanks in advice
view blade, i named it index.blade.php
<div class="col m7 s12">
<form method="submit" action="post">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
controller, i named it LayoutController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
class LayoutController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
return view('layouts/index');
}
public function submit(Request $request)
{
$name = $req->input('name');
$email = $req->input('email');
$phone = $req->input('phone');
$message = $req->input('message');
$data = array('name'=>$name,"email"=>$email,"phone"=>$phone,"message"=>$message);
$data->save();
return Redirect::to('/layouts/index');
}
routes web.php
Route::get('/', 'LayoutController#index');
Route::post('/submit', 'LayoutController#submit');
Your form method should be POST and action should be /submit
<form method="POST" action="/submit">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
Try this :
<form method="POST" action="{{ route('submit') }}">
The Error you're getting is because the wrong <form> tag attributes
action => 'The route or page or class method that'll process the form
information'
method => 'This the URI HTTP verb used to transport information, you
can either use POST(sending data as http payload) or GET(sending data
as query string)
changing the <form> tag like this will solve your issue
<form method="POST" action="{{ url('/submit') }}">
The form method should be POST and the action will be your route:
<form method="POST" action="{{ url('/submit') }}">
<div class="col m7 s12">
<form method="POST" action="{{url('/submit')}}">
{{ csrf_field() }}
<div class="card-panel">
<h5>Please Fill Out This Form</h5>
<div class="input-field">
<input type="text" name="name" id="name" required class="validate">
<label for="name">Name</label>
</div>
<div class="input-field">
<input type="email" name="email" id="email" class="validate">
<label for="email">Email</label>
</div>
<div class="input-field">
<input type="text" name="phone" id="phone">
<label for="phone">Phone</label>
</div>
<div class="input-field">
<textarea name="message" id="message" class="materialize-textarea"></textarea>
<label for="message">Message</label>
</div>
<button type="submit" class="btn" blue darken-1>Send</button>
</div>
</form>
I get an exception when returning $request from my controller.
The form in creat.blade.php:
<form action="{{ route('admin.category.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group form-float">
<div class="form-line">
<input type="text" id="name" class="form-control" name="name">
<label class="form-label">Category Name</label>
</div>
</div>
<div class="form-group">
<input type="file" name="image">
</div>
<button type="submit" class="btn btn-primary m-t-15 waves-effect">SUBMIT</button>
</form>
CategoryController
public function store(Request $request)
{
return $request;
}
how to display duplicate entry warning error to my view in laravel blade. so when they key in same name the warning will appear when they saved it.
Note: i have already make my Schema $table->string('studentname')->unique();;
Controller
public function store(Request $request)
{
$this->validate($request, [
'studentname'=>'required|max:50',
]);
$students = new Student();
$students->studentname = $request->studentname;
$students->address = $request->address;
$students->religion = $request->religion;
$students->save();
return redirect()->route('students.index')
->with('flash_message', 'Success.');
}
View-Blade
<div class="container">
<h1 class="well">Registration Form</h1>
<div class="col-lg-12 well">
<div class="row">
<form action="{{route('students.store')}}" method="POST">
{{csrf_field()}}
<div class="col-sm-12">
<h3>CHILD'S INFORMATION</h3>
<hr>
<div class="row">
<div class="col-sm-4 form-group">
<label>FULLNAME</label>
<input type="text" name="studentname" value="" placeholder="Enter FULLNAME.." class="form-control" required>
</div>
<div class="col-sm-4 form-group">
<label>RELIGION</label>
<input type="text" name="religion" value="" placeholder="Enter RELIGION.." class="form-control">
</div>
<div class="col-sm-4 form-group">
<label>ADDRESS</label>
<input type="text" name="address" value="" placeholder="Enter ADDRESS.." class="form-control">
</div>
<div>
<button type="submit" class="btn btn-default">SUBMIT</button>
</div>
</div>
</div>
</div>
Add the unique validation which returns a message if it fails.
$this->validate($request, [
'studentname'=>'required|max:50|unique:table_name,studentname',
]);
And then, in your blade template, do this.
<div class="col-sm-4 form-group {{ $errors->get('studentname') ? 'has-error' : '' }}">
<label>FULLNAME</label>
<input type="text" name="studentname" value="" placeholder="Enter FULLNAME.." class="form-control" required>
#foreach($errors->get('studentname') as $error)
<span class="help-block">{{ $error }}</span>
#endforeach
</div>
Hi i try to save fields in db ot 2 steps but i have MethodNotAllowedHttpException
on step 1 in my controller i save the 2 fields in db
public function storeNumber(Request $request){
$number = new Number;
$number->user_id = $user = \Auth::user()->id;
$number->number = $this->getGeneratedNumber();
$number = new Number($request->all());
$number->save();
return redirect('numbers/{id}/details');
}
view
<form class="form-horizontal" method="POST" action="{{action('NumberController#storeNumber')}}">
{{ csrf_field() }}
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-primary btn-block">
Generate Numbers
</button>
</div>
</div>
model
class Number extends Model
{
/**
* #var array
*
*/
protected $fillable = [
'number'
];
}
on step 2 i want to save another field in same db with same controller this is my another store function for store another fields in same db . but when i try to save laravel say MethodNotAllowedHttpException.
public function store(Request $request, $id){
$number = Number::find($id);
$number = new Number($request->all());
$number->save();
return redirect('numbers');
}
this is my view
<form method="post" action="{{action('NumberController#store', $id)}}">
{{csrf_field()}}
<input name="_method" type="hidden" value="PATCH">
<div class="form-group">
<label for="number" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Number" name="number" value="{{$number->number}}">
</div>
<div class="form-group">
<label for="comment" class="control-label">Comment</label>
<textarea name="comment" class="form-control form-control-lg" cols="30" rows="10">{{$number->comment}}</textarea>
</div>
<div class="form-group">
<label for="accept" class="control-label">Accept</label>
<input type="radio" name="accept" value="1">Yes<br>
<input type="radio" name="accept" value="0">No<br>
</div>
#if($number->accept == 1)
<div class="form-group">
<label for="name" class="control-label">Number</label>
<input type="text" class="form-control form-control-lg disabled" placeholder="Name" name="name" value="{{$number->name}}">
</div>
<div class="form-group">
<label for="city" class="control-label">City</label>
<input type="text" class="form-control form-control-lg" placeholder="City" name="city" value="{{$number->city}}">
</div>
<div class="form-group">
<label for="postcode" class="control-label">Postcode</label>
<input type="number" class="form-control form-control-lg" placeholder="Postcode" name="postcode" value="{{$number->postcode}}">
</div>
<div class="form-group">
<label for="address">Address</label>
<textarea name="address" class="form-control form-control-lg" cols="30" rows="10">{{$number->address}}</textarea>
</div>
#else
<p>TODO status for NO</p>
#endif
<div class="form-group">
<div class="col-md-12">
<button type="submit" class="btn btn-default">Finish</button>
</div>
</div>
</form>
You have a "_method" filed with "PATCH" value, so you have to change the route to "patch" instead of "post".
Please change blade content of update as follow
<form method="post" action="{{action('NumberController#store', $id)}}">
to
<form method="post" action="{{action('NumberController#update', $id)}}">
return redirect('numbers/{id}/details');
Here is incorrect, what is id?