2

我目前是 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信息以及所有其他服务详细信息。当用户上传一个 logo 文件时,这段代码在数据库中存储了两条记录:一条是正确的user_id,另一条是0. 当用户不上传标志文件时,只存储一条记录所有正确的值。

在视图中创建记录

这里user_id2 是正确的条目。请帮助我改进此代码。

4

2 回答 2

1

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

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

if (Auth::user()) {

// post the data and save the record

} else {

// redirect the user to the login page

}
于 2018-02-28T02:29:00.067 回答
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');
    }
 }
于 2019-08-08T17:13:09.310 回答