草庐IT

eloquent

全部标签

php - laravel中抽象Model类如何处理 "::find()"、 "::where()"等静态动态方法调用

我正在学习如何在laravel中实现一些功能,因为我想了解一些软件设计技术和原则。我理解当在抽象模型类实现魔术方法的Eloquent模型上进行诸如“App\User::find(1)或App\User::whereId(1)”之类的静态方法调用时__callStatic"像这样:/***Handledynamicstaticmethodcallsintothemethod.**@paramstring$method*@paramarray$parameters*@returnmixed*/publicstaticfunction__callStatic($method,$paramet

php - 未定义的方法 File::save() (Laravel 模型)

我正在使用Laravel构建一个新的Web项目。我正在使用Eloquent(它的ORM)来做所有与数据库相关的事情。我有一个包含两个表的SQLite数据库:“图像”和"file"。因此,我有两个模型:“Image.php”(类Image扩展了Eloquent)和“File.php”(类File扩展了Eloquent)。根据文档,我做对了。我尝试使用Image模型并且效果很好。模型的典型使用示例:$image=newImage;$image->val1=$val1;$image->val2=$val2;$image->save();但是,由于某种我不知道的原因,文件模型没有按预期工作。我

php - Illuminate\Database\Eloquent\Model::setAttribute() Laravel 4.1 缺少参数 2

更新:当我在Laravel4中工作时,我的注销操作有问题,但在Laravel4.1中我有这个错误:Missingargument2forIlluminate\Database\Eloquent\Model::setAttribute(),calledinC:\Users\mohammed\workspace\mylittlebiz\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.phponline2432anddefined这是我的Action:publicfunctiondoLogout(){Auth::

php - 左连接在 Laravel 中获得一行

我一直在尝试左连接并获取所需数据,但没有成功这是我的代码:$album=Albums::->where('users_id',$user_id)->leftJoin('photos',function($query){$query->on('photos.albums_id','=','albums.id');$query->where('photos.status','=',1);//$query->limit(1);//$query->min('photos.created_at');})->where('albums.status',1)->get();评论是我的一些尝试...我

php - Laravel 将 JSON 转换为数组?

我正在尝试将JSON字段转换为数组。例如模型是这样的:protected$casts=['content'=>'array'];在插入内容时,我是这样做的:'content'=>json_encode(['description'=>$faker->paragraph(3),'about'=>$faker->paragraph(2),'info'=>$faker->paragraph(2),'updated'=>$faker->dateTimeBetween('-1years','now')]),但是在获取数据时它打印一个字符串,没有别的。这部分的迁移看起来是这样的:$campaign

php - 方法或位置不存在。拉维尔 5.3

Macroable.php第74行中的BadMethodCallException:方法orWhere不存在。$category=$categories->where('Node_ID',(explode('.',$cat{$title_id})[0]))->orWhere('Node_Path',$cat->{$category_name})->first();如果我在没有“orWhere”的情况下尝试工作,如果我使用它,则会抛出错误。有人知道哪里错了吗? 最佳答案 您正在尝试对集合使用orWhere,这就是它向您显示错误的原因。

php - 预加载 : Use `with` on pivot with eloquent relationship

有4个表:bundles:id,nameproducts:id,name价格:id,namebundle_product:id,bundle_id,product_id,price_id有3种模式:bundle产品价格Product在Bundle中时有一个Price。我想要所有bundles及其相关的products和相关的price。我可以得到所有的bundles及其产品和价格ID://IcreatedaBundleModelwithaproductsmethodclassBundleextendsModel{publicfunctionproducts(){return$this-

php - Laravel 4 验证

我使用以下规则来验证创建新用户:protected$rules=['name'=>'required','email'=>['required','unique:user','email']];更新现有用户时,我使用与上面所示相同的规则集但如果用户根本没有更改他的电子邮件,则不希望出现验证错误。我目前使用以下方法解决此问题:if(!User::changed('email')){unset($user->email);}我觉得这是一个肮脏的解决方法,所以我想知道是否有更好的选择。另请注意,changed方法是我自己编写的。有谁知道有没有是用于检查模型属性是否已更改的原生Laravel4

php - 如何在 Laravel Eloquent 中插入具有多个 belongsTo 关系的记录

我正在尝试以Eloquent方式插入记录。在基本情况下很清楚。例如,如果我有一个包含帖子的博客,并且每个帖子都属于一个用户,我可以这样做:$post=newPost;$post->content='Postcontent';$user->posts()->save($post);但是如果我的帖子同时属于用户、类别和组,我应该怎么做呢?我的第一个想法是这样做:$post=newPost;$post->content='Postcontent';$user->posts()->save($post);$group->posts()->associate($post);$cat->posts

php - Laravel - Eloquent 在比较之前将查询参数转换为整数

我正在尝试根据主键从表中返回一行。$product=Product::where('id','=',$idOrSKU)->orWhere('sku','=',$idOrSKU)->take(1)->get();由于某些原因,$idorSKU在比较发生之前被转换为和(int)。例如,当$isOrSKU="9dfghfd"时,返回ID=9的行。为什么是这样?它应该什么都不返回!谁能解释一下?这里是相关的表方案|id|int(10)unsigned|NO|PRI|NULL|name|varchar(255)|NO||NULL|sku|varchar(255)|NO||NULL