我正在访问用户表属性'role',并想检查角色是否为2然后显示仪表板但是收到此错误.这是我的代码
protected $casts = [
'role' => 'integer',
];
Run Code Online (Sandbox Code Playgroud)
这是我的控制器功能,我在其中访问用户角色列值.它返回数组中的值,但我想将它与整数值'2'进行比较.
public function postSignIn(Request $request)
{
$this->validate($request,[
'email' => 'required',
'password' => 'required',
]);
$posts = Post::all();
$email = $request['email'];
$user = User::where("email",$email)->get(['role']);
if(Auth:: attempt(['email' => $request['email'] , 'password' => $request['password']]))
{
if ($user == 2) {
return view('admin.dashboard');
}
else {
return view('frontend.layouts.user_login_layout', compact('posts'));
}
}else{
return "wrong User";
}
}
Run Code Online (Sandbox Code Playgroud) 换句话说,可以说我有这样的查询:
return Location::valid()
->with('owners', 'gardeners')
->withCount('gardeners')
->get();
Run Code Online (Sandbox Code Playgroud)
返回一些json:
[
{
name: 'LocationA',
nb_gardeners_max: 3,
owners: [...],
garderners: [...],
garderners_count: 2
}
]
Run Code Online (Sandbox Code Playgroud)
在json响应中,我想添加一些自定义条目,例如is_full和available_places:
[
{
name: 'LocationA',
nb_gardeners_max: 3,
owners: [...],
garderners: [...],
garderners_count: 2,
...
is_full: false,
available_places: 1
}
]
Run Code Online (Sandbox Code Playgroud)
我想这与原始/联接/聚合...有关,但我真的不知道该怎么做。任何帮助将不胜感激
编辑
Minhajul的答案确实很有用,但不可能对附加属性执行WHERE子句。
Location::where('is_full',true)->get() // NOT WORKING
Run Code Online (Sandbox Code Playgroud)
尽管仍然可以对它执行filter(),但我想为此使用JOIN来执行单个查询
我正在使用Laravel 5.4,如何对两列进行两次排序?
例如:
ArticleController.php
public function index()
{
$articles = Auth::user()->articles->sortByDesc('updated_at')->sortByDesc('status');
return view('index', compact('articles'));
}
Run Code Online (Sandbox Code Playgroud)
我使用了sortByDesc()两次,但结果不符合规定,我该怎么办?
更新:
User.php
public function articles()
{
return $this->hasMany('App\Article');
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Laravel 5.4.
我将软删除添加到用户模型.
喜欢use SoftDeletes和$dates = ['deleted_at']
当我使用User::destroy(1)它时实际上会在数据库中添加deleted_at值,所以我确定没有关于SoftDeletes的设置问题.
但是当我获得用户列表并使用用户模型登录时,它不会过滤删除的行.
有没有人有同样的问题?请告诉我你是如何解决这个问题的.
--Update--
应用程序\ user.php的
<?php
namespace App;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Laravel\Scout\Searchable;
class User extends Authenticatable
{
/**
* Added for passport - api login checking
*/
use HasApiTokens, Notifiable, Searchable;
use SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that …Run Code Online (Sandbox Code Playgroud) 当我试图让记录有相同created_at和updated_at类似:
->where('created_at','=','updated_at')
Run Code Online (Sandbox Code Playgroud)
哪里created_at和updated_at都一样.我一无所获.有什么想法解决这个问题吗?
我有一个运行Laravel 5.3的应用程序.
我需要对表中的所有记录进行修改,这是我的代码:
Car::all()->each(function($car){
$car->created_by = 10;
$car->updated_by = 15;
$car->save();
});
Run Code Online (Sandbox Code Playgroud)
问题是created_by字段正在正确保存但updated_by没有.即使我dump($car)以前使用save()行,我也可以看到两个字段都已被修改.我还添加了两个字段$fillable(我知道,这是用于大规模分配,但以防万一..).
我试图通过使用"使用"给了别名,模型类的名称使用在laravel雄辩的方法相关模型的类名.例如,我使用过UserProfile模型类:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\UserProfile;
Run Code Online (Sandbox Code Playgroud)
而现在我正在以雄辩的方式使用它,如下所示:
public function profileDetails() {
return $this->hasOne('UserProfile', 'user_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)
但这会引发一个错误.Class 'UserProfile' not found
如果我直接在这个雄辩的第一个参数中传递相关模型的名称和路径,那么它的工作正常
public function profileDetails() {
return $this->hasOne('App\Models\UserProfile', 'user_id', 'id');
}
Run Code Online (Sandbox Code Playgroud)
我想知道为什么它没有合作 use
我有两张相关的桌子.
| id | reon_id | 名字|
| id | settlement_id | first_name | last_name | cipher_id |
在结算模型中他们的相关如下:
public function members()
{
return $this->hasMany('App\AddMember');
}
Run Code Online (Sandbox Code Playgroud)
所以,首先我需要reon_id为1的所有成员,所以我喜欢这样:
$members = Settlement::where('reon_id', '1')
->with('members')
->count();
Run Code Online (Sandbox Code Playgroud)
这很有效.但是现在,我需要同样的东西,但也需要cipher_id为1.如果我在 - > with('members')之后添加 - > where('cipher_id','1')我得到这个错误:
(2/2)QueryException SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'cipher_id'
哪里错了?
大家好我是Laravel开发的新手,我想知道如何在两个表之间创建子查询,例如,我想执行这个查询:
SELECT * FROM `contracts`
WHERE `trainer_id` = '1' OR id IN (
SELECT `contract_id` FROM `trainees`
WHERE `user_id` = '1'
)
Run Code Online (Sandbox Code Playgroud)
我测试它并且它可以正常工作,我想知道如何在Laravel雄辩中写出它
我正在Laravel 5.5中构建一个Eloquent查询.我想知道的是数据库中是否存在一行 - 我想要包含任何软删除的行:
$query = Thing::where([
'date' => $date,
'user_id' => $userId
]);
return $query->exists();
Run Code Online (Sandbox Code Playgroud)
我的问题是,这是忽略软删除的行,因此false如果带有给定date和user_id存在的软删除行,它将返回.是否有一种简洁的方法来包含软删除的行?