[英]Laravel 5.2 Retrieve Authenticated User ID
I having a small issue with Laravel 5.2 我在Laravel 5.2中有一个小问题
I am trying to retrieve authenticated user id and for some reason is not working. 我正在尝试检索经过身份验证的用户ID,并且由于某些原因而无法正常工作。 Every time I have 0 in my database instead of valid user id...
每当我的数据库中有0而不是有效的用户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. 一切似乎都很好,只是由于某种原因,插入后id始终为0。 I did a var_dump on Auth::user()->id and I am getting int(2)
我在Auth :: user()-> id上做了var_dump,并且我正在获取int(2)
I will really appreciate any suggestions... 我将非常感谢任何建议...
StoreClassifiedCommand.php : 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,
]);
}
}
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. 抱歉,如果我离这里远了。
Problem has been solved, thanks to @lagbox. 由于@lagbox,问题已解决。
I haven't set up $owner_id as fallible in my model. 我没有在模型中将$ owner_id设置为容易出错的。 I considerate this topic as closed.
我认为这个话题已经结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.