Data not saving in Database on Laravel Forms - laravel

I am trying to Update user Profile after they Login, It works well and says PROFILE UPDATED SUCCESSFULLY but nothing inserted on the Database Table, so in essence, the View shows nothing of the Updated form fields. Please anyone with an idea of where i am getting this wrong will be highly appreciated, Thanks.
CONTROLLER Update FUNCTION
public function update(Request $request)
{
$rules = [
'name' => 'required',
'email' => 'required',
'phone' => 'required|numeric',
'country' => 'required',
'gender' => 'required',
'birthday' => 'required',
'fb' => 'url',
'twitter' => 'url',
'gp' => 'url',
'instagram' => 'url',
'personal_site' => 'url',
'aboutme' => 'url',
'linkedin' => 'url',
'pinterest' => 'url'
];
$data= $request->all();
$validator = Validator::make($data, $rules);
if($validator->fails()){
return Redirect::back()->withInput()->withErrors($validator);
}
$user = Auth::user();
$user->name = $data['name'];
$user->email = $data['email'];
$user->phone = $data['phone'];
$user->country = $data['country'];
$user->birthday = $data['birthday'];
$user->address = $data['address'];
if($user->save()) {
$profile_id = $user->id;
$profile = Profile::find($profile_id);
if(count($profile) > 0) {
$profile->gender = $data['gender'];
$profile->city = $data['city'];
$profile->state = $data['state'];
$profile->aboutmyself = $data['aboutmyself'];
$profile->fb = $data['fb'];
$profile->twitter = $data['twitter'];
$profile->gp = $data['gp'];
$profile->instagram = $data['instagram'];
$profile->personal_site = $data['personal_site'];
$profile->aboutme = $data['aboutme'];
$profile->linkedin = $data['linkedin'];
$profile->pinterest = $data['pinterest'];
// $profile = $user->profile()->save($profile);
$profile->save();
}
} else {
return redirect()->back()->withInput()->withInfo("Something went wrong. Please, try again");
}
return redirect()->route('profile')->withSuccess("Your Profile Successfully Updated.");
}
MY VIEW (profile-edit.blade.php)
<div class="form-group row">
{!! Form::model($user, array('route' => 'post.edit.profile', 'method' => 'post', 'class' => 'form-horizontal')) !!}
{!! Form::label('name', "Full Name", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-8">
<div class="row">
<div class="col-md-9">
{!! Form::text('name', null, array('class' => 'form-control', 'placeholder' => 'Your Full Name', 'required' => 'required')) !!}
</div>
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('email', "Email Address", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-6">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">
<i class="material-icons md-18 text-muted">mail</i>
</span>
{!! Form::email('email', null, array('class' => 'form-control', 'placeholder' => '', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('phone', "Phone Number", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-6">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">
<i class="material-icons md-18 text-muted">mail</i>
</span>
{!! Form::text('phone', null, array('class' => 'form-control', 'placeholder' => 'e.g. +8801711223344', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('gender', "Gender", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
{!! Form::select('gender', $gender, array('class' => 'c-select form-control', 'id' => '', 'required' => 'required')) !!}
</div>
</div>
<div class="form-group row">
{!! Form::label('birthday', "Birthday", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('birthday', null, array('class' => 'datepicker form-control', 'placeholder' => '01/28/2016','id' => 'birthday', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('address', "Address", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('address', null, array('class' => 'form-control', 'placeholder' => 'Street No., Area...','id' => 'address', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('city', "City", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('city', $user->profile->city, array('class' => 'form-control', 'placeholder' => 'City', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('state', "State", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('state', $user->profile->state, array('class' => 'form-control', 'placeholder' => 'State', 'required' => 'required')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('country', "Country", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-8">
<div class="row">
<div class="col-md-6">
{!! Form::text('country', null, array('class' => 'form-control', 'placeholder' => 'Country','id' => '')) !!}
</div>
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('aboutmyself', "About Me", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::textarea('aboutmyself', Auth::user()->profile->aboutmyself, array('class' => 'form-control', 'rows' => 4, 'placeholder' => 'About Yourself')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('fb', "Facebook Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('fb', $user->profile->fb, array('class' => 'form-control', 'placeholder' => 'https://facebook.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('twitter', "Twitter Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('twitter', $user->profile->twitter, array('class' => 'form-control', 'placeholder' => 'https://twitter.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('gp', "Google+ Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('gp', $user->profile->gp, array('class' => 'form-control', 'placeholder' => 'https://plus.google.com/+username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('personal_site', "Personal Site", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('personal_site', $user->profile->personal_site, array('class' => 'form-control', 'placeholder' => 'http://www.mywebsite.me')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('instagram', "Instagram Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('instagram', $user->profile->instagram, array('class' => 'form-control', 'placeholder' => 'https://www.instagram.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('linkedin', "LinkedIn Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('linkedin', $user->profile->linkedin, array('class' => 'form-control', 'placeholder' => 'https://www.linkedin.com/username')) !!}
</div>
</div>
</div>
<div class="form-group row">
{!! Form::label('pinterest', "Pinterest Link", array('class' => 'col-sm-3 control-label')) !!}
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon2">
<i class="material-icons md-18 text-muted">language</i>
</span>
{!! Form::text('pinterest', $user->profile->pinterest, array('class' => 'form-control', 'placeholder' => 'https://www.pinterest.com/username')) !!}
</div>
</div>
</div>
<!-- <div class="form-group row">
<label for="password" class="col-sm-3 form-control-label">Change Password</label>
<div class="col-sm-6 col-md-4">
<div class="input-group">
<span class="input-group-addon" id="basic-addon3">
<i class="material-icons md-18 text-muted">lock</i>
</span>
<input type="text" class="form-control" placeholder="Enter new password">
</div>
</div>
</div> -->
<div class="form-group row">
<div class="col-sm-8 col-sm-offset-3">
<div class="media">
<div class="media-left">
{!! Form::submit('Save Changes', array('class' => 'btn btn-success')) !!}
</div>
<!-- <div class="media-body media-middle p-l-1">
<label class="c-input c-checkbox">
<input type="checkbox" checked>
<span class="c-indicator"></span> Subscribe to Newsletter
</label>
</div> -->
</div>
</div>
</div>
{!! Form::close() !!}
User MODEL (User.php)
<?php
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword,EntrustUserTrait {
EntrustUserTrait::can as may;
Authorizable::can insteadof EntrustUserTrait;
}
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public function profile(){
return $this->hasOne('App\Profile','user_id','id');
}
public function pending(){
return $this->hasMany('App\PendingTransfers', 'user_id', 'id');
}
public function transaction(){
return $this->hasMany('App\Transaction', 'user_id', 'id');
}
}
ROUTE
Route::group(array('middleware' => 'auth'), function()
{
Route::get('logout', ['as' => 'logout', 'uses' => 'Auth\AuthController#logout']);
Route::get('profile', ['as' => 'profile', 'uses' => 'UsersController#profile']);
Route::get('edit-profile', ['as' => 'edit.profile', 'uses' => 'UsersController#edit']);
Route::post('edit-profile', ['as' => 'post.edit.profile', 'uses' => 'UsersController#update']);
Route::post('edit-photo', ['as' => 'post.edit.photo', 'uses' => 'UsersController#photoUpdate']);
Route::get('dashboard', array('as' => 'dashboard', 'uses' => 'Auth\AuthController#dashboard'));
Route::get('change-password', array('as' => 'password.change', 'uses' => 'Auth\AuthController#changePassword'));
Route::post('change-password', array('as' => 'password.doChange', 'uses' => 'Auth\AuthController#doChangePassword'));
UPDATE: Thanks for the Prompt response but that didn't fix it though, Am thinking the Fault might be from the CONTROLLER FUNCTION at this point:
if($user->save()) {
$profile_id = $user->id;
$profile = Profile::find($profile_id);
if(count($profile) > 0) {
$profile->gender = $data['gender'];
$profile->city = $data['city'];
$profile->state = $data['state'];
$profile->aboutmyself = $data['aboutmyself'];
$profile->fb = $data['fb'];
$profile->twitter = $data['twitter'];
$profile->gp = $data['gp'];
$profile->instagram = $data['instagram'];
$profile->personal_site = $data['personal_site'];
$profile->aboutme = $data['aboutme'];
$profile->linkedin = $data['linkedin'];
$profile->pinterest = $data['pinterest'];
// $profile = $user->profile()->save($profile);
$profile->save();
OR Probably from the VIEW Opening Form route/variable $user
{!! Form::model($user, array('route' => 'post.edit.profile', 'method' => 'post', 'class' => 'form-horizontal')) !!}

In User Model set all tables fields $fillable
protected $fillable = ['name', 'email', 'password','phone','country','gender'];

Related

Laravel view load twice to display comment

I don't know why the page has to load twice to display comments.
Here is my route: Route::post('/addComment', 'CommentsController#addComment');
Here is my controller:
public function addComment(Request $request)
{
$this->validate($request, [
'name' => 'required',
'body' => 'required',
]);
$lesson_id = $request->lesson_id;
$comment = new Comment;
$comment->name = $request->input('name');
$comment->body = $request->input('body');
$comment->parrent_id = '0';
$comment->lesson_id = $request->lesson_id;
$comment->save();
return back();
}
Here is my view:
<div class="leave_review">
<h3 class="blog_heading_border"> コメント </h3>
{!! Form::open(['action' => ['CommentsController#addComment'], 'method' => 'POST', 'id' => 'postForm' ]) !!}
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input type="hidden" id ="lesson_id" name="lesson_id" value="{{$lesson->id}}" />
</div>
<div class="row">
<div class="col-sm-6">
#error('name')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
{{Form::label('name','名前')}}
{{Form::text('name', '', ['class' => 'form-group', 'id' => 'name' ]) }}
</div>
<div class="col-sm-12">
#error('body')
<div class="alert alert-danger">{{ $message }}</div>
#enderror
{{Form::label('body','メッセージ')}}
{{Form::textarea('body', '', ['class' => 'form-group', 'id' => 'body']) }}
</div>
</div>
<div class="row">
<div class="col-md-12">
</div>
</div>
{{Form::submit('Submit', ['class' => 'send mt_btn_yellow pull-right', 'id' => 'submit'])}}
{!! Form::close() !!}
{{-- End add comment --}}
{{--Display comment--}}
<ol class="review-lists">
#foreach ($comment as $value)
<li class="comment">
<div class="activity_rounded">
<img src="/storage/icon/icon.jpg" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$value->name}}
<small class="date-posted pull-right">{{ \Carbon\Carbon::parse($value->created_at)->diffForHumans() }}</small>
</h4>
<p>{{$value->body}} </p>
<button class="pull-left mt_btn_yellow" onclick="toggleReply('{{$value->id}}')">返事</button>
{{-- ENd Display comment--}}
#foreach ($comment as $value)
<li class="comment">
<div class="activity_rounded">
<img src="/storage/icon/icon.jpg" alt="image"> </div>
<div class="comment-body">
<h4 class="text-left">{{$value->name}}
<small class="date-posted pull-right">{{ \Carbon\Carbon::parse($value->created_at)->diffForHumans() }}</small>
</h4>
<p>{{$value->body}} </p>
<button class="pull-left mt_btn_yellow" onclick="toggleReply('{{$value->id}}')">返事</button>
{{-- ENd Display comment--}}
you don't have a #endforeach

Laravel required if validation issue

Laravel required if validation issue
Blade:
{{ Form::open(['route' => ['updateEmailSettings']]) }}
<div class="form-group row">
{{ Form::label('driver','Mail Driver',['class' => 'col-md-3 col-form-label required']) }}
<div class="col-md-9">
{{ Form::select('driver',$drivers, null,['class' => 'form-control', 'placeholder' => 'Select']) }}
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label font-weight-bold">Mandrill</label>
</div>
<div class="form-group row">
{{ Form::label('mailgun_secret','Secret',['class' => 'col-md-3 col-form-label']) }}
<div class="col-md-9">
{{ Form::text('mailgun["secret"]',null,['class' => 'form-control', 'id' => 'mailgun_secret']) }}
</div>
</div>
<div class="form-group row">
<div class="col-md-9 ml-md-auto">
{{ Form::button('<i class="far fa-save"></i> Save',['class'=>'btn btn-primary mr-3','type'=>'submit']) }}
<a class="btn btn-danger" href="{{ route('emailSettings') }}"><i class="far fa-times-circle"></i> Cancel</a>
</div>
</div>
{{ Form::close() }}
Form Request:
return [
'driver' => 'required',
'mailgun.*.domain' => 'required_if:driver,mailgun'
];
Validation always fails. Please suggest me if i miss anything.
Resolved myself
Blade: Removed double quotes inside the bracket.
{{ Form::text('mailgun[secret]',null,['class' => 'form-control', 'id' => 'mailgun_secret']) }}
Form Request: Removed asterisk
'mailgun.domain' => 'required_if:driver,mailgun'

Please what am I missing, Am using Laravel Framework

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

Route [admin/update] not defined in Laravel

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']);

'htmlentities() expects parameter 1 to be string, array given' error on Laravel view

I was testing the Request.php (submitting form with empty required fields) of my create view, and got the 'htmlentities() expects parameter 1 to be string, array given' error.
I know the error was generated because of the code below. This is a part of my view (calls javascript functions) used to create future appointment reminders.
I just don't see what is causing the error exactly.
<div style="display:none">
<div id="reminderiteminitial">
<div class="row" style="padding: 0.5em 0 0.5em 0.5em">
<div class="col-sm-2" >
{!! Form::text('reminder_time[]', 30, ['class' => 'form-control', 'placeholder' => '']) !!}
</div>
<div class="col-sm-2" >
{!! Form::select('reminder_timeunit[]', ['minute' => 'minutes(s)', 'hour' => 'hour(s)', 'day' => 'day(s)', 'week' => 'week(s)', 'month' => 'month(s)'], null, ['class' => 'form-control']) !!}
</div>
<div class="col-sm-2" >
{!! Form::select('delivery_method[]', ['eml' => 'Email'], null, ['class' => 'form-control']) !!}
</div>
<div class="col-sm-4" >
{!! Form::text('delivery_contact[]', null, ['class' => 'form-control', 'placeholder' => 'Your email address']) !!}
</div>
<div class="col-sm-2" >
<input class="btn btn-default" type="button" value="Delete reminder" onClick="removeitem(this);">
</div>
</div>
</div>
</div>
<div id="reminderitem">
</div>
<div class="row" style="padding: 1.5em 0 2.5em 0">
<div class="col-sm-12">
<input class="btn btn-default" type="button" value="Add a reminder" onClick="addreminderitem();">
</div>
</div>
Javascript:
<script language='javascript' type='text/javascript'>
var itemCount = 1;
var limit = 6;
function addreminderitem(){
if (itemCount == limit) {
alert("You have reached the maximum number of reminders.");
}
else {
var newreminderitem = document.getElementById('reminderitem');
var initialreminderitem_clone = document.getElementById('reminderiteminitial').cloneNode(true);
initialreminderitem_clone.id = 'item_'+itemCount++;
newreminderitem.appendChild(initialreminderitem_clone);
}
}
function removeitem(item){
item.parentNode.parentNode.parentNode.remove();
itemCount = itemCount - 1;
}
</script>
Whoops shows:
reminder_time
array:1 [
0 => "30"
]
reminder_timeunit
array:1 [
0 => "minute"
]
delivery_method
array:1 [
0 => "eml"
]
delivery_contact
array:1 [
0 => ""
]
Update #2
What's strange is that if I replace the following HTML forms code:
<div class="col-sm-2" >
{!! Form::text('reminder_time[]', 30, ['class' => 'form-control', 'placeholder' => '']) !!}
</div>
<div class="col-sm-2" >
{!! Form::select('reminder_timeunit[]', ['minute' => 'minutes(s)', 'hour' => 'hour(s)', 'day' => 'day(s)', 'week' => 'week(s)', 'month' => 'month(s)'], null, ['class' => 'form-control']) !!}
</div>
<div class="col-sm-2" >
{!! Form::select('delivery_method[]', ['eml' => 'Email'], null, ['class' => 'form-control']) !!}
</div>
<div class="col-sm-4" >
{!! Form::text('delivery_contact[]', null, ['class' => 'form-control', 'placeholder' => 'Your email address']) !!}
</div>
<div class="col-sm-2" >
<input class="btn btn-default" type="button" value="Delete reminder" onClick="removeitem(this);">
</div>
with the html it generates:
<div class="col-sm-2" >
<input class="form-control" placeholder="" name="reminder_time[]" type="text" value="30">
</div>
<div class="col-sm-2" >
<select class="form-control" name="reminder_timeunit[]"><option value="minute">minutes(s)</option><option value="hour">hour(s)</option><option value="day">day(s)</option><option value="week">week(s)</option><option value="month">month(s)</option></select>
</div>
<div class="col-sm-2" >
<select class="form-control" name="delivery_method[]"><option value="eml">Email</option></select>
</div>
<div class="col-sm-4" >
<input class="form-control" placeholder="Your email address" name="delivery_contact[]" type="text">
</div>
<div class="col-sm-2" >
<input class="btn btn-default" type="button" value="Delete reminder" onClick="removeitem(this);">
</div>
I don't get the error!?
Erorr
Your field names must be declared as string not as array:
<input name="reminder_time" />
instead of
<input name="reminder_time[]">`
Validating array
Or you can change your validation rules for array (https://laravel.com/docs/5.3/validation#validating-arrays):
$rules = [
'reminder_time.*' => 'unique',
],

Resources