2

I am currently new to laravel and I am working on a basic project where a service provider (SP) is able to post service details by filling up a form.

Service details form

I am trying to check if SP has uploaded a featured image. If yes, then rename file by using

$featured = $request->featured;
$featured_new_name = time().$featured->getClientOriginalName();
$featured->move('uploads/services', $featured_new_name);

and saving it to the uploads/services/ directory.

My ServiceController.php looks like this

public function store(Request $request)
{
      $this->validate($request, [
      'name' => 'required|max:255',
      'address' => 'required|max:255',
      'city' => 'required|max:255',
      'state' => 'required|max:255',
      'zipcode' => 'required|integer|digits:5',
      'category_id' => 'required',
      'workingday' => 'required',
      'content' => 'required'
      ]);
//checking if featured image (logo file) is uploaded or not 

    if($request->hasfile('featured'))
  {
    $featured = $request->featured;
    $featured_new_name = time().$featured->getClientOriginalName();
    $featured->move('uploads/services', $featured_new_name);
  }
  else {      }

     $featured->move('uploads/services', $featured_new_name);

      $service = Service::create([
      'name' => $request->name,
      'content' => $request->content,
      'address' => $request->address,
      'city' => $request->city,
      'state' => $request->state,
      'zipcode' => $request->zipcode,
      ]);

      if($request->hasfile('featured'))
      {
        $service = Service::create([
        'name' => $request->name,
        'content' => $request->content,
        'featured' =>'uploads/services/'.$featured_new_name,
        'address' => $request->address,
        'city' => $request->city,
        'state' => $request->state,
        'zipcode' => $request->zipcode,
    ]);
 }
    $service->workingdays()->attach($request->workingday);
    $service->categories()->attach($request->category_id);
    $service->user_id=auth()->id();
    $service->save();

  Session::flash('success','Service created successfully');
  return view('sp.services.create')->with('categories', Category::all())->with('workingday', Workingday::all());
}

In the migration file:

public function up()
{
    Schema::create('services', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('address');
        $table->string('city');
        $table->string('state');
        $table->string('zipcode');
        $table->integer('routes_id');
        $table->string('featured')->default('/uploads/services/default.png');
        $table->text('content')->nullable();
        $table->string('slug');
        $table->integer('user_id');
        $table->softDeletes();
        $table->timestamps();

    });
}

I am saving the logged in user_id along with all other service details. When the user uploads a logo file, this code stores two records in the database: one with the correct user_id and one with 0. When the user does not upload the logo file, only one record is stored with all correct values.

created records in view

Here user_id 2 is a correct entry. Please help me improve this code.

iled
  • 2,142
  • 3
  • 31
  • 43
mkirr
  • 81
  • 2
  • 5
  • 15
  • You're overwriting your `$service` object when an image is uploaded. – fubar Feb 28 '18 at 02:47
  • Why are you creating two `Service` models for one form? The attributes are identical, with the exception of `featured`. – fubar Feb 28 '18 at 03:01

2 Answers2

1

Seems your session expired, when you save the second record, that's why the user_id is 0 for your second record.

Can you check your session as follows before you save the record ?

if (Auth::user()) {

// post the data and save the record

} else {

// redirect the user to the login page

}
  • I already tried this. Couldn't solve the problem. Also when I don't upload logo file only one record is stored (with correct user_id). Thanks. – mkirr Feb 28 '18 at 02:43
0

I think this portion of code help you to fix out the error.

class DocumentController extends Controller
{
   public function store(Request $request)
   {
       $images = $request->file('files');
       if($request->hasFile('files')) :
          foreach ($images as $item):
            $var = date_create();
            $time = date_format($var, 'YmdHis');
            $imageName = $time . '-' . $item->getClientOriginalName();
            $item->move(base_path() . '/uploads/file/', $imageName);
            $arr[] = $imageName;
          endforeach;
          $image = implode(",", $arr);
       else:
          $image = '';
       endif;

      DB::table('document')->insert(array('id' => $id,'image' => $image));

      Session::flash('message', 'Image are uploaded successfully');
      return redirect('/addimage');
    }
 }
rashedcs
  • 3,588
  • 2
  • 39
  • 40