douyan1453
2017-10-16 13:06
浏览 132
已采纳

Laravel 5.5 Eloquent - 文章::找到返回null

I have two files,

CommentTransformer

And

ImageTransformer

In the first file, CommentTransformer, I can retrieve which Article it belongs to by doing this:

$article = Article::find($comment->article_id);

I'm doing the same exact thing in my ImageTransformer, but it returns null. Even if instead of using Article::find($image->article_id) I use Article::find(1) I still get a null result!

Here's the full code:

namespace App\Transformers;

use App\Article;
use App\User;
use App\Image;
use League\Fractal\TransformerAbstract;

class ImageTransformer extends TransformerAbstract
{
/**
 * A Fractal transformer.
 *
 * @return array
 */
public function transform(Image $image)
{
    $user = User::findOrFail($image->user_id);
    $article = Article::find($image->article_id);
   // $userArticle = User::find($article->user_id);
    return [
        'id'                 => (int) $image->id,
        'original_filename'  => $image->original_filename,
        'filename'           => $image->filename,
        'size'               => (int) $image->size,
        'path'               => url('/') . "/" . $image->path . '/' . $image->filename,
        'posted_by_username' => $user->name,
        'article'            => $article //if I call $article->id it returns "Trying to get property of non-object" as it is, it returns null
    ];
}
}

This is the response:

{
"data": [
    {
        "id": 1,
        "original_filename": "ptd.jpg",
        "filename": "f11bbe288649e76ec3b694890160abf930601aed.jpeg",
        "size": 103297,
        "path": "http:\/\/192.168.1.85:1299\/uploads\/f11bbe288649e76ec3b694890160abf930601aed.jpeg",
        "posted_by_username": "josh beck",
        "article": null
    }
   ]
 }

图片转代码服务由CSDN问答提供 功能建议

我有两个文件,

CommentTransformer

ImageTransformer

在第一个文件中, CommentTransformer ,我可以通过这样做来检索它所属的文章:

$ article = Article :: find($ comment-> article_id);

我在ImageTransformer中执行同样的事情,但它返回 null 。 即使不使用 Article :: find($ image-> article_id),我使用 Article :: find(1)我仍然得到一个null结果!

以下是完整代码:

 命名空间App \ Transformers; 
 
use App \ Article; 
use App \ User; 
use App \ 图像; 
use League \ Fractal \ TransformerAbstract; 
 
class ImageTransformer扩展了TransformerAbstract 
 {
 / ** 
 *分形变换器。
 * 
 * @return array 
 * / 
 npublic function transform(  Image $ image)
 {
 $ user = User :: findOrFail($ image-> user_id); 
 $ article = Article :: find($ image-> article_id); 
 // $ userArticle =  User :: find($ article-> user_id); 
 return [
'id'=>  (int)$ image-> id,
'origative_filename'=>  $ image-> original_filename,
'filename'=>  $ image-> filename,
'size'=>  (int)$ image-> size,
'path'=>  url('/')。  “/”。  $ image->路径。  '/'。  $ image-> filename,
'pased_by_username'=>  $ user-> name,
'artment'=>  $ article //如果我调用$ article-> id它返回“试图获取非对象的属性”,它返回null 
]; 
} 
} 
   
 
 

这是响应:

  {
“data”:[
 {
“id”:1,
“  original_filename“:”ptd.jpg“,
”filename“:”f11bbe288649e76ec3b694890160abf930601aed.jpeg“,
”size“:103297,
”path“:”http:\ / \ / 192.168.1.85:1299\/uploads  \ /f11bbe288649e76ec3b694890160abf930601aed.jpeg“,
”posted_by_username“:”josh beck“,
”article“:null 
} 
] 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongye1143 2017-10-16 13:09
    已采纳

    I assume that this one returns null because findOrFail() would throw an exception:

    $article = Article::find($image->article_id);
    

    If this returns null, it means there is no Article with the specified ID. You can check ID with dd($image-article_id);

    Also, you can just load the data by using load() method:

    $image->load(['user', 'article']);
    
    点赞 打赏 评论
  • doucou19961205 2017-10-16 13:16

    I am agreed with Alexey. If there is data of that user id then use:-

    $user = User::find($image->user_id)->first();
    $article = Article::find($image->article_id)->first();
    

    To ensure the existence of data.

    点赞 打赏 评论

相关推荐 更多相似问题