I have Laravel 5.5 Application. I am trying to update my post and image is not updating. When I die and dump the following
dd($request->hasFile('image'));
it outputs false
View:
<div class="box-body">
{{ Form::model($slider, array('route' => array('admin.slider.update', $slider->title), 'method' => 'PUT')) }}
<div class="form-group">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control', 'placeholder' => 'Title']) !!}
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'placeholder' => 'Content']) !!}
</div>
<div class="row">
<div class="col-md-2 col-xs-6">
<div class="form-group">
{!! Form::label('image', 'Change Image') !!}
{!! Form::file('image') !!}
</div>
</div>
<div class="col-md-10 col-xs-6">
<img src="{{ secure_asset('images/slider/thumb/' . $slider->image )}}" class="img-responsive" width="150">
{{ $slider->image }}
</div>
</div>
{!! Form::submit('Submit', array( 'class'=>'btn btn-info' )) !!}
{!! Form::close() !!}
</div>
Controller:
public function sliderupdate($slider, Request $request){
$slider = Slider::where('title', $slider)->firstorfail();
$slider->title = $request->title;
$slider->content = $request->content;
dd($request->hasFile('image'));
if ($request->hasFile('image')) {
$file = Input::file('image');
//getting timestamp
$timestamp = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString());
$name = $timestamp. '-' .$file->getClientOriginalName();
$file->move(public_path().'/images/slider/', $name);
$slider->image = $name;
$thumb = Image::make(public_path().'/images/slider/' . $name)->resize(1920,1080)->save(public_path().'/images/slider/thumb/' . $name, 90);
}
$slider->save();
return redirect()->route('admin.slider.edit', $slider->title)->with('status', 'Update Success');
}
Route:
Route::put('admin/slider/{slider}', [
'uses' => 'AdminController#sliderupdate',
'as' => 'admin.slider.update'
]);
Something I am missing, but couldn't find out what it is.
You are missing the 'files' => true option. You need files option in the array passed to Form::open or Form::model if you are going to accept files.
{{ Form::model($slider, [
'route' => ['admin.slider.update', $slider->title],
'method' => 'PUT',
'files' => true
]) }}
Method:
public function slideredit($slider){
$slider = Slider::where('title', $slider)->firstOrFail();
return view('admin.slider.edit', compact('slider'));
}
Change your Form to
{!! Form::model($slider, ['route' => ['admin.slider.post', $slider->title, 'method' => 'PUT', 'files'=>true]]) !!}
<div class="modal-body">
<!-- text input -->
<div class="form-group">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control', 'placeholder' => 'Title']) !!}
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'placeholder' => 'Content']) !!}
</div>
<div class="form-group">
{!! Form::label('image', 'Choose Image') !!}
{!! Form::file('image') !!}
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
{!! Form::submit('Submit', array( 'class'=>'btn btn-info')) !!}
</div>
{!! Form::close() !!}
Related
I have a Laravel site running on a GoDaddy share hosting plan. I'm on PHP version 7.1. It has functioned fine up until yesterday. Now the blade views no longer show changes that occur on the database.
I see that the POST functions are working, as the changes are reflected in the database. They're just not showing up in the views themselves.
I've tried to clear the view cache as suggested here: Blade view not reflecting changes
I have also changed the timezone in config/app to 'America/Phoenix' to try and match godaddy's servers: https://www.godaddy.com/community/cPanel-Hosting/How-To-Change-Timezone-on-a-shared-server/td-p/102712
I've contacted GoDaddy's tech support and they couldn't find anything they believed could cause it.
Example Route:
//Resource
Route::resource('beer', 'BeerController');
Example Controller:
public function update(Request $request, Beer $beer)
{
$this->validate($request, ['name' => 'required']);
$beer->update(request(['name', 'beer_style_id', 'style', 'abv', 'ibus', 'srm', 'brewery_id', 'on_tap']));
return view('beers.show', compact('beer'));
}
Example View
#extends('layouts.master')
#section('content')
<div class="row">
<div class="col-sm-12">
<h1>Edit {{ $beer->name }}</h1>
<hr />
{!! Form::model($beer, ['route' => ['beer.update', $beer->id], 'method' => 'patch']) !!}
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('brewery_id', 'Brewery') !!}<br />
{!! Form::select('brewery_id', $breweries, null, ['placeholder' => 'Select Brewery', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="row" style="margin-bottom: 1rem;">
<div class="col">
{!! Form::label('beer_style_id', 'Style Family') !!}<br />
{!! Form::select('beer_style_id', $beerstyles, null, ['placeholder' => 'Select Style', 'class' => 'custom-select mb-2', 'style' => 'width:100%;']) !!}
</div>
<div class="col">
{!! Form::label('style', 'Style') !!}
{!! Form::text('style', $value = null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="row" style="margin-bottom: 1.5rem;">
<div class="col">
{!! Form::label('abv', 'ABV') !!}
<div class="input-group">
{!! Form::number('abv', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
<span class="input-group-addon">%</span>
</div>
</div>
<div class="col">
{!! Form::label('ibus', 'IBUs') !!}
{!! Form::number('ibus', $value = null, ['class' => 'form-control']) !!}
</div>
<div class="col">
{!! Form::label('srm', 'SRM') !!}
{!! Form::number('srm', $value = null, ['class' => 'form-control', 'step' => '.1']) !!}
</div>
</div>
<div class="form-group" id="on-tap-checkbox">
{!! Form::checkbox('on_tap', '1') !!} On Tap
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
Cancel
</div>
#include('layouts.errors')
{!! Form::close() !!}
</div>
</div>
#endsection
Here is my code:
controller file: EmergencyContactsController.php
$surnames = DB::table('salutations')->pluck('name');
return view('patient/emergencycontacts', ['salutation' => $surnames]);
Blade file: patient/emergencycontacts.blade.php
{!! Form::open(array('route' => 'emergencycontacts_store', 'class' => 'form')) !!}
<div class="form-group">
{!! Form::label('Salutation') !!}
{{ Form::select('role', ['' => 'Select Role'] + $salutation, null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{!! Form::label('First Name') !!}
{!! Form::text('firstname', null, array('required', 'class'=>'form-control', 'placeholder'=>'First Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Last Name') !!}
{!! Form::text('lastname', null, array('required', 'class'=>'form-control', 'placeholder'=>'Last Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Relationship') !!}
{{ Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) }}
</div>
<div class="form-group">
{!! Form::label('Phone') !!}
{!! Form::text('phone', null, array('required', 'class'=>'form-control', 'placeholder'=>'Phone')) !!}
</div>
<div class="form-group">
{!! Form::label('Fax') !!}
{!! Form::text('fax', null, array('class'=>'form-control', 'placeholder'=>'Fax')) !!}
</div>
<div class="form-group">
{!! Form::submit('Save',array('class'=>'btn btn-primary')) !!}
</div>
{{ Form::close() }}
When I go to url http://localhost:8000/patient/emergency-contacts/create it gives me error:
"Unsupported operand types"
You need to change 2 things
In your controller:
$surnames = DB::table('salutations')->pluck('name', 'id')->toArray();
So you get an array as [id => 'value'] and not only ['value']. In the View:
{!! Form::select('role', $salutation, null, ['class' => 'form-control']) !!}
{!! Form::select('relationship', ['Father', 'Mother', 'Husband','Wife','Son','Daughter','Uncle','Aunty','Other']) !!}
{!! Form::close() !!}
Always 'escape' the Form tags, if not, the HTML will be printed on screen, not parsed.
please use view('patient.emergencycontacts', ['salutation' => $surnames]);
My form.blade.php is sth like
<div class="form-group">
{!! Form::label('title', 'title :', ['class' => 'awesome']) !!}
{!! Form::text('product[title]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', 'description : ', ['class' => 'awesome']) !!}
{!! Form::text('product[description]', null, ['class' => 'form-control']) !!}
<div id="phone" class="form-group">
{!! Form::label('reference_id1', 'reference_id1 : ', ['class' => 'awesome']) !!}
{!! Form::text('product[reference_id1]', null, ['class' => 'form- control']) !!}
</div>
<div class="form-group">
{!! Form::label('category_id', 'category_id : ', ['class' => 'awesome']) !!}
{!! Form::select('category[]', $categories,null, ['class' => 'form- control', 'multiple']) !!}
</div>
<div class="form-group">
{!! Form::label('color', 'color : ', ['class' => 'awesome']) !!}
{!! Form::text('feature[0][color]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('height', 'height : ', ['class' => 'awesome']) !!}
{!! Form::text('feature[0][height]', null, ['class' => 'form-control']) !!}
</div> `
and my Edit.blade.php is like
{!! Form::model($product,['method' => 'PATCH', 'action' => ['ProductController#update',$product->id]]) !!}
#include('products.form', ['submitBtn' => 'submit'])
{!! Form::close() !!}
And this my ProductController.php#edit
public function edit($id)
{
$product = Product::with('feature')->findOrFail($id);
$categories = Category::pluck('title','id');
return view('products.edit')->withProduct($product)->withCategories($categories);
}
this is while when i wanna edit a product, the input requests are set empty!!
for instance when i go to http://myLarave/Public/product/2/edit the title and other inputs are empty :(
any suggestions?!
In your route.php or web.php depend to the version of your laravel, you can make the arg {id?}, for example:
Route::get('edit/{id?}', 'ProductController#edit');
and in the edit function you can initialize the variable $id=null or empty:
public function edit($id = null)
{
if($id != null){
$product = Product::with('feature')->findOrFail($id);
$categories = Category::pluck('title','id');
return view('products.edit')->withProduct($product)->withCategories($categories);
}
}
I have this problem in my Public function Store of Laravel 4, i can upload an update images, pretty cool, but I cannot create a new record :( appears: Call to a member function getClientOriginalName() on a non-object, in this line:
$filename = $image->getClientOriginalName();This is complete code.
public function store(){
$image = Input::file('image');` <br/>
$filename = $image->getClientOriginalName();
if(Input::hasFile('image')){
Input::file('image')->move(public_path().'/assets/img/', $filename);
}
$titulo = new Titulo();
$titulo->id = Input::get('id');
$titulo->title = Input::get('title');
$titulo->description = Input::get('description');
$titulo->date = Input::get('date');
$titulo->image = $filename;
$titulo->category_id = Input::get('category_id');
$titulo->save();
}
This is my form
{{ Form::open (['route' => 'titulos.store', 'class'=> 'form', 'method' =>'post', 'files'=> 'true']) }}
{{ Form::label('title', "Titulo:", ['class' => 'col-sm-2 control-label']) }}
{{ Form::text('title') }}
{{ $errors->first('title') }}
<div class="form-group">
{{ Form::label('date', "Fecha:", ['class' => 'col-sm-2 control-label']) }}
<input type="date" name="date" >
</div>
{{ Form::label('description', "Description:", ['class' => 'col-sm-2 control-label']) }}
{{ Form::textarea('description') }}
{{ $errors->first('description') }}
<div class="form-group">
{{ Form::file('image') }}
</div>
{{ Form::label('category_id', 'Category:', ['class' => 'col-sm-2 control-label']) }}
<div class="col-sm-10">
{{ Form::select('category_id', array('1' => 'TBLeaks', '2' => 'Quejas', '3' => 'Denuncias', '4' => 'Ideas'), null, array('class' => 'form-control')) }}
</div>
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
{{ Form::submit('Submit', ['class' => "btn btn-primary"]) }}
</div>
</div>
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
<a class="btn btn-success" href="{{ URL::to('admin') }}">Back to Admin</a>
</div>
</div>
{{ Form::close() }}
</div>
You missed the enctype part. Your open form should be
{{ Form::open (['route' => 'titulos.store', 'enctype' => 'multipart/form-data', 'class'=> 'form', 'method' =>'post', 'files'=> 'true']) }}
To much apporoaches, my controller:
public function store(Request $request)
{
$this->validate($request, ['title' => 'required',
'date' => 'required',
'image_1' => 'required|mimes:png,jpeg',
]);
$user = Auth::user()->id;
$report = new Report($request->all());
$report->author_id = $user;
$image = $request->file('image_1');
$destinationPath = 'uploads/reports';
$ext = $image->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$ext;
$report->image_1 = $image->move($destinationPath, $fileName);
$report->save();
Session::flash('flash_message', 'Report added!');
return redirect('dash/reports');
}
and my create view is:
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Crea un report</h1>
<p>I campi sono tutti obbligatori. In caso di difficoltà , fare riferimento ad <strong>eloquent</strong> l'assistente virtuale.</p>
<hr/>
{!! Form::open(['url' => '/dash/reports', 'files' => true, 'class' => 'form-horizontal']) !!}
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('fake', 'Nome e cognome', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
<input class="form-control" id="disabledInput" type="text" placeholder="{{ $author->name }} {{ $author->surname}}" disabled>
</div>
</div>
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('title', 'Servizio', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::text('title', null, ['class' => 'form-control', 'required' => 'required']) !!}
{!! $errors->first('title', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('title') ? 'has-error' : ''}}">
{!! Form::label('date', 'Data', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::text('date', \Carbon\Carbon::now()->format('d/m/Y'), ['class' => 'form-control', 'required' => 'required']) !!}
{!! $errors->first('date', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('category_id') ? 'has-error' : ''}}">
{!! Form::label('category_id', 'Cliente', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::select('category_id', $category, null, ['class' => 'form-control'] ) !!}
{!! $errors->first('category_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('image_1') ? 'has-error' : ''}}">
{!! Form::label('image_1', 'Upload report cartaceo', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
<p>Solo immagini .jpg/.png</p>
{!! Form::file('image_1', null, ['class' => 'form-control', 'required' => 'required']) !!}
{!! $errors->first('image_1', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
{!! Form::submit('Create', ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! Form::close() !!}
#if ($errors->any())
<ul class="alert alert-danger">
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
</div>
#endsection
Now, if I submit a form with a error, nothing appear, why? Also, my "session flash" does not work, i just use the apzzcoder/crud generator, i just follow the guide. Validation work correctly, but nothing appear