Laravel Controller:如何使用函数存储和创建

时间:2018-02-28 02:12:56

标签: php laravel laravel-5 laravel-4 laravel-controller

我目前是laravel的新手,我正在开发一个基本项目,服务提供商(SP)可以通过填写表单来发布服务详细信息。

Service details form

我正在尝试检查SP是否上传了精选图片。如果是,则使用

重命名文件
$featured = $request->featured;
$featured_new_name = time().$featured->getClientOriginalName();
$featured->move('uploads/services', $featured_new_name);

并将其保存到uploads/services/目录。

我的ServiceController.php看起来像这样

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

在迁移文件中:

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();

    });
}

我正在保存登录的user_id以及所有其他服务详细信息。 当用户上传徽标文件时,此代码会在数据库中存储两条记录:一条记录​​具有正确的user_id,另一条记录具有0。当用户没有上传徽标文件时,只存储一条记录,其中包含所有正确的值。

created records in view

此处user_id 2是正确的条目。请帮我改进这段代码。

2 个答案:

答案 0 :(得分:0)

当您保存第二条记录时,似乎您的会话已过期,这就是为什么第二条记录的user_id为0的原因。

在保存记录之前,您可以按照以下方式检查会话吗?

if (Auth::user()) {

// post the data and save the record

} else {

// redirect the user to the login page

}

答案 1 :(得分:0)

我认为这部分代码可以帮助您纠正错误。

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