我已经安装了通行证,并且从获取用户令牌以及注册和登录到一切正常, 我无法通过令牌在订单模型中存储订单
这是我的api路由
Route::group(['prefix' => '/v1', 'namespace' => 'Api\V1', 'as' =>
'api.'], function () {
Route::post('userLogin','UserController@UserLogin');
Route::post('userRegister','UserController@UserRegister');
Route::get('/orders/{orders}', 'OrdersController@show')- >name('orders.show');
Route::group(['middleware' => 'auth:api'], function () {
Route::post('/cities', 'CitiesController@store')- >name('CitiesController.store');
Route::post('/orders', 'OrdersController@store')- >name('OrdersController.store');
Route::get('userDetails','UserController@UserDetails');
Route::get('orders','OrdersController@index');
});
这是我的订单模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Traits\FilterByUser;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\User;
use Illuminate\Support\Facades\Auth;
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
/**
* Class Order
*
* @package App
* @property integer $order_no
* @property string $user
* @property string $vendor
* @property string $status
* @property string $details
* @property string $service_date
* @property decimal $price
* @property string $created_by
*/
class Order extends Model
{
use SoftDeletes, FilterByUser , HasApiTokens;
protected $fillable = ['order_no', 'details', 'service_date', 'price', 'user_id', 'vendor_id', 'status_id', 'created_by_id'];
protected $hidden = [];
//Saad Edited here on april 30
//added random order number
public static function boot()
{
parent::boot();
static::creating(function($model){
$random_code = null;
while($random_code === null){
$tmp_random_code = rand(100000, 999999);
if(!self::where('order_no', $tmp_random_code)->count()){
$random_code = $tmp_random_code;
}
}
$model->order_no = $random_code;
});
}
/**
* Set attribute to money format
* @param $input
*/
public function setOrderNoAttribute($input)
{
$this->attributes['order_no'] = $input ? $input : null;
}
/**
* Set to null if empty
* @param $input
*/
public function setStatusIdAttribute($input)
{
$this->attributes['status_id'] = $input ? $input : null;
}
/**
* Set attribute to date format
* @param $input
*/
public function setServiceDateAttribute($input)
{
if ($input != null && $input != '') {
$this->attributes['service_date'] = Carbon::createFromFormat(config('app.date_format'), $input)->format('Y-m-d');
} else {
$this->attributes['service_date'] = null;
}
}
/**
* Get attribute from date format
* @param $input
*
* @return string
*/
public function getServiceDateAttribute($input)
{
$zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));
if ($input != $zeroDate && $input != null) {
return Carbon::createFromFormat('Y-m-d', $input)->format(config('app.date_format'));
} else {
return '';
}
}
/**
* Set attribute to money format
* @param $input
*/
public function setPriceAttribute($input)
{
$this->attributes['price'] = $input ? $input : null;
}
/**
* Set to null if empty
* @param $input
*/
public function setCreatedByIdAttribute($input)
{
$this->attributes['created_by_id'] = $input ? $input : null;
}
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function vendor()
{
return $this->belongsTo(User::class, 'vendor_id');
}
public function status()
{
return $this->belongsTo(Status::class, 'status_id')->withTrashed();
}
public function created_by()
{
return $this->belongsTo(User::class, 'created_by_id');
}
}
这是我在ordercontroller中的存储功能
public function store(StoreOrdersRequest $request)
{
$user = Auth::user();
$order = Order::create($request->all());
$order->user_id=$user->id;
$order->save();
return response()->json($order);
}
我希望我的post方法受auth:api保护,并在订单模型中存储与user_id中的令牌匹配的用户ID,并成功存储新订单