1

I am new to laravel so please guide me, My problem is, I need to store a string and text array in my database in laravel, tho I cannot pass anything inside the database using arrays.. can anyone help me out here please thanks.

Here is my code in View and Controller

My code in View

   {!! Form::open(['action'=>'Admin\AboutusController@store', 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="form-group">   
    <div class="table-responsive">  
        <table class="table table-bordered" id="dynamic_field">  
           <tr>  
              <td>  {{Form::text('title[]', '', ['class' => 'form-control', 'placeholder' => 'Enter a Title'])}}<br>
                    {{Form::textarea('description[]', '', ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br>

                          {{ Form::file('about_image[]') }}


              </td>
              <td>{{ Form::button('', ['class' => 'btn btn-success fa fa-plus-circle', 'id'=>'add','name'=>'add', 'style'=>'font-size:15px;']) }}</td>
           </tr>  
        </table>  
        {{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
    </div> 
</div>  
{!! Form::close() !!}

There you can see my textbox, textarea and submit button

My Controller

 $this->validate($request, [
        'title' => 'required',
        'description' => 'required',
        'about_image' => 'required'
    ]);

    if ($request->has('about_image'))
    {   
        //Handle File Upload

        $about = [];
        foreach ($request->file('about_image') as $key => $file)
        {
            // Get FileName
            $filenameWithExt = $file->getClientOriginalName();
            //Get just filename
            $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
            //Get just extension
            $extension = $file->getClientOriginalExtension();
            //Filename to Store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            //Upload Image
            $path = $file->storeAs('public/about_images',$fileNameToStore);
            array_push($about, $fileNameToStore);
        }

        $fileNameToStore = serialize($about);
    }
    else
    {
        $fileNameToStore='noimage.jpg';
    }

    foreach ($about as $key => $value) {
    $aboutContent = new About;
    $aboutContent->title = $value->input('title');
    $aboutContent->description = $value->input('description');
    $aboutContent->about_image = $value;
    $aboutContent->save();
    }

1 Answer 1

1

You are trying to get information that is not available from the $about array.

try replacing it with:

foreach ($about as $key => $value) {
    $aboutContent = new About;
    $aboutContent->title = $request->title[$key];
    $aboutContent->description = $request->description[$key];
    $aboutContent->about_image = $value;
    $aboutContent->save();
}

You might just want to make sure that the title and description line up with the correct image

Sign up to request clarification or add additional context in comments.

1 Comment

You welcome Summer. We all start in the same place. when you are having trouble in the future you can use Log::info({variable}) and then you can see the data that you are trying to access in your log file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.