Property Controller
public function store(CreatePropertyRequest $request)
{
$property = Property::create($request->except(['_token', 'property_photo']));
if($request->hasFile('property_photos')) {
foreach($request->file('property_photos') as $photo) {
$imageName = Storage::disk('public')->putFile('propertyImages/' . $property->id, $photo);
PropertyPhoto::create(['property_id' => $property->id, 'filename' => $imageName]);
}
}
return redirect()->route('property.index');
}
Property Model
//I have set the fillable field
public function propertyPhotos()
{
return $this->hasMany(PropertyPhoto::class, 'property_id');
}
PropertyPhoto Model
class PropertyPhoto extends Model
{
//
protected $fillable = ['property_id', 'filename'];
public function property()
{
return $this->belongsTo(Property::class);
}
public function getFilenameAttribute()
{
return 'storage/propertyImages/'. $this->property_id. '/' . $this->filename;
}
}
AND VIEW PAGE
{!! Form::open(['method' => 'POST','action' => 'PropertyController#store', 'enctype' => 'multipart/form-data']) !!}
{!! csrf_field() !!}
<div class="form-body">
<!-- <h3 class="card-title m-t-15">Property Information</h3> -->
<h3 class="box-title m-t-40">Property Details</h3>
<hr>
<div class="row">
<div class="col-md-8">
<div class="form-group">
{!! Form::label('property_title', 'Property Title') !!}
{!! Form::text('property_title', null, ['class' => 'form-control'] ) !!}
</div>
</div>
<!--/span-->
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_category', 'Category' . '*') !!}
{!! Form::select('property_category', ['sale' => 'Sale', 'rent' => 'Rent', 'lease' => 'Lease'], null, ['class' => 'form-control', 'placeholder' => '--Choose Property Category--']) !!}
</div>
</div>
<!--/span-->
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_group', 'Property Group') !!}
{!! Form::select('property_group', $propertygroups, null, ['class' => 'form-control', 'placeholder' => 'Property Group' ]) !!}
</div>
</div>
<!--/span-->
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_type', 'Type') !!}
{!! Form::select('property_type', [], null, ['class' =>'form-control', 'placeholder' => '---Property Type---' ] ) !!}
</div>
</div>
<!--/span-->
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_location', 'Location') !!}
{!! Form::select('property_location', $locations, null, ['class' => 'form-control', 'placeholder' => 'Choose Location']) !!}
</div>
</div>
<!--/span-->
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_area', 'Area') !!}
{!! Form::select('property_area',[], null, ['class' => 'form-control', 'placeholder' => '---Select Area---']) !!}
</div>
</div>
<div class="col-md-8">
<div class="form-group">
{!! Form::label('property_busstop', 'Bus Stop') !!}
{!! Form::text('property_busstop', null, ['class' =>'form-control', 'placeholder' => 'Closest Bus Stop to Property']) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<div class="form-group">
{!! Form::label('property_address', 'Address') !!}
{!! Form::text('property_address', null, ['class' => 'form-control', 'placeholder' => 'Address of the Property' ]) !!}
</div>
</div>
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_postcode', 'Post Code') !!}
{!! Form::text('property_postcode', null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_amount', 'Currency Type') !!}
{!! Form::select('property_amount',['n' => 'Naira Sign','s' => '$'], null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_amount', 'Amount') !!}
{!! Form::text('property_amount', null, ['class' => 'form-control' ]) !!}
</div>
</div>
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_size', 'Size') !!}
{!! Form::text('property_size', null, ['placeholder' => 'Property Size in sqr mtr','class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_bedrooms', 'No. of Bedrooms') !!}
{!! Form::text('property_bedrooms', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_bathrooms', 'No. of Bathrooms') !!}
{!! Form::text('property_bathrooms', null, ['class' => 'form-control' ]) !!}
</div>
</div>
<div class="col-md-4">
<div class="form-group">
{!! Form::label('property_toilet', 'No. of Toilet') !!}
{!! Form::text('property_toilet', null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
{!! Form::label('property_carpack', 'No. of Car Pack') !!}
{!! Form::text('property_carpack', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="col-md-6">
<div class="form-group">
{!! Form::label('property_mode', 'Mode') !!}
{!! Form::select('property_mode',['fully_furnish' => 'Fully Furnish', 'partly_furnish' => 'Partly Furnish', 'unfurnish' => 'Unfurnish'], null, ['class' => 'form-control' ]) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
{!! Form::label('property_social_medias', 'Social Media Link(s)') !!}
{!! Form::text('property_social_medias', null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
{!! Form::label('property_facilities', 'Facilities') !!}
{!! Form::text('property_facilities', null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
{!! Form::label('property_description', 'Description') !!}
{!! Form::textarea('property_description', null, ['class' => 'form-control','rows' => 3 ]) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
{!! Form::label('property_photos', 'Property Photo') !!}
{!! Form::file('property_photos', array('class' => 'form-control', 'multiple')) !!}
</div>
</div>
</div>
</div>
<div class="form-actions">
{!! Form::submit('Submit', ['class' => 'btn btn-success'] ) !!}
Go Back
</div>
{!! Form::close() !!}
</div>
</div>
</div>
<!--Displa Error Message Here -->
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<!---------End------------------>
DATABASE MIGRATION
for propertyphotos
public function up()
{
Schema::create('property_photos', function (Blueprint $table) {
$table->increments('id');
$table->integer('property_id')->unsigned()->index();
$table->string('filename');
$table->timestamps();
$table->foreign('property_id')
->references('id')->on('properties')
->onDelete('cascade');
});
}
The problem is: When I click the submit button, the records are inserted into property table, and no record whatsover is inserted to propertyphotos table, nor is any path created for property_images, and worst is that there is no error message.
Also, in my {!! Form::open() !!} I included the 'files' => true
I did change it to 'encytype' => 'multipart/form-data' and no luck.
Please, what am I missing? Is there a better way to achieve what I am trying to do? Any suggestion whatsoever would be highly appreciated. Thanks.
Please make sure you are using enctype= "multipart/form-data" within form tag
A review of what you have
You have a form where you add a property and multiple photos. It would be super helpful to know how your html looks like, but i'll just assume your input for the file is and that you have ... and you have added a hidden input with csrf token and you also do some validation behind that request to make sure the files are always sent. I will also assume you don;t so this using ajax (which would change the behavior a bit)
In Property model you have a reltionshop with PropertyPhoto called property_photos (should be propertyPhotos) and is hasMany
In PropertyPhoto you have a relation with Property model and it's called propertis (shoudld be called property since is belongs to)
In PropertyController you have a method called store where you want to store all this stuff. This is how you should do it:
Code
In Property and PropertyPhoto model add the column you want to save to in fillable like this
In Property:
protected $fillable = [
'property_title',
'property_category',
'property_group',
'property_type',
'property_location',
'property_area',
'property_busstop',
'property_address',
'property_postcode',
'property_amount',
'property_size',
'property_bedrooms',
'property_bathrooms',
'property_toilet',
'property_carpack',
'property_mode',
'property_social_medias',
'property_facilities',
'property_description',];
In PropertyPhotos
protected $fillable = ['property_id', 'filename'];
This is not mandatory, but makes the code look a lot cleaner. You can skip this
public function store(Request $request)
{
$property = Property::create($request->except['_token', 'property_photos']);
if($request->hasFile('property_photos')) {
$images = [];
foreach($request->file('property_photos') as $photo) {
$imageName = Storage::disk('public')->putFile('propertyImages/' . $property->id, $photo);
$images[]['filename'] = $imageName;
}
$property->property_images()->create($images);
}
return redirect()->route('property.index');
}
or
public function store(Request $request)
{
$property = Property::create($request->except['_token', 'property_photos']);
if($request->hasFile('property_photos')) {
foreach($request->file('property_photos') as $photo) {
$imageName = Storage::disk('public')->putFile('propertyImages/' . $property->id, $photo);
PropertyImages::create(['property_id' => $property->id, 'filename' => $imageName]);
}
}
return redirect()->route('property.index');
}
in PropertyImages
This is an accessor used to get the path to the image
public function getFilenameAttribute()
{
// edit here: since filename should include that stuff. Image should be in storage/propertyImages/propertyId
return 'storage/' . $this->filename;
}
finally php artisan storge:link
You should save all your files in storage. Later if you work with AWS of GCloud you can use S3 or Bucket to store files at lower costs
To access the image $propertyImage->filename; // just like that
Related
I have used this https://github.com/proengsoft/laravel-jsvalidation for client side validation.
Server side validation is working but client side onFocusout validation is not fire.
In composer file
Laravel 5.4
"proengsoft/laravel-jsvalidation": "^2.0"
In Controller
<?php
protected $validationRules=[
'email' => 'required|unique|max:255',
'name' => 'required',
'password' => 'required',
'userRoleId' => 'required'
];
public function create() {
$model = new Admuser();
$validator = JsValidator::make($this->validationRules);
$userRoleData = Userrole::orderBy('role')->pluck('role', 'userRoleId');
return view('adminlte::portaluser.create')->with([
'validator' => $validator,
'userRoleData' => $userRoleData,
]);
}
?>
And in View file for creating userdata
{!! Form::open(['url' => 'backoffice/portaluser/store', 'class' => 'form-horizontal']) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<fieldset>
<div class="col-sm-8">
<div class="form-group {{ $errors->has('name') ? 'has-error' : ''}}">
{!! Form::label('name', 'Name:', ['class' => 'col-lg-2 control-label']) !!}
<div class="col-lg-10">
{!! Form::text('name', $value = null, ['class' => 'form-control', 'placeholder' => 'Name']) !!}
{!! $errors->first('name', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : ''}}">
{!! Form::label('email', 'Email:', ['class' => 'col-lg-2 control-label']) !!}
<div class="col-lg-10">
{!! Form::email('email', $value = null, ['class' => 'form-control', 'placeholder' => 'Email', ]) !!}
{!! $errors->first('email', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('password') ? 'has-error' : ''}}">
{!! Form::label('password', 'Password:', ['class' => 'col-lg-2 control-label']) !!}
<div class="col-lg-10">
{!! Form::password('password', ['class' => 'form-control', 'placeholder' => 'Password', 'type' => 'password', ]) !!}
{!! $errors->first('password', '<p class="help-block">:message</p>') !!}
</div>
</div>
<div class="form-group {{ $errors->has('userRoleId') ? 'has-error' : ''}}">
{!! Form::label('userRoleId', 'Select Userrole', ['class' => 'col-lg-2 control-label'] ) !!}
<div class="col-lg-10">
{!! Form::select('userRoleId', $userRoleData, '', ['class' => 'form-control' ]) !!}
{!! $errors->first('userRoleId', '<p class="help-block">:message</p>') !!}
</div>
</div>
<button type="submit" class="submitbtn btn btn-primary">Submit</button>
</div>
</fieldset>
{!! Form::close() !!}
<!-- Scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/js/bootstrap.min.js"></script>
<!-- Laravel Javascript Validation -->
<script type="text/javascript" src="{{ asset('/jsvalidation/js/jsvalidation.js')}}"></script>
{!! $validator !!}
I guess Your question is not connected with jsvalidation but the logic how the OnFocusOut working.
Test how the event works:
<!DOCTYPE html>
<html>
<body>
Enter your name: <input type="text" id="fname" onfocusout="myFunction()">
<p>When you leave the input field, a function is triggered which transforms the input text to upper case.</p>
<script>
function myFunction() {
var x = document.getElementById("fname");
x.value = x.value.toUpperCase();
}
</script>
</body>
</html>
The onfocusout event occurs when an element is about to lose focus.
Tip: The onfocusout event is similar to the onblur event. The main difference is that the onblur event does not bubble. Therefore, if you want to find out whether an element or its child loses focus, you should use the onfocusout event.
Tip: Although Firefox does not support the onfocusout event, you can find out whether a child of an element loses focus or not, by using a capturing listener for the onblur event (using the optional useCapture parameter of the addEventListener() method).
Tip: The onfocusout event is the opposite of the onfocusin event.
Be sure that you need onfocusout not onmouseleave event.
I am having trouble updating my data in Laravel and this is giving me headache. Every time i clicked on the update button error occurs.
Below is the error:
"Route [admin/update] not defined. (View: C:\xampp\htdocs\Tailor\core\resources\views\expensesCat\edit.blade.php)"
here is the edit.blade
<div class="portlet light bordered">
<h3 class="page-title">Expenses Categories</h3>
{!! Form::model($expenses_category, ['method' => 'POST', 'route' => ['admin/update', $expenses_category->id]]) !!}
<div class="panel panel-default">
<div class="panel-heading">
Edit
</div>
<div class="panel-body">
<div class="row">
<div class="col-xs-12 form-group">
<input name="order_create_by" type="hidden" value="{{ Auth::user()->id }}">
{!! Form::label('name', 'Name*', ['class' => 'control-label']) !!}
{!! Form::text('name', old('name'), ['class' => 'form-control', 'placeholder' => '']) !!}
<p class="help-block"></p>
#if($errors->has('name'))
<p class="help-block">
{{ $errors->first('name') }}
</p>
#endif
</div>
</div>
</div>
</div>
The web.php
Route::post('/update/{id}', 'ExpensesCategoriesController#updateCat');
The ExpensesCategoriesController
public function updateCat(UpdateExpensesCategoriesRequest $request, $id)
{
$expenses_category = ExpensesCategory::findOrFail($id);
$expenses_category->update($request->all());
return redirect('admin/expenses_categories');
}
Use url in your form -
{!! Form::model($expenses_category, ['method' => 'POST', 'url' =>'admin/update/'.$expenses_category->id]) !!}
You're trying to use the route by its name, but you didn't name it. So, change it to:
'route' => ['admin.update', ....
And name the route:
Route::post('/update/{id}', 'ExpensesCategoriesController#updateCat')->name('admin.update');
Or:
Route::post('/update/{id}', ['as' => 'admin.update', 'uses' => 'ExpensesCategoriesController#updateCat']);
I want make search by parameters. But it shows i have mixing GET and POST methods. (Error message: MethodNotAllowedHttpException
No message). Blade form by default have POST. i changed to GET. Route have GET method. Maybe you can see what i am doing wrong. This is my VIEW:
{!! Form::open([ 'action' => ['HomePageController#index', 'method' => 'get']]) !!}
<div class="container">
<div class="col-xs-2 form-inline">
{!! Form::label('city_id', trans('quickadmin.companies.fields.city').'', ['class' => 'control-label']) !!}
{!! Form::select('city_id', $cities, old('city_id'), ['class' => 'form-control select2') !!}
</div>
<div class="col-xs-3 form-inline">
{!! Form::label('categories', trans('quickadmin.companies.fields.categories').'', ['class' => 'control-label']) !!}
{!! Form::select('categories', $categories, old('categories'), ['class' => 'form-control select2']) !!}
</div>
<div class="col-xs-3 form-inline">
{!! Form::label('search', trans('quickadmin.companies.fields.name').'', ['class' => 'control-label']) !!}
{!! Form::text('search', old('search'), ['class' => 'form-control', 'placeholder' => 'Search']) !!}
</div>
<div class="form-inline">
<div class="col-xs-2">
<button type="submit"
class="btn btn-primary">
Search
</button>
</div>
</div>
</div>
{!! Form::close() !!}
My controller:
public function index( Request $request)
{
$cities = \App\City::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$categories = \App\Category::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$name = $request->input('city_id');
$companies = \App\Company::All()->where('city_id', '=', $name);
return view('table', compact('companies', $companies, 'cities', $cities, 'categories', $categories));
My route:
Route::get('/', 'HomePageController#index');
Thank you for your help.
There is a problem in the form open, try it like this :
{!! Form::open([ 'action' => 'HomePageController#index', 'method' => 'get']) !!}
I try to save two values from text field but actually with this view when i click on save i get only the first ID of the foreach.
the two text field are : resultat_domicile and resultat_visiteur i would like to get the values foreach objects
someone know how to achieve this ? thanks a lot in advance
here my view :
#foreach($rencontres as $rencontre)
{!! Form::open(array('route' => array('add.resultat', $rencontre->id)))!!}
<div class="box-body">
<div class="row">
<div class="col-md-5">
<h2 class="pull-right"><div class="fa fa-shield pull-right"></div>{{$rencontre->equipe_domicile->lb_equipe}}</h2>
</div>
<div class="col-md-2">
<div class="row">
<div class="col-xs-5">
{!! Form::text('resultat_domicile', null , ['class' => 'form-control', 'placeholder' => 'Ex: 20 points']) !!}
</div>
<div class="col-xs-2 text-center">
<span class="h2">-</span>
</div>
<div class="col-xs-5">
{!! Form::text('resultat_visiteur', null , ['class' => 'form-control', 'placeholder' => 'Ex: 20 points']) !!}
</div>
</div>
</div>
<div class="col-md-5">
<h2 class="pull-left"><div class="fa fa-shield pull-left"></div>{{$rencontre->equipe_visiteur->lb_equipe}}</h2>
</div>
</div>
<div class="box-footer">
<div class="col-xs-12 text-center">
{!! Form::submit('Save', ['class' => 'btn btn-info btn-lg center-block']) !!}
{!! Form::close() !!}
</div>
</div>
#endforeach
here my controller :
public function addResultat(Request $request , $id){
$rencontre = Rencontre::findOrFail($id);
$rencontre->resultat_domicile = $request->input('resultat_domicile');
$rencontre->resultat_visiteur = $request->input('resultat_visiteur');
dd($rencontre);
}
Are you sure you're not only retrieving the LAST of the 'recontres'? When you create those form elements with the names 'resultat_domicile' and 'resultat_visiteur', you are overwriting which field gets sent to the request.
If you put a [] after the name, then they will be an array of values when they get sent to the request.
For example, this would be:
{!! Form::text('resultat_domiciles[]', ...
My project is on storing reviews on products. I am using Laravel framework here. The review has a field for rating the product. I am using the jquery 'starrr' plugin for the rating.
Form :
{!! Form::open(['url'=>'/admin/reviews', 'role' => 'form', 'class' => 'form-horizontal']) !!}
<div class="form-group">
{!! Form::label('title', "Title:", ['class'=>'control-label col-md-2']) !!}
<div class="col-md-8">
{!! Form::text('title', null, ['class'=>'form-control'])!!}
</div>
</div>
<div class="form-group">
{!! Form::label('product_id', "Product:", ['class'=>'control-label col-md-2']) !!}
<div class="col-md-8">
{!! Form::select('product_id', $products, null, ['class'=>'form-control product_list'])!!}
</div>
</div>
<div class="form-group">
{!! Form::label('rating', "Rating:", ['class'=>'control-label col-md-2']) !!}
<div class="starrr"></div>
{!! Form::hidden('rating', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
<div class="col-md-3 col-md-offset-4">
{!! Form::button('<i class = "fa fa-plus-circle"></i> Add New Review', ['type' => 'submit', 'class'=>'btn btn-primary btn-lg']) !!}
</div>
</div>
{!! Form::close() !!}
controller store method:
public function store(Request $request){
$rules = [
'product_id' => 'required',
'title' => 'required',
'content' => 'required',
];
$this->validate($request, $rules);
create($request->all());
return redirect('admin/reviews')->withSuccess('Review is created');
}
My question is how to get the rating value from the star rating, into the request object to store on the database?
Thanks for your help in advance
$('.starrr').starrr({
change: function(e, value){
$('input[name="value"]').val(value)
}
})
This will set the input to the value of the starrr plugin whenever it is changed. Then the input value will be sent to the server