can't upload audio file to database - php

I'm trying to upload audio file to database, but nothing happens and i don't get any errors.
controller:
public function store (Request $request)
{
$this->validate(request(), [
'title' => 'required'
]);
$muzika = new Muzika;
if ($request->hasFile('featured_muzika')) {
$daina = $request->file('featured_muzika');
$filename = time(). '.' .$daina->getClientOriginalExtension();
$location = public_path('muzika/' . $filename);
Storage::disk('local')->save($location);
$muzika->daina = $filename;
}
$muzika->daina = $filename;
$muzika->title = $request->title;
$muzika->save();
return redirect('/');
}
this is my form, at first i tried only for title, it was storing in to DB, when i added store method for file it stopped working
{!! Form::open(array('route' => 'muzika.store', 'files' => true)) !!}
{{csrf_field()}}
{!! Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
{{ Form::label('featured_muzika', 'Upload Featured mp3:')}}
{{ Form::file('featured_muzika')}}
{!! Form::submit('Post', ['class' => 'btn btn-primary']) !!}
{!! Form:: close () !!}
when i press submit it only redirects. Database stays empty

In laravel 5.5 you can do
$muzika = new Muzika();
$path = request()->file('featured_muzika')->store('/muzika');
$muzika->daina = $path;
$muzika->save();
make sure your form have enctype="multipart/form-data" set

Related

Input::hasFile() not working

I am trying to check if the image that i am trying to upload is getthing processed, but when i try to use de Input::hasFile('upimg') to check it out it doesnt do anything. This is my form:
{!! Form::open(array('route' => array('uploadimage'), 'method' => 'POST')) !!}
{!! csrf_field() !!}
<div class="form-group">
{!! Form::label('newimg', 'Choose Image to upload:', ['class' => 'control-label']) !!}
{!! Form::file('upimg', null) !!}
</div>
<div class="form-group">
{!! Form::label('directory', 'Choose directory:', ['class' => 'control-label']) !!}
{!! Form::select('newimage', [ '' => 'none'] + $imagesdir , '', ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Upload', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
And this is the code i am using to check the file:
public function doUpload()
{
if (Input::hasFile('upimg')){
echo 'uploaded';
};
}
As you can see i am trying to check:
{!! Form::file('upimg', null) !!}
If i am not wrong its suppose to give mi back the echo but nothing happens. Why is this not working? what am i doing wrong?
Be sure you have an 'file' => true option in your form definition like below:
{!! Form::open(['route' => ['uploadimage'], 'file' => true]) !!}
This option causes that your form will be render with enctype="multipart/form-data" option in HTML form tag. See documentation: HTML enctype
You don't have to add method => 'POST' to your definition of form, because Laravel 5 sents forms with POST method by default.
Please let me know if this solution will not work for you.
For checking files on server side you can use methods of Request object like in documentation of Laravel: Files in Laravel

Error when updating database after deploying to server. Laravel 5

I have one form that adds data to two different tables (Articles & Deals). An Article has many deals. A deal has one Article. There are multiple deals with different dealnames that the user inputs on the create and edit form. I can update the 'deals' part of my database fine in my local/vagrant dev environment but I get a 'Creating default object from empty value' error when I try on my live site.
It says the problem is in the update function of my Articles Controller.
I'm using Laravel 5.
The Articles Table has: id(primary), title, image, description, address.
The Deals table has: id(primary), dealname, article_id (index), dayID.
The only difference I can see between my dev and live environment is that the index (article_id) on the 'Deals' table doesn't have a key icon next to it in PHPMyAdmin. But the foreign key r/ship is set correctly.
You can see all the code here: https://github.com/lakemck/gethappy
Articles Controller- Update
public function update(ArticleRequest $request, $id)
{
$article = Article::findOrFail($id);
if( $request->hasFile('image') ){
// photo saving stuff.
}
$article->update($request->all());
for($i = 0; $i < sizeof($request->input('dealname')); $i++) {
//Error is supposedly on the next line.
$article->deals->where('dayID',($i + 1))->first()->dealname = $request->input('dealname')[$i];
$article->deals->where('dayID',($i + 1))->first()->save();
}
return redirect('/');
}
Form
{!! Form::model($article, ['route' => ['articleUpdate_path', $article->id], 'files' => true, 'method' => 'PATCH']) !!}
{!! Form::label('title','TITLE') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
{!! $errors->first('title','<p class="error">:message</p>')!!}
{!! Form::label('image','PHOTO') !!}
{!! Form::file('image', null, ['class' => 'form-control']) !!}
{!! Form::label('description','DESCRIPTION') !!}
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
#foreach ($article->deals as $deal)
#if($deal->dayID == '1' )
{!! Form::label('dealname','Monday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '1']) !!}
#endif
#if($deal->dayID == '2' )
{!! Form::label('dealname','Tuesday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '2']) !!}
#endif
#if($deal->dayID == '3' )
{!! Form::label('dealname','Wednesday') !!}
{!! Form::text('dealname[]', $deal->dealname, null, ['class' => 'form-control', 'id' => '3']) !!}
#endif
#endforeach
{!! Form::label('address','ADDRESS') !!}
{!! Form::text('address', null, ['class' => 'form-control']) !!}
{!! Form::close() !!}
Article Model
class Article extends Model
{
public function deals()
{
return $this->hasMany('App\Deal');
}
protected $fillable = array('title', 'photo', 'description', 'address');
}
DEAL MODEL
class Deal extends Model
{
public function article()
{
return $this->belongsTo('App\Article')->withTimestamps();
}
protected $fillable = array('dealname', 'article_id', 'dayID');
}
In the end I had to change the code in my ArticlesController update function to this:
for($i = 0; $i < sizeof($request->input('dealname')); $i++) {
$deal = $article->deals()->where('dayID', $i + 1)->first();
$deal->dealname = $request->input('dealname')[$i];
$deal->save();
}
return redirect('/');
}
Note the brackets on the deals().

Laravel 5 validation rule required_with:password_confirmation

I have a problem with validation rule in edit form. My form fields are name, email, password and password_confirmation. My UpdateUserRequest class return this rules:
return [
'name' => ['required', 'max:50', 'min:3'],
'email' => ['required', 'email', 'unique:users,email,' .$this->route('users')],
'password' => ['required_with:password_confirmation', 'confirmed']
];
And update method in UsersController is:
public function update(Requests\UpdateUserRequest $request, $id)
{
$user = $this->users->findOrFail($id);
$user->fill($request->only('name', 'email', 'password'))->save();
return redirect(route('backend.users.edit', $user->id))->with('status', 'User has been updated.');
}
And this is a form:
{!! Form::model($user, [
'method' => $user->exists ? 'put' : 'post',
'route' => $user->exists ? ['backend.users.update', $user->id] : ['backend.users.store']
]) !!}
<div class="form-group">
{!! Form::label('name') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::email('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password_confirmation') !!}
{!! Form::password('password_confirmation', ['class' => 'form-control']) !!}
</div>
{!! Form::submit($user->exists ? 'Save User' : 'Create New User', ['class' => 'btn btn-primary btn-sm']) !!}
Password validate rule required_with:password_confirmation should allow user to leave empty fields password and password_confirmation if he does not want to change it, but this doesn't work. When I leave the field empty, the empty value is hashed and stored in database. All other validations work fine, but not when user leaves empty value in password and password_confirmation. Why this isn't working? What am I doing wrong?
You will need to updated the db seperate without using fill etc as it will always get the password value empty or not and then add it, so try:
$user = $this->users->findOrFail($id);
$user->name = $request->get('name');
$user->email = $request->get('emil');
if(!empty($request->get('password')) {
$user->password = $request->get('password');
}
$user->save();
Something like that so you are checking if the PW needs to also be updated etc.
Basically you need to verify if it need to be entered or not, your method takes all the requested values and will update no matter if they are empty or note.

Laravel :Trying to get property of non-object on file input

Im trying to do a submit a file with a form submission however I continue to get this error:
Error:
Trying to get property of non-object
at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/Users/plastics1509moore/Desktop/elephant_gin/app/Http/Controllers/AdminController.php', '33', array('request' => object(Request), 'input' => array('_token' => 'y0ExMD4FoH3y1hRX61IOvMW520rn7AEx0UOzrc2R', 'title' => 'lol', 'description' => 'picture of gin one', 'link' => 'www.google.com', 'image' => object(UploadedFile)))) in AdminController.php line 33
I have files set to true. Is the issue the request all?
Here is the Controller function:
public function createSlider(Request $request)
{
$input = Request::all();
if (Input::hasFile('image')) {
$imageName = $input->id . '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image/', $imageName
);
$input->image = $imageName;
}
Sliders::create($input);
return redirect('/admin');
}
HTML
{!!Form::open(array('url' => 'admin/new_slider', 'files' => true)) !!}
<div class = "form-group">
{!!Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!!Form::text('title', null, ['class'=> 'input-mini ina tch'])!!}
{!!Form::label('title', 'Description:', ['class' => 'control-label']) !!}
{!!Form::text('description', null, ['class'=> 'input-mini '])!!}
</div>
<div class = "form-group">
{!!Form::label('title', 'Link:', ['class' => 'control-label']) !!}
{!!Form::text('link', null, ['class'=> 'input-mini'])!!}
{!!Form::label('title', 'Image:', ['class' => 'control-label']) !!}
{!! Form::file('image', ['id' => 'imgInp', 'class' => 'prev-upload']) !!}
</div>
<div class = "form-group">
{!!Form::submit('Submit', ['class'=> 'btn btn-default'])!!}
</div>
{!! Form::close() !!}
You are trying to get the id from the input. Your form isn't passing any id so naturally, your input won't have the id.
You can create the slider first and then get the id of the slider like this:
public function createSlider(Request $request)
{
$input = Request::all();
// Create slider
$slider = Sliders::create($input);
if (Input::hasFile('image')) {
// Use the slider id
$imageName = $slider->id . '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image/', $imageName
);
$input->image = $imageName;
}
return redirect('/admin');
}

Lravel 5.1 How to find database records through a from?

In the admin area of my app, I want to have a form by which an admin can find a project by its id and show the project details there. What is the best approach to implement this?
This is what I have tried:
//route
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
//form
{!! Form::open(['action' => 'AdminController#showProject', 'method' => 'get']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
//controller method
public function showProject(Request $request)
{
$project=Project::find($request->get('project_id'));
return view('admin.projects.showProject', compact('project'));
}
It almost worked but there is little problem. After retrieving the requested project, the ULR is like this:
admin/projects/%7Bproject_id%7D?project_id=5
I want it be like this one:
admin/projects/5
How can I solve this problem?
Create the following routes:
Route::get('admin/projects', 'AdminController#getProject');
Route::post('admin/projects', 'AdminController#postProject');
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
In your getProject function you return a view that show the form where the user can enter an ID. (The one you already have):
{!! Form::open(['action' => 'AdminController#postProject', 'method' => 'post']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
In your postProject function you just send a redirect to admin/project/{project_id} URL:
public function postProject(Request $request)
{
return redirect('admin/projects/' . $request->project_id);
}
In your showProject function you just retrieve the record and return a view with the information:
public function showProject($ProjectID)
{
$project=Project::find($ProjectID);
return view('admin.projects.showProject'),
->with('Project', compact('project'));
}
Try changing your controller method to
public function showProject($project_id)
{
$project=Project::find($project_id);
return view('admin.projects.showProject', compact('project'));
}
You don't need to use request on get route.

Categories