我正在以 UTC 格式保存所有日期时间数据(created_at、updated_at 和自定义日期时间列:appointment_at)。
我想检索所有这些日期,并将它们转换为用户的时区以进行显示。
我已将这些函数添加到我的模型中,认为这将检索用户时区中的日期时间:
public function getCreatedAtAttribute($value)
{
return $created_at->timezone(Auth::user()->timezone);
}
public function getUpdatedAtAttribute($value)
{
return $updated_at->timezone(Auth::user()->timezone);
}
public function getAppointmentAtAttribute($value)
{
return $appointment_at->timezone(Auth::user()->timezone);
}
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
Call to a member function timezone() on a non-object
Run Code Online (Sandbox Code Playgroud)
我猜我的语法有错误。我错过了什么吗?谢谢
更新 #1 对 Note Model 进行了更正,并在下面添加了完整的模型(包括 @bernie 的更正)
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Note extends Model {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'notes';
/**
* The attributes that are …Run Code Online (Sandbox Code Playgroud) 我喜欢使用该dd函数进行调试.这次当我用它来显示约会列表时,我看不到(没有可点击的箭头)属性和原始数据.我得到[ …19]显示的括号,不知道为什么.
Collection {#3061 ?
#items: array:548 [?
0 => Appointment {#821 ?
#table: "appointments"
#fillable: array:16 [ …16]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:19 [ …19]
#original: array:19 [ …19]
#relations: array:2 [ …2]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [ …1]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
1 => Appointment {#822 ?}
2 …Run Code Online (Sandbox Code Playgroud) 在条纹测试中遇到这个问题。过去一切都在测试中工作,但是当我在 Stripe 中创建一个新计划并删除原始计划时,我现在收到以下错误:
No such plan: monthly; one exists with a name of monthly, but its ID is primary.
Run Code Online (Sandbox Code Playgroud)
控制器
$user->newSubscription('primary', 'monthly')->create($token, [ ]);
Run Code Online (Sandbox Code Playgroud)
计划详情
ID: primary
Name: monthly
Price: $19.99 USD/month
Trial period: No trial
Run Code Online (Sandbox Code Playgroud)
php artisan config:clear 没有帮助。我使用的是 Laravel 5.2 和 Cashier 6.0。
.env 文件
STRIPE_KEY=pk_test_...
STRIPE_SECRET=sk_test_....
Run Code Online (Sandbox Code Playgroud)
配置/services.php
'stripe' => [
'model' => App\User::class,
'key' => env('STRIPE_KEY'),
'secret' => env('STRIPE_SECRET'),
],
Run Code Online (Sandbox Code Playgroud) 当用户注册时,如何将行插入多个表.
我有基本的Laravel用户表.我还有一个企业表(id,business_name)和一个usersbusinesses表(id,user_id,business_id)
首先,我需要创建(db insert)用户并获取其生成的id值.
然后,我需要创建(db insert)业务并获取其生成的id值.
最后,我需要使用上面的值(user_id和business_id)在usersbusinesses表中创建一个(db insert)行
我知道我将不得不修改app/Services/Registrar.php文件,我只是不知道如何解决这个问题.当前代码仅插入到users表:
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
public function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
Run Code Online (Sandbox Code Playgroud)
谢谢Christian
当用户登录时,我为该用户添加了创建新用户的功能(指定电子邮件地址,而不提供密码)。
完成后,我希望新用户收到一封电子邮件(如带有链接的重置密码电子邮件),将用户发送到设置密码页面(类似于重置密码视图)。
我想出了如何在商店功能(UserController)中向新用户发送常规电子邮件:
public function store(UsersRequest $request)
{
$user = User::create(Request::all());
Mail::send('users.welcomemail', [], function ($message) {
$message->from('email@example.com', 'Email');
$message->to('email@example.com', 'Email')->subject('Welcome!');
});
return redirect('business/');
}
Run Code Online (Sandbox Code Playgroud)
我为设置密码创建了一个新视图(从 views/auth/reset.blade.php 复制)。
我只是不确定我应该写什么(用于设置密码)到我的控制器,所以它就像重置密码功能一样。任何想法都会有所帮助。
如果可能,我想使用 laravel 中已经存在的内容。默认用户表和控制器,以及 password_resets 表。
更新 - 解决方案
我设法让它发挥作用。
用户控制器
public function store(UsersRequest $request)
{
$user = User::create(Request::all());
$contactfirstname = $user->first_name;
$contactemail = $user->email;
$token = hash_hmac('sha256', str_random(40), config('app.key'));
DB::table('password_resets')->insert(['email' => $user->email, 'token' => $token, 'created_at' => \Carbon\Carbon::now()->toDateTimeString()]);
Mail::send('users.welcomemail', ['user' => $user, 'token' => …Run Code Online (Sandbox Code Playgroud) 我的数据透视表中有4个额外的属性(“ product_id”,“ quantity”,“ discount_percent”,“ discount_amount”),但是当我存储这些属性时,这些属性的值始终为0,其余属性则正确填充。有任何想法吗?
发票模型
public function productversion()
{
return $this->belongsToMany('App\Productversion')->withPivot('product_id', 'quantity', 'discount_percent', 'discount_amount')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)
产品版本模型
public function invoice()
{
return $this->belongsToMany('App\Invoice')->withPivot('product_id', 'quantity', 'discount_percent', 'discount_amount')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)
控制器(商店)
$invoice->productversion()->attach($productversionid, ['product_id' => $productid], ['quantity' => $qty], ['discount_percent' => $discountprc], ['discount_amount' => $discountamt]);
Run Code Online (Sandbox Code Playgroud) 作为Laravel 5框架(和OOP)的新手,我想创建一个视图,用户可以在登录时查看/编辑自己的用户配置文件.
在控制器中获取Auth用户ID,检索用户数据(从DB)并在配置文件视图中显示该数据的语法是什么.
我尝试了以下,但不起作用.
控制器:
/**
* Show the application user profile to the user.
*
* @return Response
*/
public function profile()
{
$users = User::find(Auth::user()->id);
return view('profile')->with(['users' => $users]);
}
Run Code Online (Sandbox Code Playgroud)
档案视图:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<div class="panel panel-default">
<div class="panel-heading">Profil</div>
<div class="panel-body">
Your user id is: {{ Auth::user()->id }}.
<table>
@foreach($users as $user)
<tr>
<td>{{ $user->first_name }}</td>
<td>{{ $user->last_name }}</td>
</tr>
@endforeach
</table>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
谢谢.任何帮助将不胜感激