请问我错过了什么,使用Laravel Framework

时间:2018-04-19 14:33:43

标签: laravel

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

物业模型

//I have set the fillable field
public function propertyPhotos()
{
    return $this->hasMany(PropertyPhoto::class, 'property_id');
}

PropertyPhoto模型

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;
}

}

和查看页面

 {!! 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'] ) !!}
                        <a href="{{ URL::previous() }}" class="btn btn-inverse">Go Back</a>
                    </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');
    });
}

问题是:当我单击提交按钮时,记录将被插入到属性表中,并且没有任何记录插入到propertyphotos表中,也没有为property_images创建任何路径,并且最差是没有错误信息。

另外,在我的{!! Form::open() !!}我添加了'files' => true 我确实把它改成了'encytype&#39; =&GT; &#39;多部分/格式数据&#39;没有运气。

拜托,我错过了什么?有没有更好的方法来实现我想要做的事情?任何建议都将受到高度赞赏。谢谢。

2 个答案:

答案 0 :(得分:0)

请确保您使用的是enctype= "multipart/form-data" within form tag

答案 1 :(得分:0)

  1. 审核您的内容
  2. 您有一个表单,您可以在其中添加属性和多张照片。知道你的html是什么样子会非常有帮助,但我只是假设你输入的文件是你的......并且你已经添加了一个带有csrf令牌的隐藏输入,你也在后面做了一些验证请求确保始终发送文件。我也会假设你不要这样使用ajax(这会改变一些行为)

    在Property模型中,你有一个名为property_photos的PropertyPhoto的reltionshop(应该是propertyPhotos)并且是hasMany

    在PropertyPhoto中,你与Property模型有关系,它叫做属性(shoudld被称为属性,因为它属于)

    在PropertyController中,您有一个名为store的方法,您希望存储所有这些内容。这就是你应该这样做的方式:

    1. 在Property和PropertyPhoto模型中,将要保存的列添加到fillable中,如下所示 在财产:

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

      在PropertyPhotos

      protected $fillable = ['property_id', 'filename'];
      

      这不是强制性的,但会使代码看起来更清晰。你可以跳过这个

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

      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');
      }
      
      PropertyImages

      中的

      这是一个用于获取图像路径的访问器

           public function getFilenameAttribute()
              {
      // edit here: since filename should include that stuff. Image should be in storage/propertyImages/propertyId
                  return 'storage/' . $this->filename;
              }
      

      最后php artisan storge:link

      您应该将所有文件保存在存储中。稍后,如果您使用AWS的GCloud,您可以使用S3或Bucket以较低的成本存储文件

      要访问图像$propertyImage->filename; //就像那样