如果我尝试声明属性,像这样:
public $quantity = 9;
Run Code Online (Sandbox Code Playgroud)
...它不起作用,因为它不被视为"属性",而仅仅是模型类的属性.不仅如此,我还阻止访问实际存在的"数量"属性.
那我该怎么办?
我正在使用Laravel 5.0开发web apis,但我对查询有疑问.我的课程是:
class Event extends Model {
protected $table = 'events';
public $timestamps = false;
public function participants()
{
return $this->hasMany('App\Participant', 'IDEvent', 'ID');
}
public function owner()
{
return $this->hasOne('App\User', 'ID', 'IDOwner');
}
}
Run Code Online (Sandbox Code Playgroud)
和
class Participant extends Model {
protected $table = 'participants';
public $timestamps = false;
public function user()
{
return $this->belongTo('App\User', 'IDUser', 'ID');
}
public function event()
{
return $this->belongTo('App\Event', 'IDEvent', 'ID');
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我希望用特定的partecipant获取所有事件.我尝试过:
Event::with('participants')->where('IDUser', 1)->get();
Run Code Online (Sandbox Code Playgroud)
但where条件适用于Event的类而不是Partecipants.这给了我一个例外:
Participant::where('IDUser', 1)->event()->get();
Run Code Online (Sandbox Code Playgroud)
我知道我可以这样写:
$list = Participant::where('IDUser', 1)->get();
for($item …Run Code Online (Sandbox Code Playgroud) 我一直在尝试创建一个简单的用户管理系统,但在查询关系时不断遇到障碍.例如,我有用户和角色,每当我尝试对所有用户及其角色进行查询时,我都会收到错误消息.标题中的那个只是我遇到的最新版本.
我的用户和角色模型如下所示:
class Role extends Model
{
public function users()
{
$this->belongsToMany('\App\User', 'fk_role_user', 'role_id', 'user_id');
}
}
Run Code Online (Sandbox Code Playgroud)
class User extends Model
{
public function roles()
{
$this->belongsToMany('\App\Role', 'fk_user_role', 'user_id', 'role_id');
}
}
Run Code Online (Sandbox Code Playgroud)
我的迁移表中两者之间的多对多关系如下所示:
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable(); //fk => users
$table->integer('role_id')->unsigned()->nullable(); //fk => roles
$table->foreign('fk_user_role')->references('id')->on('users')->onDelete('cascade');
$table->foreign('fk_role_user')->references('id')->on('roles')->onDelete('cascade');
});
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试在控制器中获取他们关系的所有记录:
public function index()
{
$users = User::with('roles')->get();
return $users;
}
Run Code Online (Sandbox Code Playgroud)
所以我需要另一双眼睛告诉我这里我缺少什么?
我有一个关于内部联接的问题,有多个值.我在laravel中构建了这样的代码.
public function scopeShops($query) {
return $query->join('kg_shops', function($join)
{
$join->on('kg_shops.id', '=', 'kg_feeds.shop_id');
// $join->on('kg_shops.active', '=', "1"); // WRONG
// EDITED ON 28-04-2014
$join->on('kg_shops.active', '=', DB::raw("1"));
});
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,它给出了这个结果:
Column not found: 1054 Unknown column '1' in 'on clause' (SQL: select `kg_feeds`.* from `kg_feeds` inner join `kg_shops` on `kg_shops`.`id` = `kg_
feeds`.`shop_id` and `kg_shops`.`active` = `1`) (Bindings: array ( ))
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,连接中的多个条件都可以,但它认为它1是一列而不是字符串.这甚至可能,或者我必须在哪里修复它.
提前致谢!
我正在努力获得最受欢迎的黑客马拉松,需要各自的黑客马拉松订购partipants->count().对不起,如果这有点难以理解.
我有一个具有以下格式的数据库:
hackathons
id
name
...
hackathon_user
hackathon_id
user_id
users
id
name
Run Code Online (Sandbox Code Playgroud)
该Hackathon模型是:
class Hackathon extends \Eloquent {
protected $fillable = ['name', 'begins', 'ends', 'description'];
protected $table = 'hackathons';
public function owner()
{
return $this->belongsToMany('User', 'hackathon_owner');
}
public function participants()
{
return $this->belongsToMany('User');
}
public function type()
{
return $this->belongsToMany('Type');
}
}
Run Code Online (Sandbox Code Playgroud)
并HackathonParticipant定义为:
class HackathonParticipant extends \Eloquent {
protected $fillable = ['hackathon_id', 'user_id'];
protected $table = 'hackathon_user';
public function user()
{
return $this->belongsTo('User', 'user_id'); …Run Code Online (Sandbox Code Playgroud) 可能不是特定于Eloquent集合的问题,但它只是在与他们合作时打击了我.让我们假设我们有一个$collection对象是一个实例Illuminate\Support\Collection.
现在,如果我们想要迭代它,使用each()闭包与常规的优缺点是什么foreach.有没有?
foreach ($collection as $item) {
// Some code
}
--- VS ---
$collection->each(function ($item) {
// Some code
});
Run Code Online (Sandbox Code Playgroud) 当我通过Laravel的Eloquent ORM执行查询时,我希望将行ID作为数组结果键,当我想根据ID检查某些内容(使用array_key_exists)或做一些查找时(如果我需要结果数组中的特定条目)
有什么办法我可以告诉Eloquent设置字段ID的密钥吗? 见图像http://oi60.tinypic.com/kc02a8.jpg
这是我的
$art = Article::where('id',$article)->firstOrFail();
$products = $art->products;
Run Code Online (Sandbox Code Playgroud)
我只是想限制'产品'这是错误的方式
$products = $art->products->offset($offset*$limit)->take($limit)->get();
Run Code Online (Sandbox Code Playgroud)
请帮我一把!
谢谢!
我使用关系的条件/约束创建了一个模型游戏,如下所示:
class Game extends Eloquent {
// many more stuff here
// relation without any constraints ...works fine
public function videos() {
return $this->hasMany('Video');
}
// results in a "problem", se examples below
public function available_videos() {
return $this->hasMany('Video')->where('available','=', 1);
}
}
Run Code Online (Sandbox Code Playgroud)
什么时候使用它像这样:
$game = Game::with('available_videos')->find(1);
$game->available_videos->count();
Run Code Online (Sandbox Code Playgroud)
一切正常,因为角色是最终的集合.
我的问题:
当我试图访问它而没有急切加载
$game = Game::find(1);
$game->available_videos->count();
Run Code Online (Sandbox Code Playgroud)
抛出异常,因为它表示" 在非对象上调用成员函数count() ".
运用
$game = Game::find(1);
$game->load('available_videos');
$game->available_videos->count();
Run Code Online (Sandbox Code Playgroud)
工作正常,但对我来说似乎很复杂,因为我不需要加载相关的模型,如果我不在我的关系中使用条件.
我错过了什么吗?我怎样才能确保可以在不使用预先加载的情况下访问available_videos?
对于任何有兴趣的人,我也在http://forums.laravel.io/viewtopic.php?id=10470上发布了这个问题.
我知道在使用查询构建器时,可以使用多列进行排序
...orderBy('column1')->orderBy('column2')
Run Code Online (Sandbox Code Playgroud)
但现在我正在处理一个集合对象.集合有sortBy方法,但我无法弄清楚如何使它适用于多列.直觉上,我最初尝试使用相同的语法orderBy.
sortBy('column1')->sortBy('column2)
Run Code Online (Sandbox Code Playgroud)
但这显然只是按顺序应用排序,最终按列2排序,忽略column1.我试过了
sortBy('column1', 'column2')
Run Code Online (Sandbox Code Playgroud)
但是抛出错误"asort()期望参数2为long,字符串给出".运用
sortBy('column1, column2')
Run Code Online (Sandbox Code Playgroud)
不会抛出错误,但排序看起来很随机,所以我真的不知道实际上是做什么的.我查看了sortBy方法的代码,但不幸的是我很难理解它是如何工作的.