This issue appears after I submit a form and run form validation, even though my form validation is functioning on another page in that file.
Method Illuminate\Validation\Validator::validateDocs does not exist.
public function store(Request $request) {
$request->validate([
'title' => 'required',
'category' => 'required',
'content' => 'required|min:50',
'file' => 'required|docs|mimes:txt,pdf,ppt|max:2048'
$docsName = time() . '.'. $request->file->extension();
// $request->docs->move(public_path('docs'), $docsName);
$request->file->storeAs('public/docs', $docsName);
$postData = ['title' => $request->title, 'category' => $request->category, 'content' => $request->content, 'docs' => $docsName];
Post::create($postData);
return redirect('/post')->with(['message' => 'Post added successfully!', 'status' => 'success']);
}
Related
controller validator in laravel :
$validationController = $this->validate(request(), [
'title' => 'min:100',
'text' => 'required',
'image' => 'required',
]);
and we can make validator with :
$validationNormaly = Validator::make(request(), [
'title' => 'min:100',
'text' => 'required',
'image' => 'required',
]);
and i can't use $validationController->fails(). how can i use it?
The first validation that you mentioned will fail automatically as designed by the laravel code base.
The Validator::make() i typically use when doing ajax requests and such to the controller method. You will need to run the validation fails method to invoke the failed response like so:
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
if ($validator->fails()) {
return redirect('post/create')
->withErrors($validator)
->withInput();
}
You Can Use :
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
])->validate();
take advantage of the automatic redirection with error messages
I defined my route but it is not showing that Route [dealer] not defined.
Route::resource('/dealer', DealerController::class);
This is my controller where there is index, create and store method is in same page.
public function index()
{
$users = User::all();
return view('dealer', compact('users'));
}
public function create()
{
$dealers = Dealer::all();
return view('dealer', compact('dealers'));
}
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
'name_of_firm' => 'required',
'address' => 'required',
'number' => 'required',
]);
$user = User::create([
'name' => $request->input('name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
'name_of_firm' => $request->input('name_of_firm'),
'address' => $request->input('address'),
'number' => $request->input('number'),
]);
return redirect()->route('dealer')->withSuccess('done');
}
https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller
Look at the example from the documentation. There exists no such route in your ressource controller.
Depending on what you want you either have to use dealer.index, dealer.show or dealer.edit
Normally you would also use the plural form and not the singular form of the word.
i have a problem this controller is not working how can i do? should send mutiple emails how do i solve?
I don't know how to handle it
function submit(Request $request) {
$this->validate($request, [
'email' => 'required|email',
'file' => 'mimes:pdf,doc,docx'
]);
$data = array(
'name' => $request->name,
'cognome' => $request->cognome,
'luogo' => $request->luogo,
'date' => $request->date,
'telefono' => $request->telefono,
'email' => $request->email,
'citta' => $request->citta,
'provincia' => $request->provincia,
'studio' => $request->studio,
'lingua' => $request->lingua,
'livello' => $request->livello,
'lingua2' => $request->lingua2,
'livello2' => $request->livello2,
'file' => $request->file,
'agree' => $request->agree
);
Mail::send('mail', $data, function($message) use ($request,$data){
$message->to('luis#gmail.com', 'luis')->subject('Send mail ' . $request->name);
$message->from($request->email, $request->name);
if($request->hasFile('file')){
$message->attach($request->file('file')->getRealPath(), array(
'as' => $request->file('file')->getClientOriginalName(),
'mime' => $request->file('file')->getMimeType())
);
}
});
Session::flash('success', 'Mail spedita con sucesso');
}
I wish I could solve the problem
any advice? on how to do it?
I'm using the Spatie MediaLibrary library in a Laravel application. I want to upload 0 or more photos to my app via a REST API.
I can get it to work when the photo attribute contains 1 file
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required',
'description' => 'required',
'price' => 'required|integer',
'photo' => 'nullable|file'
]);
$listing = Listing::Create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
'price' => $request->price,
]);
// stores the photo
if ($request->hasFile('photo')) {
$listing->addMediaFromRequest('photo')->toMediaCollection('photos');
}
return new ListingResource($listing);
}
The postman request looks as follows:
I know want to change the code so it can handle multiple photos in the request. I'm using the following code in the controller above to do so:
if ($request->hasFile('photo')) {
foreach ($request->input('photo', []) as $photo) {
$listing->addMediaFromRequest('photo')->toMediaCollection('photos');
}
}
and I have changed the attribute to photos[] instead of photo.
The code never goes into the foreach loop even.
Anyone has a hint on how to solve this?
Apparently the Spatie Medialibrary has a function called addMultipleMediaFromRequest. The full code is now
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'slug' => 'required',
'description' => 'required',
'price' => 'required|integer',
'photo' => 'nullable'
]);
$listing = Listing::Create([
'user_id' => auth('api')->user()->id,
'name' => $request->name,
'slug' => $request->slug,
'description' => $request->description,
'price' => $request->price,
]);
if ($request->hasFile('photo')) {
$fileAdders = $listing->addMultipleMediaFromRequest(['photo'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('photos');
});
}
return new ListingResource($listing);
}
In Postman, I'm calling it as follows:
documentation reference
I managed to upload multiple files like this:
if($request->file('photos')) {
foreach ($request->file('photos') as $photo) {
$post->addMedia($photo)->toMediaCollection('post');
}
}
Check this out:
https://github.com/spatie/laravel-medialibrary/issues/227#issuecomment-220794240
This code is working for me.
View
<input type="file" name="photo[]" multiple />
ListingController
public function store(Request $request)
{
if ($request->hasFile('photo')) {
$fileAdders = $listing->addMultipleMediaFromRequest(['photo'])
->each(function ($fileAdder) {
$fileAdder->toMediaCollection('photos');
});
}
}
I'm trying to submit data from one form into two different tables, but it is not submitting, my form contains on two parts one is model, i.e., when modal form submit then its data saves into one table and rest of other data into another table.
Controller
public function store(Request $request)
{
$request->validate([
'patient_fname' => 'required',
'patient_lname' => 'required',
'patient_email' => 'required',
'patient_dob' => 'required',
'patient_guardian' => 'required',
'patient_gender' => 'required',
'user_id' => 'required',
'patient_name' => 'required',
'patient_insurance' => 'required',
'patient_insurance_id' => 'required',
'patient_reason' => 'required',
'patient_new' => 'required',
'patient_contact_no' => 'required',
'patient_message' => 'required',
]);
$userdata = Userdata::create([
Input::get('patient_fname'),
Input::get('patient_lname'),
Input::get('patient_email'),
Input::get('patient_dob'),
Input::get('patient_guardian'),
Input::get('patient_gender')
]);
$form = Form::create([
Input::get('patient_name'),
Input::get('patient_insurance'),
Input::get('patient_insurance_id'),
Input::get('patient_reason'),
Input::get('patient_new'),
Input::get('patient_contact_no'),
Input::get('patient_message')
]);
return back();
}
form.php
protected $guarded = [];
protected $table = "forms";
protected $fillable=['patient_name',
'patient_insurance','patient_insurance_id', 'patient_reason',
'patient_new', 'patient_message'];
public function userdata()
{
return $this->belongsTo('App\Userdata');
}
userdata.php
protected $fillable = ['patient_fname', 'patient_lname',
'patient_email', 'patient_dob', 'patient_guardian',
'patient_gender','user_id'];
public function form()
{
return $this->hasMany('App\Form');
}
I would imagine this is because you're not supplying a key for the values in your create method.
Also, validate will return an array of the validated properties so you can just use that instead of using Input::get():
public function store(Request $request)
{
$data = $request->validate([
'patient_fname' => 'required',
'patient_lname' => 'required',
'patient_email' => 'required',
'patient_dob' => 'required',
'patient_guardian' => 'required',
'patient_gender' => 'required',
'user_id' => 'required',
'patient_name' => 'required',
'patient_insurance' => 'required',
'patient_insurance_id' => 'required',
'patient_reason' => 'required',
'patient_new' => 'required',
'patient_contact_no' => 'required',
'patient_message' => 'required',
]);
$userdata = Userdata::create([
'patient_fname' => $data['patient_fname'],
'patient_lname' => $data['patient_lname'],
'patient_email' => $data['patient_email'],
'patient_dob' => $data['patient_dob'],
'patient_guardian' => $data['patient_guardian'],
'patient_gender' => $data['patient_gender'],
]);
$form = Form::create([
'patient_name' => $data['patient_name'],
'patient_insurance' => $data['patient_insurance'],
'patient_insurance_id' => $data['patient_insurance_id'],
'patient_reason' => $data['patient_reason'],
'patient_new' => $data['patient_new'],
'patient_contact_no' => $data['patient_contact_no'],
'patient_message' => $data['patient_message'],
]);
return back();
}