草庐IT

eloquent

全部标签

php - 在 Laravel 项目中为评论创建 Like 系统时遇到问题

我正在使用Laravel创建一个项目。用户可以点赞评论。我想显示一个“喜欢”按钮,以便用户可以喜欢评论,如果用户已经喜欢该评论,我希望该按钮“不喜欢”,以便用户可以对喜欢的评论不喜欢在我的数据库中,我有一个likes表:|id|user_id|comment_id|我喜欢的模特是这样的:classLikeextends\Eloquent{protected$fillable=['user_id','comment_id'];protected$table='likes';publicfunctionowner(){return$this->belongsTo('Acme\Users\U

php - 新用户注册应用程序时添加表单字段

我正在尝试向Laravel5创建的users表中添加一个字段。我修改了迁移以添加该字段:classCreateUsersTableextendsMigration{publicfunctionup(){Schema::create('users',function(Blueprint$table){$table->increments('id');$table->string('name');$table->string('email')->unique();$table->string('password',60);$table->string('my_new_field');//A

php - Laravel 5 应用命名空间

我是Laravel5的新手。我在Homestead中设置并运行了原始应用程序。当我编写一个简单的UserTableSeeder并尝试使用artisan为MYSQL数据库做种时,弹出错误:“找不到类用户”。好吧,我没有更改“app/User.php”文件中的任何内容,并且在我将命名空间App添加到播种器代码之后,也就是从User::create(array(..));到App\User::create(array(..));播种工作正常。当我尝试使用时发生了同样的事情User::all();或“routes.php”中的其他Eloquent查询操作。只有当我添加命名空间App\时它才会起

php - 使用 Hashids 库对 Laravel Eloquent 集合中的 ID 进行哈希处理

我从数据库中抓取一组任务作为一个Eloquent集合,然后我将集合发送到我执行foreach的View。这里没有问题。除了,我需要在我的View中引用任务id(URL操作等)。但我显然不想在源代码中使用这个,所以我使用了thislibrary对id进行哈希处理。但是在View中这样做似乎是错误的。有什么方法可以在模型或Controller中散列id吗?下面是我在Controller中调用集合的方式:$tasks=Auth::user()->tasks()->orderBy('created_at','desc')->get();这就是我目前在我的View中散列id的方式:id))}}"

php - DB::table 与 Eloquent 模型 - Laravel 数据库种子

在研究DatabaseSeeder时,经常看到人们在Seeder类中使用DB::table('my_table')->insert(['column'=>'value']).我想知道关于为什么我应该使用DB::*而不是MyModel::*的明显约定背后的原因执行此类任务。 最佳答案 最重要的是,因为使用DB插入,您可以一次执行多个插入。尤其是在对许多大表进行播种时,这比每次插入执行一个查询要快得多。http://laravel.com/docs/master/queries#insertsDB::table('users')->in

php - Laravel 5.1 - BelongsTo 关系返回 null

\应用\用户classUserpublicfunctionstatus(){return$this->belongsTo('App\UserStatus','user_status_id','id');}\App\UserStatusclassUserStatusprotected$fillable=['id'];publicfunctionuser(){return$this->hasMany('App\User','user_status_id','id');}我已经从带有一些字段的简单User::find()查询中获得了$user对象,然后我尝试访问status对象,使用$use

php - 有没有办法在 laravel 5 中批量分配关系?

我有两个具有这样的oneToMany关系的模型类应用\汽车classCarextendsModel{public$timestamps=true;protected$fillable=['name','price'];publicfunctionparts(){return$this->hasMany('App\Part');}}应用\零件classPartextendsModel{public$timestamps=false;protected$fillable=['name','price'];publicfunctioncar(){return$this->belongsTo(

php - laravel 左连接中 undefined variable

在我的例子中,我想显示用户和每个用户完成的订单,订单有状态我从输入表单中获取$status这是我的代码$orders=DB::table('user_profiles')->leftJoin('orders',function($join){$join->on('user_profiles.id','=','orders.id_user')->where('orders.status','=',$status);})->selectRaw('user_profiles.*,count(orders.id_user)asorder_try_count')->groupBy('user_p

php - 从两个相关的 Illuminate Eloquent 模型中获取数据

目前在我们使用直接sql查询的项目之一中,决定删除它们并实现ORM,因此我们决定使用IlluminateEloquentModel组件为了那个原因。由于我们是新手,我对两个相互关联的模型之间的关系了解不多。当前的sql查询select*fromloginasA,user_profileasBwhereA.user_id=B.login_idandA.user_id=$user_id我创建了两个模型,一个User扩展Illuminate\Database\Eloquent\Model和另一个UserProfile扩展Illuminate\数据库\Eloquent\模型。模型实现是这样的用

php - 使用 DB::table() 或 Model::all() Laravel 的 Eloquent

我想知道为什么很多人使用$users=DB::table('users')->get();而不是$users=Users::all();在Laravel项目中?什么是原因?问候 最佳答案 您可以这样做,因为Model和DBfacade都实现了生成Builder实例的函数。https://laravel.com/api/5.2/Illuminate/Database/Eloquent/Model.htmlhttps://laravel.com/api/5.2/Illuminate/Database/Query/Builder.html