duanhuancong1969 2016-09-25 17:03
浏览 56
已采纳

试图在Laravel 5.3中获得非对象的属性

guys i am working on laravel project (i am beginner in laravel ) my role in this project is making the views and injecting the data ... while i am trying to inject Post in my view i got this error "Trying to get property of non-object " i am searching on the internet and some says that "may be the object returns null but i checked all the data comes from the object in the Controller and i found data by dd($post);"

here's the HomeController method that returns the post object

public function index()
        {
            $sliders = Slider::take(5)->get();

            $post = Post::latest()->first();

            $users = User::subscribed()->take(12)->get()->shuffle();

        return view('frontend.pages.home2', compact('sliders', 'sliders', 'users', 'post'))->with('success',' success test');
        }

and this is what i am trying to do in the view

             @foreach($post as $pos)
                   <p  class="text-primary col-lg-4 col-md-4 sliderParagraph menuDirection">

                      {{$pos->body_ar}}
                    </p>
                @endforeach

            <p  class="text-primary col-lg-4 col-md-4 sliderParagraph menuDirection">
        some text here to be displayed 
                        <br>
                        <button  type="button" class=" btn btn-primary">more</button>
                    </p>
                    <img   class="nlpimage img-responsive " src="images/nnn.jpg">
                </div>

and here's the Post.php class

        namespace App\Src;

        use App\Core\Traits\LocaleTrait;
        use Illuminate\Database\Eloquent\Model;
        use Illuminate\Notifications\Notifiable;

        class Post extends Model
        {

            use Notifiable, LocaleTrait;

            public $localeStrings = ['title', 'body'];
            public $guarded = [];

            public function user()
            {
                return $this->belongsTo(User::class);
            }

            public function gallery()
            {
                return $this->morphMany(Gallery::class, 'galleryable');
            }

        }

and the post table in the DB enter image description here

  • 写回答

2条回答 默认 最新

  • drhozgt6007 2016-09-25 17:14
    关注

    Simply remove the foreach, because you are just passing one post instance to the view:

    <p  class="text-primary col-lg-4 col-md-4 sliderParagraph menuDirection">
    
    {{$post->body_ar}}
    </p>
    

    If you want to display more post, change to controller, to get more results (and use the foreach in your template, because now its an array):

    $post = Post::latest()->take(3)->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作