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

问题描述 投票:0回答:1

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

我正在尝试检查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。当用户未上载徽标文件时,只存储一条记录,其中包含所有正确的值。

这里user_id 2是一个正确的条目。请帮我改进这段代码。

php laravel laravel-5 laravel-4 laravel-controller
1个回答
0
投票

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

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

if (Auth::user()) {

// post the data and save the record

} else {

// redirect the user to the login page

}
© www.soinside.com 2019 - 2024. All rights reserved.