'multipart/form-data' not working - php

I'm using Laravel and trying to build a gallery, i'm testing the upload of a file but i when i attach a file and click submit i can't get a positive outcome using the test set up. The code is below
GalleryController
// Store Gallery
public function store(Request $request){
// Get Request Input
$name = $request->input ('name');
$description = $request->input ('description', '');
$cover_image = $request->input ('cover_image');
$owner_id = 1;
// Check Image Upload
if($cover_image){
die ('YES');
} else {
die ('NO');
}
}
The form is set up as follows
{!! Form::open(array('action' => 'GalleryController#store', 'enctype' => 'multipart/form-data')) !!}
{!! Form::label ('name', 'Name') !!}
{!! Form::text ('name', $value = null, $attributes = ['placeholder' => 'Gallery Name', 'name' => 'name']) !!}
{!! Form::label ('description', 'Description') !!}
{!! Form::text ('name', $value = null, $attributes = ['placeholder' => 'Gallery Description', 'name' => 'Description']) !!}
{!! Form::label ('cover_image', 'Cover Image') !!}
{!! Form::file('cover_image') !!}
{!! Form::submit ('Submit', $attributes = ['class' => 'button']) !!}
{!! Form::close() !!}
Any help is appreciated
Thanks

Your form looks correct, it is likely your controller where you are retrieving the uploaded file.
As per the docs, to retrieve an uploaded file you should use $request->file():
$request->file('cover_image');
That link to the docs above also goes on to explain how you can check the file properly and store the file.

//use input facades.
use File;
use Illuminate\Support\Facades\Input;
public function store(Request $request){
// Get Request Input
$name = $request->input ('name');
$description = $request->input ('description', '');
$cover_image = $request->input ('cover_image');
$owner_id = 1;
// Check Image Upload
if(Input::hasFile('cover_image'){
die ('YES');
}
else {
die ('NO');
}
}

Related

always return null while uploading file to laravel 7

I'm having an issue while uploading file to laravel either its pdf or image always return to null
This is the View
{!! Form::open(['action' => 'TransactionInController#store', 'method' => 'POST', 'autocomplete' => 'off',
'class' => 'form-horizontal', 'enctype' => 'multipart/form-data']) !!}
<div class="row">
{{ Form::label('Device Document', '', ['class' => 'col-sm-2 col-form-label']) }}
<div class="col-sm-7">
{{ Form::file('device_document') }}
<p style="color: red;">#error('device_document') {{ $message }} #enderror</p>
</div>
</div>
{!! Form::close() !!}
and this is the Controller i use
public function store(Request $request)
{
$this->validate($request, [
'device_document' => 'nullable|max:8192|mimes:pdf'
]);
$transactionsin = new TransactionIn;
$imageName = $request->input('device_document');
$request->image->move(public_path('document-image'), $imageName);
$transactionsin->save();
return redirect('/transactionsin');
}
i know its been asked before and i already try several way to upload file this error.
This is the error message i get while running the code
Call to a member function move() on null
but if i change the code in controller into something more simple like this
public function store(Request $request)
{
$this->validate($request, [
'device_document' => 'nullable|max:8192|mimes:pdf'
]);
$transactionsin = new TransactionIn;
$transactionsin->device_document = $request->input('device_document');
$transactionsin->save();
return redirect('/transactionsin');
}
it will not return any error message but it will saved as null in the database.
Use $request->file('device_document') instead of input method to catch a file.
If you would like to get original name of the uploaded file, you may do so using the getClientOriginalName() method
Try this :
public function store(Request $request)
{
$this->validate($request, [
'device_document' => 'nullable|max:8192|mimes:pdf'
]);
$transactionsin = new TransactionIn;
$imageName = $request->file('device_document');
$imageName->move(public_path('document-image'), $imageName->getClientOriginalName());
$transactionsin->device_document = $request->file('device_document')->getClientOriginalName();
$transactionsin->save();
return redirect('/transactionsin');
}
See the official documentation here
you can access file using file() method not input method and after upload image to get image path using asset() function like this below code
$transactionsin = new TransactionIn;
$image= $request->file('device_condition');
//upload imaage
$image->move(public_path('document-image'), $image->getClientOriginalExtension());
//asset() function use store path
$transactionsin->device_document = asset('document-image/'.$image->getClientOriginalExtension());
$transactionsin->save();

laravel delete multiple record from different database at once

I want delete two record from different table with one delete function. however it is only deleting one of the record even though I passed two different Ids.
this is my delete function
public function delete($id){
$user_id = auth() ->user()->id;
$card = Card::where('id', $id)->delete();
$actCity = city::where('id', $id)->delete();
return redirect('/home')->with('success', 'Post Removed');
this is my delete button
#if (!empty($cardd && $actCity))
{!!Form::open(['action'=>['PlanController#delete','id' =>$cardd[0], 'id'=>$actCity[0]],'method'=>'POST','class'=>''])!!}
#endif
{{Form::hidden('_method','DELETE')}}
{!! Form::submit('Delete', array(
'class' => 'btn btn-danger',
'onclick' => "if( ! confirm('Are you sure you want to delete your
package?')){return false;}"
)) !!} {!! Form::close() !!}
my route
Route::delete('delete/{id}', 'PlanController#delete');
You can not use same parameter name more than once,
Do something like this :
public function delete($id, $city_id){
$user_id = auth() ->user()->id;
$card = Card::where('id', $id)->delete();
$actCity = city::where('id', $city_id)->delete();
return redirect('/home')->with('success', 'Post Removed');
this is my delete button
#if (!empty($cardd && $actCity))
{!!Form::open(['route'=>['PlanController#delete','id' =>$cardd[0], 'city_id'=>$actCity[0]],'method'=>'POST','class'=>''])!!}
#endif
{{Form::hidden('_method','DELETE')}}
{!! Form::submit('Delete', array(
'class' => 'btn btn-danger',
'onclick' => "if( ! confirm('Are you sure you want to delete your
package?')){return false;}"
)) !!} {!! Form::close() !!}
my route
Route::delete('delete/{id}/{city_id}', 'PlanController#delete');

Display stored image in Laravel

I am new to this, I have stored image in public/storage but I can't figure out how to display it.
I have this function in UploadController:
public function store(request $request)
{
if ($request->hasFile('file')) {
$request->file('file');
return $request->file->store('public');
}
return 'No file selected';
}
This in web.php:
Route::get('/', 'UploadController#index');
Route::post('store', 'UploadController#store');
And this in welcome.blade.php:
{!! Form::open(['url' => '/store', 'method' => 'post', 'files' => true]) !!}
File name:
{!! Form::text('episode') !!}
{!! Form::file('file'); !!}
{!! Form::submit('Upload File') !!}
{!! Form::token() !!}
{!! Form::close() !!}
It displays name of the saved file in /store
Can anyone please give me an advice how to display the image?
'$request->file->store('public');' will return a path to your saved file with a unique name so save the returned path in your database or where ever like below:
$path = $request->file->store('public');
Now $path variable contains path (url) to your saved file.
And use this path to display your stored image.

How to customize a field of the model before storing in database

I have a model like this :
class Article extends Model {
protected $fillable = [
'title',
'body',
'img_profile', // store name of image
];
}
And a controller like this:
public function store(ArticleRequest $request){
$article = Auth::user()->articles()->create($request->all());
return redirect('articles');
}
And a form like this:
{!! Form::model($article = new \App\Article,['url' => 'articles','files' => true ]) !!}
{!! Form::text('title', null ) !!}
{!! Form::textarea('body', null) !!}
{!! Form::file ('img_profile', '') !!}
{!! Form::submit('Submit') !!}
{!! Form::close() !!}
When I submit the form, the img_profile field is stored default cached file which is /private/var/tmp/phpceBMP2.But I just want to update file_name in img_profile.
How can I change the value of the img_profile request before storing it in database?
You should use mutator for Article model this way:
public function setImgProfileAttribute($value)
{
$this->attributes['img_profile'] = '/private/var/tmp/phpceBMP2/'. $value;
}
However you should think if you really want to store the path in DB this way. What if you will want to change it in future? You will have to update all the records?

Laravel - Resource route passing in wrong parametre (not the id)

I have an issue with my the update method in my controller.
Normally everything works and it takes care of itself, but this time its not working.
My controller:
public function update($id)
{
$input = Input::all();
$validation = Validator::make($input, Vehicle::$rules, Vehicle::$messages);
if ($validation->passes())
{
$this->vehicle->update($id, $input);
return Redirect::route('admin.vehicles.index')->with('success', 'Car Updated');
}
return Redirect::back()
->withInput()
->withErrors($validation);
}
Repository:
public function update($id, $input)
{
print_r($id);
die();
}
This prints out:
{vehicle}
from the URI:
http://localhost/admin/vehicles/1/edit
My form:
{{ Form::open(['route' => 'admin.vehicles.update', 'class' => 'form-horizontal edit-vehicle-form', 'method' => 'PATCH']) }}
// inputs
<div class="form-group">
{{ Form::submit('Update Vehicle', ['class' => 'btn btn-success']) }}
</div>
{{ Form::close() }}
Route:
Route::resource('/admin/vehicles', 'VehiclesController');
Where is this going wrong? How can I get this form to send the ID not the word vehicle?

Categories