dpfz27768 2016-02-26 00:23
浏览 63

Laravel 5.2检索经过身份验证的用户ID

I having a small issue with Laravel 5.2

I am trying to retrieve authenticated user id and for some reason is not working. Every time I have 0 in my database instead of valid user id...

In my controller I have Use Auth, on top included.

My store method looks like that:

public function store(StoreClassifiedRequest $request)
{
    $title = $request->input('title');
    $category_id = $request->input('category_id');
    $description = $request->input('description');
    $price = $request->input('price');
    $condition = $request->input('condition');
    $main_image = $request->file('main_image');
    $location = $request->input('location');
    $email = $request->input('email');
    $phone = $request->input('phone');
    $owner_id = Auth::user()->id;

    ....

    // Create command
    $command = new StoreClassifiedCommand($title, $category_id, $description, $main_image_filename, $price, $condition, $location, $email, $phone, $owner_id);
    $this->dispatch($command);

    return \Redirect::route('classifieds.index')->with('message', 'Listing Created');
  }

All seems to be fine, just the id is always 0 for some reason after insert. I did a var_dump on Auth::user()->id and I am getting int(2)

I will really appreciate any suggestions...

StoreClassifiedCommand.php :

<?php

namespace App\Commands;

use App\Commands\Command;
use Illuminate\Contracts\Bus\SelfHandling;
use App\Classified;

class StoreClassifiedCommand extends Command implements SelfHandling {
  public $title;
  public $category_id;
  public $description;
  public $main_image;
  public $price;
  public $condition;
  public $location;
  public $email;
  public $phone;
  public $owner_id;

  // Create a new command instance

  public function __construct($title, $category_id, $description, $main_image, $price, $condition, $location, $email, $phone, $owner_id)
  {
    $this->title = $title;
    $this->category_id = $category_id;
    $this->description = $description;
    $this->main_image = $main_image;
    $this->price = $price;
    $this->condition = $condition;
    $this->location = $location;
    $this->email = $email;
    $this->phone = $phone;
    $this->owner_id = $owner_id;
  }

  //Execute the command.

  public function handle()
  {
    return Classified::create([
      'title' => $this->title,
      'category_id' => $this->category_id,
      'description' => $this->description,
      'main_image' => $this->main_image,
      'price' => $this->price,
      'condition' => $this->condition,
      'location' => $this->location,
      'email' => $this->email,
      'phone' =>$this->phone,
      'owner_id' =>$this->owner_id,
    ]);
  }
}
  • 写回答

2条回答 默认 最新

  • dongweihuai5601 2016-02-26 04:31
    关注

    Try

    $request->user()->id;
    

    i'm not familiar with the code you've presented but this is a guess.

    Also use these.

    use Illuminate\Http\Request;
    use App\Http\Requests;
    

    in order to use the request object.

    Sorry if i'm way off the ball here.

    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计