Showing Page

Published 1 month ago by davy_yg

ErrorException in 7acfe019ded6ab1ceaef14a2b1b3e25f line 30: Undefined property: Illuminate\Database\Eloquent\Collection::$title (View: C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\pages.blade.php)

Line 30: Even the all-powerful Pointing {{ $pages->title }}

pages.blade.php

<section id="container">
    <menu></menu>    
    <cont>
        <center><b>Even the all-powerful Pointing {{ $pages->title }}</b></center><br> 

        {{ $pages->content }}
        
    </cont>        
    <sidenav>

        {{ $pages->pages_name }}<br><br>

PageController.php

public function frontindex()
    {
    // page on frontpage

    $domain = $_SERVER['SERVER_NAME'];
    $user = User::where('domain', $domain)->first();  
    
    $pages = Pages::where('user_id', $user->id)->get();
    
    return view('soulfy.pages')->with(['pages' => $pages]);

    }
Best Answer (As Selected By davy_yg)
tisuchi

Huh...! @davy_yg

It's typo.

It should be-

<center><b>Title: {{ $pages->pages_title }}</b></center><br> 

        Content :{{ $pages->pages_content }}

Instead of-

<center><b>Title: {{ $pages->title }}</b></center><br> 

        Content :{{ $pages->content }}
tisuchi
tisuchi
1 month ago (262,165 XP)

Its because $pages is a collections. But you are trying to access as a single object.

You need to change here-

@foreach($pages as $page)

    <cont>
        <center><b>Even the all-powerful Pointing {{ $page->title }}</b></center><br> 

        {{ $page->content }}
        
    </cont>        
    <sidenav>

        {{ $page->pages_name }}<br><br

@endforeach

Note: Of course there are few other ways to fix this.

davy_yg

i can't understand where all the data goes since the data seems to be empty:

@foreach($pages as $page) 
<section id="container">
    <menu></menu> 
      
    <cont>
        <center><b>{{ $page->title }}</b></center><br> 

        {{ $page->content }}
           
    </cont>        
    <sidenav>

        {{ $page->pages_name }}<br><br>
        Category #2<br><br>
        Category #3<br><br>
    </sidenav>
  

</section>    
 @endforeach 

Yet, if I type

     <?php dd($page) ?> 

it shows something.

I can't even see the {{ $page->pages_name }} which located right above Category #2.

tisuchi
tisuchi
1 month ago (262,165 XP)

If that's the case, you need to make sure that following object is returning data.

    $user = User::where('domain', $domain)->first();  

If this is empty, surely you won't get any data on $pages collection.

davy_yg

I try typing

 <?php dd($user) ?> 

on apages.blade.php and I can see the user.

PageController.php

public function frontindex()
    {
        // page on frontpage

        $domain = $_SERVER['SERVER_NAME'];
        $user = User::where('domain', $domain)->first();  
    
        $pages = Pages::where('user_id', $user->id)->get()->first();
    
        return view('soulfy.pages', ['pages' => $pages]);

    }

Now, is it still a collection after using ->first();

pages.blade.php

<section id="container">
    <menu></menu> 
      
    <cont>
        <center><b>Title: {{ $pages->title }}</b></center><br> 

        Content :{{ $pages->content }}
           
    </cont>        
    <sidenav>

        {{ $pages->pages_name }}<br><br>
        Category #2<br><br>
        Category #3<br><br>
    </sidenav>
  

    </section>    

Now, I can only see {{ $pages->pages_name }}, I wonder why the title and content don't appear while they are in the database.

tisuchi
tisuchi
1 month ago (262,165 XP)

Minimize your method.

From -

        $pages = Pages::where('user_id', $user->id)->get()->first();

to

        $pages = Pages::where('user_id', $user->id)->first();

Try now. If not working, can you dd() your page and show the result?

davy_yg

Is not working.

dd result:

Pages {#321 ▼ #table: "cms_pages" #connection: null #primaryKey: "id" #perPage: 15 +incrementing: true +timestamps: true #attributes: array:7 [▶] #original: array:7 [▶] #relations: [] #hidden: [] #visible: [] #appends: [] #fillable: [] #guarded: array:1 [▼ 0 => "*" ] #dates: [] #dateFormat: null #casts: [] #touches: [] #observables: [] #with: [] #morphClass: null +exists: true +wasRecentlyCreated: false }

"id" => 1 "user_id" => 43 "pages_name" => "Page #1" "pages_title" => "Far far away, behind the word mountains" "pages_content" => "

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic&" "updated_at" => "2017-10-17 02:35:41" "created_at" => "2017-10-12 09:54:24" ]

tisuchi
tisuchi
1 month ago (262,165 XP)

Huh...! @davy_yg

It's typo.

It should be-

<center><b>Title: {{ $pages->pages_title }}</b></center><br> 

        Content :{{ $pages->pages_content }}

Instead of-

<center><b>Title: {{ $pages->title }}</b></center><br> 

        Content :{{ $pages->content }}
davy_yg

Now, I cannot think how to alternate the page number: home/pages/1, home/pages/2 etc. for each new pages.

Also everytime the page link being click I must see the page article being pull out.

PageController.php

public function frontindex()
   {
    // page on frontpage

    $domain = $_SERVER['SERVER_NAME'];
    $user = User::where('domain', $domain)->first();  
    
    $pages = Pages::where('user_id', $user->id)->get();
    
    return view('soulfy.pages', ['pages' => $pages]);

   }

pages.blade.php

<section id="container">
    <menu></menu> 
      
    <cont>
        <center><b></b></center><br> 

                       
    </cont>        
    <sidenav>

        @foreach( $pages as $page)
        
        <a href="{{url('')}}/home/pages/1">{{ $page->pages_name }}</a><br><br>    
                               
        @endforeach
        
    </sidenav>
  

    </section>  
tisuchi
tisuchi
1 month ago (262,165 XP)

Just do this-

 @foreach( $pages as $page)
        
        <a href="{{url('')}}/home/pages/{{ $page->id }}">{{ $page->pages_name }}</a><br><br>    
                               
        @endforeach 

And of course, in your method, you need to make sure that you use paginate instead of get().

$pages = Pages::where('user_id', $user->id)->paginate(10);
davy_yg

ErrorException in Builder.php line 2123: Call to undefined method Illuminate\Database\Query\Builder::renders() (View: C:\xampp\htdocs\soulfy_repo\framework\resources\views\soulfy\pages.blade.php)

I am not sure how to define the pagination where to place it.

Looks like the error goes to the view.

I am not sure if I can make each title and content to be one page. That's one way, but it's better that the article only comes out if I click the page navigation only not the paging.

tisuchi
tisuchi
1 month ago (262,165 XP)

How to make a pagination?

Here is an example-

$users = User::latest()->paginate(15);

Ref: https://laravel.com/docs/5.5/pagination#basic-usage

Now in your view, how to use pagination. Here is an example-

div class="container">
    @foreach ($users as $user)
        {{ $user->name }}
    @endforeach
</div>

{{ $users->links() }}

Ref: https://laravel.com/docs/5.5/pagination#displaying-pagination-results

davy_yg

In my case I think I should use {!! $users->render() !!} since it's laravel 5.1

Even so it's still displaying the same error. I try using link() and the error remains the same.

<section id="container">
    <menu></menu> 
      

    <cont>

        @foreach( $pages as $page)

        <center><b>{{ $page->pages_title }}</b></center><br> 

        {{ $page->pages_content }}

        @endforeach


        {!! $page->render() !!}

    </cont>

    

    <sidenav>

        @foreach( $pages as $p)
        
        <a href="{{url('')}}/home/pages/{{ $p->id }}">{{ $p->pages_name }}</a><br><br>    
                               
        @endforeach

    </sidenav>
  

    </section>

If not using paging, can I not use another way such as query the database using query building by pulling the id from the url?

How to pull the id from the url? like the home/pages/id

How to pull the id out and store it into a variable?

tisuchi
tisuchi
1 month ago (262,165 XP)

Ohh... You are using L5.1!

Yes, in your version, you have to use

{!! $users->render() !!}

Ref: https://laravel.com/docs/5.1/pagination#displaying-results-in-a-view

davy_yg

Just solved it.

pages.blade.php

<?php

$url = $_SERVER['REQUEST_URI'];
$pos = strrpos($url, '/');
$id = $pos === false ? $url : substr($url, $pos + 1);   

$pages2 = \Soulfy\Pages::where('id', $id)->first();
                                                
?>

@extends('home')

@section('title', 'Home')


@section('content')


    <section id="container">
    <menu></menu> 
      
            
    <cont>

        
        <center><b>{{ $pages2->pages_title }}</b></center><br> 

        {{ $pages2->pages_content }}
                    
    </cont>

Please sign in or create an account to participate in this conversation.