how to fix Object of class Illuminate\Database\Eloquent\Collection could not be converted to int - laravel

when i want to create new post it is return this error Object of class Illuminate\Database\Eloquent\Collection could not be converted to int (View: C:\xampp\htdocs\new-project\resources\views\admin\posts\create.blade.php
public function create()
{
$categories = Category::all('id','name');
return view('admin.posts.create')->with('categories', $categories);
}
my view is
<div class="row">
<div class="col-md-8 col-md-offset-2 panel panel-default">
<h1>ایجاد پست</h1>
{!! Form::open(['method' => 'POST','action'=>'AdminPostsController#store','files'=>true]) !!}
<div class="form-group">
{!! Form::label('title','عنوان:') !!}
{!! Form::text('title',null,['class'=>'form-control']) !!}
</div>
{{--<div class="form-group">--}}
{!! Form::label('category_id','بخش:') !!}
{{--{!! Form::select('category_id',[''=>'زیر مجموعه مورد نظر را انتخاب کنید']+$categories,null,['class'=>'form-control']) !!}--}}
{{--</div>--}}
{!! Form::select('category_id',[''=>'زیر مجموعه مورد نظر را انتخاب کنید']+$categories,null,['class'=>'form-control']) !!}
<div class="form-group">
{!! Form::label('photo_id','عکس:') !!}
{!! Form::file('photo_id',['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('excerpt','خلاصه:') !!}
{!! Form::text('excerpt',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('title','متن:') !!}
{!! Form::textarea('body',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('ایجاد پست',['class'=>'btn btn-success btn-block']) !!}
</div>
{!! Form::close() !!}
#include('partcial.form-error')
</div>
</div>
<div>
<script>
$(document).ready(function() {
$('.selection').select2();
});
</script>
</div>

you can do this:
<select>
<option value=0 > زیر مجموعه مورد نظر را انتخاب کنید </option>
#foreach($categories as $category)
{
<option value={{$category->id}} > {{$category->name}} </option>
}
</select>

Related

Missing required parameters for [Route: messenger.store]

Why it gives an error of undefined id, i just changed Route::get to route::post and it says that id is undefined... should i change the way i pass it? is it a correct way of inserting data into database ?
here's the route
Route::post('messenger/store/{id}','MessengerController#store')->name('messenger.store');
View...
{!! Form::open(['method'=>'POST','action'=>['MessengerController#store',$id]]) !!}
<div class="form-group">
{!! Form::text('msg',null,['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Send Message',['class'=>'btn btn-primary'])!!}
</div>
{!! Form::close() !!}
index controller from where i pass ID
public function index($id)
{
//
$user=Auth::user();
return view('messenger.index',compact('user','id'));
}
{!! Form::open(['method'=>'POST','action'=>['MessengerController#store',$id]]) !!}
<div class="form-group">
{!! Form::text('msg',null,['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Send Message',['class'=>'btn btn-primary'])!!}
</div>
{!! Form::close() !!}
change this.
{!! Form::open(['method'=>'POST','url'=>route('messenger.store',[$id])]) !!}
<div class="form-group">
{!! Form::text('msg',null,['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Send Message',['class'=>'btn btn-primary'])!!}
</div>
{!! Form::close() !!}
to this.
Using action is kind of deprecated.

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

How do I put fill out this field in Laravel?

How do I put in my form.blade the "Please fill out this field" in every attribute?
<div class="form-group">
{!! Form::label('title','Title:'); !!}
{!! Form::text('title', null,['class'=>'form-control']); !!}
</div>
<div class="form-group">
{!! Form::label('description','Description:'); !!}
{!! Form::textarea('description', null,['class'=>'form-control']); !!}
</div>
<div class="form-group">
{!! Form::label('stock','Stock:'); !!}
{!! Form::text('stock', null,['class'=>'form-control']); !!}
</div>
<div class="form-group">
{!! Form::label('category_id','Category ID:'); !!}
{!! Form::select('category_id', $categories, null,['class'=>'form-control', 'placeholder'=>'Choose a Category']); !!}
</div>
{!! Form::submit('Save',['class'=>'btn btn-success']); !!}
Try this
<div class="form-group">
{!! Form::label('title','Title:'); !!}
{!! Form::text('title', null,['class'=>'form-control','required']); !!}
</div>
<div class="form-group">
{!! Form::label('description','Description:'); !!}
{!! Form::textarea('description', null,['class'=>'form-control','required]); !!}
</div>
<div class="form-group">
{!! Form::label('stock','Stock:'); !!}
{!! Form::text('stock', null,['class'=>'form-control','required']); !!}
</div>
<div class="form-group">
{!! Form::label('category_id','Category ID:'); !!}
{!! Form::select('category_id', $categories, null,['class'=>'form-control', 'placeholder'=>'Choose a Category','required']); !!}
</div>
{!! Form::submit('Save',['class'=>'btn btn-success']); !!}

laravel collective html form model is giving a weird case of losing the styles and layouts

i have a weird case where when i change the form::open to form::model and add $user, the page layouts are gone. by this i mean if i use below it works fine and all the layouts are perfectly there.
{!! Form::open( ['method'=>'PATCH', 'action'=>['AdminUsersController#update', $user->id], 'files'=>true , 'class'=>'form-horizontal']) !!}
but when i try to get the user details on the edit page with below code, all my layouts and styles on the edit page disappears.
{!! Form::model($user, ['method'=>'PATCH', 'action'=>['AdminUsersController#update', $user->id], 'files'=>true , 'class'=>'form-horizontal']) !!}
chrome doesnt detect any error of not able to get css files. its like laravel drops drops all of it. Below is my full code in edit page.
{!! Form::model($user, ['method'=>'PATCH', 'action'=>['AdminUsersController#update', $user->id], 'files'=>true , 'class'=>'form-horizontal']) !!}
<div class="form-group">
{!! Form::Label('name', 'Name:', ['class'=>'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::text('name', null, ['class'=>'form-control','placeholder'=>'Full Name']) !!}
</div>
</div>
<div class="form-group">
{!! Form::Label('email', 'Email:', ['class'=>'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::email('email', null, ['class'=>'form-control','placeholder'=>'user#email.com']) !!}
</div>
</div>
<div class="form-group">
{!! Form::Label('password', 'Password:', ['class'=>'col-sm-3 control-label', 'for'=>'password']) !!}
<div class="col-sm-9 strength-container">
{!! Form::password('password', ['class'=>'password-strength-example1 form-control', 'id'=>'password', 'data-plugin'=>'strength']) !!}
</div>
</div>
<div class="form-group">
{!! Form::Label('is_active', 'Active:', ['class'=>'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::hidden('is_active', 0) !!}
{!! Form::checkbox('is_active', 1, null, ['data-plugin'=>'switchery']) !!}
</div>
</div>
<div class="form-group">
{!! Form::Label('role_id', 'Role:', ['class'=>'col-sm-3 control-label']) !!}
<div class="col-sm-9">
{!! Form::select('role_id', $roles ,null, ['class'=>'form-control']) !!}
</div>
</div>
<div class="form-group form-material">
{!! Form::Label('photo_id', 'Photo:', ['class'=>'col-sm-3 control-label', 'for'=>'photo_id']) !!}
<div class="col-sm-9">
{!! Form::text('', null, ['class'=>'form-control', 'placeholder'=>'Browse..', 'readonly'=>'']) !!}
{!! Form::file('photo_id', null, ['multiple'=>'']) !!}
</div>
</div>
<div class="form-group">
<div class="col-sm-9 col-sm-offset-3">
{!! Form::submit('Submit', ['class'=>'btn btn-primary']) !!}
{!! Form::reset('Reset', ['class'=>'btn btn-danger']) !!}
</div>
</div>
{!! Form::close() !!}
and for reference. both images to see difference
Try
{!! Form::file('photo_id', ['multiple'=>'']) !!}
for file field since.

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