foreach loop in a form to save values - laravel

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[]', ...

Related

How can I keep selected the previous value when updating a form?

I am using laravel collctive form. I want to update only the quantity field of the following form keeping the blood_id field readonly. When I submit the form I am not getting blood_id value.
How can i solve it?
{!! Form::model($bloodBank, ['route' => ['bloodBanks.update', $bloodBank->id], 'method' => 'put']) !!}
<div class="row">
<div class="col-lg-8">
<div class="form-group">
{!! Form::label('blood_id', 'Blood Group', ['class' => 'form-control-label']);!!}
{!! Form::select('blood_id', $bloods , null , ['placeholder' => 'Choose Blood Group',"class"=>"form-control",'disabled' => true]) !!}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-8">
<div class="form-group">
{!! Form::label('quantity','Quantity', ['class' => 'form-control-label']);!!}
{!! Form::number("quantity",null, ["class"=>"form-control form-control-label",'min'=>'0']) !!}
<span class="validation-error">{{ $errors->first("quantity") }}</span>
</div>
</div><!-- col-12 -->
</div>
<button class="btn btn-info">Update </button>
{!! Form::close() !!}
For disabled fields, you may want to add hidden fields which won't display on the rendered page BUT will be included in the request object. E.g.
{{ Form::hidden('blood_id', $bloods) }}
This is in addition to the displayed field you already have which is disabled.

Link Button Not Working in Laravel

I want to edit a record in a CRUD application in laravel where I have a button which has been linked to go to the index view but when I click it, it redirects me to the UPDATE method of the controller.
This is my form:
{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
<div class="row col-md-10 col-md-offset-1 panel">
<div class="col-md-8 col-md-offset-2">
<br />
<div class="form-group">
{{ Form::label('name', 'Player Name') }}
{{ Form::text('name', $player->name, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Upload Image') }}
{{ Form::file('pic') }}
</div>
<div class="form-group">
{{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
{!! Form::close() !!}
<a href="{{ route('players.index') }}">
<button class="btn btn-danger" >Cancel</button>
</a>
</div>
</div>
</div>
I have the following button for going back to the index page but this is taking me to the UPDATE method of the controller:
<a href="{{ route('players.index') }}">
<button class="btn btn-danger" >Cancel</button>
</a>
This is my index method in the controller:
public function index()
{
$players = Player::paginate(5);
return view('players.index', compact('players'));
}
This is the UPDATE method in the controller:
public function update(Request $request, $id)
{
return "Hi";
}
This is my route file contents:
Route::resource('news', 'NewsController');
Route::resource('competition', 'CompetitionsController');
Route::resource('players', 'PlayersConroller');
Everything looks fine to me but I don't know what goes wrong here.
Any help is appreciated in advance.
I am not sure if it will solve your issue, try to put your button code outside the form-group div.
You can change your code as
Cancel
You can check your html you have put button inside form tag which of type submit that's why it is submitting the form again.
Replace your form code with:
<div class="row col-md-10 col-md-offset-1 panel">
<div class="col-md-8 col-md-offset-2">
{!! Form::open(['route' => ['players.update', $player->id], 'method' => 'PUT', 'files'=>'true']) !!}
<br />
<div class="form-group">
{{ Form::label('name', 'Player Name') }} {{ Form::text('name', $player->name, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('file', 'Upload Image') }} {{ Form::file('pic') }}
</div>
<div class="form-group">
{{Form::button('Save Record', ['type' => 'submit', 'class' => 'btn btn-success'])}}
</div>
{!! Form::close() !!}
</div>
<a href="{{ route('players.index') }}">
<button class="btn btn-danger">Cancel</button>
</a>
</div>

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

My view displays raw blade code

I'm new to Laravel and using ver 5.5. I have a simple view and when I run the code, instead of showing the rendered version, I see the Blade commands.
This is the view code:
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>{{ $title }}</h2>
</div>
<div class="pull-left">
{!! \App\Combine\BaseCombine::tableHeader($collection) !!}
{!! \App\Combine\BaseCombine::tableData($collection) !!}
{!! \App\Combine\BaseCombine::tableFooter($collection) !!}
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('transaction.index') }}"> Back</a>
</div>
</div>
</div>
#endsection
Here is the code in the controller that executes the view:
return view('general.genericTable',
[
'title' => __FUNCTION__,
'transactionID' => $transactionsID,
'type' => $type,
'collection' => TransactionCombine::agents($transactionsID, $type),
]
);
This is what I see when I execute the code:
#section('content')
{{ $title }}
{!! \App\Combine\BaseCombine::tableHeader($collection) !!} {!! \App\Combine\BaseCombine::tableData($collection) !!} {!! \App\Combine\BaseCombine::tableFooter($collection) !!}
Back
#endsection
What have I done wrong?

Undefined offset 0 laravel blade

I am using following route
Route::get('admin/new/password','PostController#newPasswordForm');
Route::post('admin/new/password','PostController#newPasswordStore');
and function is like this
public function newPasswordForm() {
return view('pages.posts.password');
}
and in view file this is the code
#section('content')
<div class="container bg-white">
<div class="row padding-top-80 padding-bottom-40">
<div class="col-md-12">
{!! Form::open(['url' => 'newpassword','method'=>'POST']) !!}
<div class="col-md-8">
<h2>Enter Your Password</h2>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password') !!}
</div>
<div class="form-group">
{!! Form::label('confirmpassword', 'Confirm Password') !!}
{!! Form::password('password_confirmation') !!}
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
{!! Form::submit('Save', ['class' => 'btn btn-primary btn-large']) !!}
</div>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
#endsection
but I am getting following error.
ErrorException in FormBuilder.php line 11:
Undefined offset: 0 (View: ..\resources\views\pages\posts\password.blade.php)
I cannot understand what I am doing wrong. Please help me out.TIA.

Resources