Laravel - Upload mp3 file failed - php

I met a problem with upload of an mp3 file
Everytime I send the form I got a "File "" not found.
and that's what i got from my POST DATA :
IMAGE OF POST DATA
Here it's the controller :
public function Display()
{
return view('pages.new');
}
public function Post(Request $request)
{
$rules = [
'name' => ['required'],
'sources' => ['required'],
'cover' => ['required'],
'resume-podcast' => ['required'],
];
$validator = Validator::make($request->all(), $rules);
$pathimg = $request->file('cover')->store('/audio/cover');
$pathsources = $request->file('sources')->store('/audio/sources');
$podcasts = Audio::create(
[
'name' => request('name'),
'user_id' => auth()->id(),
'sources' => $pathsources,
'cover' => $pathimg,
'description' => request('resume'),
]);
return($pathsources);
flash("Yes !")->success();
}
Here it's the view :
<form action="/new" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group" >
<label for="exampleInputPassword1">Nom podcast</label>
#if($errors->has('name'))
<p class="bg-warning"> {{ $errors->first('name') }}</p>
#endif
<input class="form-control" name="name" id="name" type="text" aria-describedby="emailHelp" placeholder="Nom du podcast">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Description du podcast</label>
#if($errors->has('resume-podcast'))
<p class="bg-warning"> {{ $errors->first('resume-podcast') }}</p>
#endif
<input class="form-control" name="resume-podcast" id="resume-podcast" type="text" aria-describedby="emailHelp" placeholder="Description rapide">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Source (url)</label>
#if($errors->has('sources'))
<p class="bg-warning"> {{ $errors->first('sources') }}</p>
#endif
<input class="form-control" id="sources" name="sources" type="file">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Cover (url)</label>
#if($errors->has('cover'))
<p class="bg-warning"> {{ $errors->first('cover') }}</p>
#endif
<input class="form-control" id="cover" name="cover" type="file>
</div>
<input type="submit" class="btn btn-primary btn-block" value="Ajouter podcast">
</form>
</div>
</div>
</div>
I saw it was a recurrent problem but didn't find any solution :(
I already try with mimes:audio/mpeg but nothing...

There are 4/5 reasons why you faced that issue.
1 - You're uploading a large file and didn't change the php.ini file to allow file sized more than a certain value. change the value of these variables.
post_max_size = 2G
or 500M
upload_max_filesize=500M
2 - You've changed the php.ini but didn't restart the server.
3 - You've messed up the route.
4 - Your HTML form isn't correct. You might be missing:
enctype="multipart/form-data"
Also,
<input type="file" name="pic" accept="audio/*">
5 - You didn't change
file_uploads = On
``` in php.ini file

Related

data was not stored in laravel and not getting any errors

I tried to store data but data not store to database, the field in database and form input already match but still can't store data, and there is no actual message error. please help.
this is my controller:
public function store(Request $request)
{
$validatedData = $request->validate([
'kabupaten' => ['required'],
'provinsi' => ['required'],
'unit' => ['required'],
'satuan_kerja' => ['required'],
'nama_area' => ['required'],
'kode_area' => ['required']
]);
Area::create($validatedData);
return redirect('/dashboard/areas')->with('success','Area baru telah ditambahkan!');
}
this is the form input:
<form action="/dashboard/areas" method="POST">
#csrf
<div class="mb-3">
<label for="provinsi" class="form-label">Provinsi</label>
<input type="text" class="form-control" id="provinsi" name="provinsi" value="Jawa Tengah">
</div>
<div class="mb-3">
<label for="kabupaten" class="form-label">Kabupaten</label>
<input type="text" class="form-control" id="kabupaten" name="kabupaten" value="Brebes">
</div>
<div class="mb-3">
<label for="unit" class="form-label">Unit</label>
<input type="text" class="form-control" id="unit" name="unit" value="Pemerintah Kabupaten Brebes">
</div>
<div class="mb-3">
<label for="satuan_kerja" class="form-label">Satuan Kerja</label>
<input type="text" class="form-control" id="satuan_kerja" name="satuan_kerja" value="Pemerintah Desa Dumeling">
</div>
<div class="mb-3">
<label for="nama_area" class="form-label">Nama Area</label>
<input type="text" class="form-control" id="nama_area" name="nama_area">
</div>
<div class="mb-3">
<label for="kode_lokasi" class="form-label">Kode Lokasi</label>
<input type="text" class="form-control" id="kode_lokasi" name="kode_lokasi">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
And this is my area model:
class Area extends Model
{
use HasFactory;
protected $primaryKey = 'id_area';
protected $guarded = [
'id_area'
];
public function aset(){
return $this->hasMany(Aset::class, 'id_area');
}
}
Thank you if there anyone can help me with this problem, I really appreciate it.
So most likely your validation is failing, what you need to do is to display the results of the failed validation error messages, and you can do so in your blade file:
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
You may read more on how to display the errors here: https://laravel.com/docs/9.x/validation#quick-displaying-the-validation-errors
You can as well display it per input field or change the class of the input method, etc.. check the #error directive from here: https://laravel.com/docs/9.x/validation#the-at-error-directive

What is the cause of the "route not defined" error in this Laravel 8 application?

I am working on a Laravel application that requires user registration and login.
Alter registration, a user should have the possibility to add more info to his/her profile.
For this purpose, I did the following:
In routes/web.php I have the necessary routes, including update:
Auth::routes();
Route::get('/dashboard', [App\Http\Controllers\Dashboard\DashboardController::class, 'index'])->name('dashboard');
Route::get('/dashboard/profile', [App\Http\Controllers\Dashboard\UserProfileController::class, 'index'])->name('profile');
Route::post('/dashboard/profile/update', [App\Http\Controllers\Dashboard\UserProfileController::class, 'update'])->name('update');
In the newly created Controllers\Dashboard\UserProfileController.php I have:
namespace App\Http\Controllers\Dashboard;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;
use App\Models\UserProfile;
class UserProfileController extends Controller
{
public function index(UserProfile $user)
{
return view('dashboard.userprofile',
array('current_user' => Auth::user())
);
}
public function update(Request $request, $id)
{
$id = Auth::user()->id;
$request->validate([
'first_name' => ['required', 'string', 'max:255'],
'last_name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
]);
$current_user = User::find($id);
$current_user->first_name = $request->get('first_name');
$current_user->last_name = $request->get('last_name');
$current_user->email = $request->get('email');
$current_user->bio = $request->get('bio');
$current_user->avatar = $request->get('avatar');
$current_user->update();
return redirect('/dashboard/profile')->with('success', 'User data updated successfully');
}
}
In the view file that holds the form (resources\views\dashboard\userprofile.blade.php) I have:
<form action="{{ route('dashboard/profile/update') }}" enctype='multipart/form-data' method="post" novalidate>
{{csrf_field()}}
<div class="form-group">
<input type="text" id="first_name" name="first_name" placeholder="First name" class="form-control" value="{{$current_user->first_name}}">
#if ($errors->has('first_name'))
<span class="errormsg text-danger">{{ $errors->first('first_name') }}</span>
#endif
</div>
<div class="form-group">
<input type="text" id="last_name" name="last_name" placeholder="Last name" class="form-control" value="{{$current_user->last_name}}">
#if ($errors->has('first_name'))
<span class="errormsg text-danger">{{ $errors->first('last_name') }}</span>
#endif
</div>
<div class="form-group">
<input type="text" id="email" name="email" placeholder="E-mail address" class="form-control" value="{{$current_user->email}}">
#if ($errors->has('email'))
<span class="errormsg text-danger">{{ $errors->first('email') }}</span>
#endif
</div>
<div class="form-group">
<textarea name="bio" id="bio" class="form-control" cols="30" rows="6">{{$current_user->bio}}</textarea>
#if ($errors->has('bio'))
<span class="errormsg text-danger">{{ $errors->first('bio') }}</span>
#endif
</div>
<label for="avatar" class="text-muted">Upload avatar</label>
<div class="form-group d-flex">
<div class="w-75 pr-1">
<input type='file' name='avatar' id="avatar" class="form-control border-0 py-0 pl-0 file-upload-btn">
#if ($errors->has('file'))
<span class="errormsg text-danger">{{ $errors->first('avatar') }}</span>
#endif
</div>
<div class="w-25">
<img class="rounded-circle img-thumbnail avatar-preview" src="{{asset('images/avatars/default.png')}}" alt="{{$current_user->first_name}} {{$current_user->first_name}}">
</div>
</div>
<div class="form-group mb-0">
<input type="submit" name="submit" value='Save' class='btn btn-block btn-primary'>
</div>
</form>
The problem:
For a reason I was unable to figure out, whenever I am on the dashboard/profile route (in the browser), Laravel throws this error:
Route [dashboard/profile/update] not defined. (View: Path\to\views\dashboard\userprofile.blade.php)
What am I missing?
The route() function accepts a name, not a URL: https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes
So you should have used route('update'). Though seeing your code, you might not realize the ->name() method should accept a unique route name. So you should make sure you don't have any other route named 'update'.
Some people do this: ->name('dashboard.profile.update'). You can see if you like this convention.
I applied an easy fix, thanks to the info received from the community:
I replaced <form action="{{ route('dashboard/profile/update') }}" enctype='multipart/form-data' method="post" novalidate> with:
<form action="{{ route('update') }}" enctype='multipart/form-data' method="post" novalidate>

Laravel: I can't upload my dropzone file into my database

Hi I am new to laravel and javascript,
I have a multiple input forms for updating of product information including a image upload using Dropzone.js to update product image. However I cant see to update my image file as I keep getting a null value for my dropzone image when I have already dragged a file in.
Below are my codes in the product-edit page:
<form action="{{ route('merchant.product.update', $product->prodID) }}" method="POST" class="dropzone" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<h5>Code</h5>
<input type="text" name="prodCode" class="form-control" id="prodCode" placeholder="Product Code" value="{{ old('prodCode', $product->prodCode) }}" required>
<div class="spacer"></div>
</div>
<div class="form-group">
<h5>Description</h5>
<textarea id="prodDesc" name="prodDesc" class="form-control" id="prodDesc" placeholder="Description" rows="5" required> {{ $product->prodDesc }}</textarea>
<div class="spacer"></div>
</div>
<div class="form-group">
<h5>Price</h5>
<input type="text" name="price" class="form-control" id="price" placeholder="Price" value="{{ old('price', $product->price)}}">
<div class="spacer"></div>
</div>
<div class="form-group">
<h5>Quantity</h5>
<input type="number" name="quantity" class="form-control" id="quantity" placeholder="Quantity" value="{{ old('quantity', $product->quantity)}}">
<div class="spacer"></div>
</div>
<div class="form-group">
<h5>Feature Product</h5>
<div class="switch">
<input type="checkbox" name="featured" class="switch-input" value="1" {{ old('featured', $product->featured=="true") ? 'checked="checked"' : '' }}/>
<div class="circle"></div>
</div>
</div>
<div class="form-group">
<h5>Product Image</h5>
<div id="dropzoneDragArea" class="dz-default dz-message dropzoneDragArea">
<span>Upload Image</span>
</div>
<div class="dropzone-previews"></div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-large btn-primary">Update Product</button>
</div>
<div class="spacer"></div>
</form>
Below are the Javascript portion of the blade.php:
Below are my Controller Methods I have for the update:
public function storeImage(Request $request) {
if($request->file('file')){
// Upload path
$destinationPath = 'img/products/';
// Get File
$image = $request->file('file');
// Get File Name
$imageName = $image->getClientOriginalName();
// Uploading File to given path
$image->move($destinationPath,$imageName);
$product = new Product();
$product->where('prodID', '=', $request->{'prodID'}, 'AND', 'created_by_merchant_id', '=', $this->checkMerchantID())
->update([
'prodImage' => $imageName,
]);
}
}
//update Product details
public function update(Request $request, $prodID)
{
if ($this->isCodeExist($request->{'prodCode'}, $request->{'prodID'})) {
return back()->withErrors('This code already exists!');
}
try{
$db = new Product();
$db->where('prodID', '=', $prodID, 'AND', 'created_by_merchant_id', '=', $this->checkMerchantID())
->update([
'prodCode' => $request->{'prodCode'},
'prodImage' =>$this->storeImage($request),
'prodDesc' => $request->{'prodDesc'},
'price' => $request->{'price'},
'quantity' => $request->{'quantity'},
'featured' => $request->input('featured') ? true : false,
]);
return back()->with('success_message', 'Product updated successfully!');
} catch (\Illuminate\Database\QueryException $ex) {
Log::channel('merchant_error')->error($ex);
return back()->withErrors('There seems to be an error in updating');
}
}

Problem with updating data in database in Laravel

I have a form that has username, job, date of birth and city inputs. I am trying to update them through my form. When I click submit button my form submits but data remains unchanged. I successfully read them from database but when I try and update nothing happens. Any help is appreciated. Here is my code.
UserController.php
public function showProfile($username, Request $request)
{
$profileId = User::getIdFromUsername($username);
$userForShowProfile = User::with('userProfile')->where('id', $profileId)->first();
return view('profile.show', compact('userForShowProfile'));
}
public function updatePersonalData(UpdatePersonalDataRequest $request)
{
$user = Auth::user();
$request->validated();
$user->where('id', $user->id)->update(
[
'username' => $request->username,
'job' => $request->job,
'date_of_birth' => $request->date_of_birth,
'updated_at' => Carbon::now()
]
);
$city = City::where('name', $request['city'])->first();
if ($city != null && $city->count() > 0) {
$request->user()->city()->associate($city->id);
}
$request->user()->save();
return response()->json(null, 204);
}
web.php
Route::get('profile/{profile}', 'UserController#showProfile')->name('profile.show');
Route::patch('profile/personal', 'UserController#updatePersonalData')->name('profile.update.personal.data');
show.blade.php
<section data-edit="generalInfo" class="editGeneralInfo">
<form action="{{ route('profile.update.personal.data') }}" method="POST" class="flex">
#method('PATCH')
#csrf
<div class="form-group">
<label for="" class="textBold">Name</label>
<input type="text" name="username" value="{{ $userForShowProfile->username }}">
</div>
<div class="form-group">
<label for="" class="textBold">Ort</label>
<input type="text" name="job" value="{{ $userForShowProfile->job }}">
</div>
<div class="form-group">
<label for="" class="textBold">Beruf</label>
<input type="text" name="city" value="{{ $userForShowProfile->city->name }}">
</div>
<div class="form-group mb-0">
<label for="" class="textBold">Geburtsdatum</label>
<input type="text" placeholder="dd/mm/yyyy" name="date_of_birth" value="{{ $userForShowProfile->date_of_birth }}">
<p class="infoText mt-2">Dein Geburtsdatum wird nicht öffentlich angezeigt.</p>
<p class="infoText">Wir ermitteln damit nur dein Alter</p>
</div>
<div class="form-group">
<label for="" class="textBold">Button</label>
<input type="submit" class="form-control" name="submit" value="BUTTON">
</div>
</form>
</section>
rules
public function rules()
{
return [
'username' => ['string', 'max:255', 'unique:users,username'],
'job' => ['string', 'max:255'],
'date_of_birth' => ['date', 'date_format:d.m.Y'],
'city' => ['string', 'max:255', 'exists:cities,name']
];
}
I have a same problem in my code the error is:
I save like this : $user->save();
While i change the data using: $user and $user->profile
So i added in my code another line: $user->profile->save()
It work now

Laravel Reservation Form not Mailing

I have a 'contact' and 'reservation' form on a client site. The 'contact' form works well and when filled out, it sends the email to my mailtrap.io account. As far as I can see the structure of my 'reservation' form and controller is exactly the same as that of the 'contact form', however, no emails are sent out when the 'reservation' form is filled out. I can't figure out why.
When I dd the data on both the 'contact' and 'reservation' forms, I can see the array results. However, as mentioned, the emails for the 'reservation' form is not being sent to mailtrap.io.
Here's my web.php file
Route::get('reservation', 'PageController#reservation');
Route::post('reservation', 'PageController#postReservation');
Route::get('contact', 'PageController#contact');
Route::post('contact', 'PageController#postContact');
Here's my Controller for both the 'contact' and 'reservation' form. The contact and reservation functions are in the same controller:
public function reservation()
{
return view('reservation');
}
public function postReservation(Request $request)
{
dd($request->all()); //-> Fill in data to test if form works
$request->validate([
'name' => 'required|string|min:2',
'email' => 'required|email',
'phone' => 'required|digits:10',
'date' => 'required|date_format:Y/m/d|after:today',
'seats' => 'required|integer',
'message' => 'min:10'
]);
$data = array (
'name' => $request->name,
'email' => $request->email,
'phone' => $request->phone,
'date' => $request->date,
'time' => $request->time,
'seats' => $request->seats,
'reservationMessage' => $request->message,
);
Mail::send('emails.reservation', $data, function ($message) use ($data) {
$message->from('contact#venueat50.co.za');
$message->to('info#webdevpro.co.za');
$message->subject($data['name']);
});
return redirect('reservation')->with('success', 'Thank you. We will contact you to confirm the booking');
}
public function contact()
{
return view ('contact');
}
public function postContact(Request $request)
{
dd($request->all()); //-> Fill in data to test if form works
$request->validate([
'name' => 'required|string|min:2',
'email' => 'required|email',
'number' => 'required|digits:10',
'subject' => 'required|min:3',
'message' => 'required|min:10'
]);
$data = array(
'name' => $request->name,
'email' => $request->email,
'number' => $request->number,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from('contact#venueat50.co.za');
$message->to('info#webdevpro.co.za');
$message->subject($data['subject']);
});
return redirect('contact')->with('success', 'Your message has been sent. We will reply promptly');
}
I also have an email template setup for both the 'contact' and 'reservation' form. Not sure if that is needed.
Let me also attach the 'contact' and reservation forms:
Contact Form:
<form method="post" id="contact-form" action='{{ action('PageController#postContact') }}'>
{{ csrf_field() }}
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
<label>Name</label>
#if($errors->has('name'))
<small class="form-text invalid-feedback">{{ $errors->first('name') }}</small>
#endif
<p><input type="text" name="name" class="reservation-fields" value="{{ old('name') }}"/></p>
<label>Email</label>
#if($errors->has('email'))
<small class="form-text invalid-feedback">{{ $errors->first('email') }}</small>
#endif
<p><input type="text" name="email" class="reservation-fields" value="{{ old('email') }}"/></p>
<label>Mobile Nr</label>
#if($errors->has('number'))
<small class="form-text invalid-feedback">{{ $errors->first('number') }}</small>
#endif
<p><input type="text" name="number" class="reservation-fields" value="{{ old('number') }}"/></p>
<label>Subject</label>
#if($errors->has('subject'))
<small class="form-text invalid-feedback">{{ $errors->first('subject') }}</small>
#endif
<p><input type="text" name="subject" class="reservation-fields" value="{{ old('subject') }}"/></p>
<label>Message</label>
#if($errors->has('message'))
<small class="form-text invalid-feedback">{{ $errors->first('message') }}</small>
#endif
<p> <textarea name="message" id="msg-contact" class="reservation-fields" rows="7"></textarea></p>
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
<p class="contact-btn"><input type="submit" value="Send message" id="submit"/></p>
And Lastly the Reservation Form:
<form method="post" id="reservation-form" action={{ action('PageController#postReservation') }}>
{{ #csrf_field() }}
<div class="row">
<div class="col-md-4">
<label>Name*</label>
<p><input type="text" name="name" class="reservation-fields" required/></p>
</div>
<div class="col-md-4">
<label>Email*</label>
<p><input type="text" name="email" class="reservation-fields" required/></p>
</div>
<div class="col-md-4">
<label>Phone*</label>
<p><input type="text" name="phone" class="reservation-fields" required/></p>
</div>
</div>
<!--end row-->
<div class="row">
<div class="col-md-4">
<label>Date*</label>
<p><input type="date" name="datepicker" id="datepicker" class="reservation-fields" size="30" required/></p>
</div>
<div class="col-md-4">
<label>Time*</label>
<p>
<select name="time" class="reservation-fields" >
<option value="10:00">10:00</option>
<option value="11:00">11:00</option>
<option value="12:00">12:00</option>
<option value="13:00">13:00</option>
<option value="14:00">14:00</option>
<option value="15:00">15:00</option>
<option value="16:00">16:00</option>
</select>
</p>
</div>
<div class="col-md-4">
<label>Seats*</label>
<p><input type="text" name="seats" class="reservation-fields" required/></p>
</div>
</div>
<!--end row-->
<label>Special Requests</label>
<p> <textarea name="message" id="message2" class="reservation-fields" cols="100" rows="4" tabindex="4"></textarea></p>
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
<p class="alignc"><input type="submit" value="Book Now" id="submit" /></p>

Categories