相关疑难解决方法(0)

Laravel Eager Loading - 仅加载特定列

我试图在laravel中加载一个模型,但只返回某些列.我不希望呈现整个急切的加载表.

public function car()
{
    return $this->hasOne('Car', 'id')->get(['emailid','name']);
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

log.ERROR:异常'Symfony\Component\Debug\Exception\FatalErrorException',带有消息'调用未定义的方法Illuminate\Database\Eloquent\Collection :: getAndResetWheres()'

php laravel laravel-4

32
推荐指数
5
解决办法
2万
查看次数

Laravel includesToMany只返回特定的列

所以我通过透视表在“用户”和“照片”之间有很多关系user_photo。我belongsToMany('Photo')在用户模型中使用。但是这里的麻烦是我的Photo表中有很多我不需要的列(特别是在json响应期间)。因此,一个例子是:

//Grab user #987's photos:
User::with('photos')->find(987);
//json output:
{
  id: 987,
  name: "John Appleseed",
  photos: {
    id: 5435433,
    date: ...,
    name: 'feelsgoodman.jpg',
    ....// other columns that I don't need
  }
}
Run Code Online (Sandbox Code Playgroud)

是否可以修改此方法,以使Photos模型仅返回可接受的列(例如由数组指定['name', 'date'])?

User.php

public function photos()
{
  return $this->belongsToMany('Photo');
}
Run Code Online (Sandbox Code Playgroud)

注意:仅执行一次操作时,我只想选择特定的列User->belongsToMany->Photo。当做类似的事情时Photo::all(),是的,我希望所有列都一样。

编辑:我已经尝试过在Laravel Eloquent中使用“ with()”函数来获取特定的列,但仍在选择列。也是https://github.com/laravel/laravel/issues/2306

php model pivot-table laravel

5
推荐指数
2
解决办法
4873
查看次数

雄辩的"选择"方法无法使用"with"方法

我的村庄模型;

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Village extends Model {
    public function positions() {
        return $this->belongsTo(Map::class, 'id', 'field_id');
    }
}
Run Code Online (Sandbox Code Playgroud)

我的Map类迁移;

Schema::create('map_data', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('field_type');
    $table->integer('field_id');
    $table->string('x');
    $table->string('y');
    $table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)

我在"VillageController"课上的"村庄"方法;

public function villages() {
    $villages = Village::with([
        'positions' => function ($query) {
            $query->select('x', 'y');
        }
    ])->get();

    return $villages;
}
Run Code Online (Sandbox Code Playgroud)

结果;

{
  "villages": [
    {
      "id": 1,
      "name": "village 1",
      "created_at": "2016-10-26 18:36:34",
      "updated_at": "2016-10-26 18:36:34",
      "positions": null
    },
    {
      "id": 2,
      "name": "village 2",
      "created_at": …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-5

4
推荐指数
1
解决办法
3480
查看次数

如何从 Laravel Eloquent API 资源中仅获取特定字段?

我正在开发一个可通过移动和网络应用程序访问的 API。
我需要从 API 资源发送特定字段。

我有ItemResource

public function toArray($request) {
    return [
        'id'          => $this->id,
        'name'        => $this->name,
        'description' => $this->description,
        'price'       => $this->price,
        'stock'       => $this->stock,
        'reviews'     => $this->reviews, // this is from reviews table
    ];
}
Run Code Online (Sandbox Code Playgroud)

我的Item模特

public function reviews() {
    return $this->hasMany(ItemReview::class);
}
Run Code Online (Sandbox Code Playgroud)

我的控制器代码

return ItemResource::collection(Item::all());
Run Code Online (Sandbox Code Playgroud)

我得到这个数组

{
    "data": {
        "id": 10,
        "name": "Item Name",
        "description": "Item description",
        "price": 410,
        "stock": "815",
        "reviews": [
            {
                "id": 4,       // I don't want to get this …
Run Code Online (Sandbox Code Playgroud)

php mysql laravel eloquent

3
推荐指数
1
解决办法
4293
查看次数

Laravel with([]) 只选择关系的一些列

编辑

这个问题很独特,因为它提出了独特的问题,例如:

  1. 与组件之间的关系。(items.product.stockManagement)。
  2. 大量组件,这导致链接问题的公认答案不适用。

假设你有一个像下面这样的大 with():

$order = Order::with([
        'company',
        'complaints',
        'person',
        'items',
        'items.product',
        'items.product.stockManagement',
        'status', 
    ])->findOrFail($id);
Run Code Online (Sandbox Code Playgroud)

然后我如何选择他们的所有关系,但为其中一些选择特定的列?

$order = Order::with([
        'company',   // select only id,name, size
        'complaints',  // select only id, name , body
        'person',     
        'items',  
        'items.product',  // select only id, name , price
        'items.product.stockManagement',
        'status',  // select only id
        'items.product.media',
        'items.product.mainProduct',
        'items.product.mainProduct.media'
    ])->findOrFail($id);
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent

1
推荐指数
1
解决办法
280
查看次数

标签 统计

laravel ×5

php ×5

eloquent ×3

laravel-4 ×1

laravel-5 ×1

model ×1

mysql ×1

pivot-table ×1