HansSchouten / Laravel-Pagebuilder

A drag and drop pagebuilder to manage pages in any Laravel project
https://www.phpagebuilder.com
MIT License
783 stars 181 forks source link

how to render page data ? #22

Closed kousar2334 closed 4 years ago

kousar2334 commented 4 years ago

public function viewPage($pageId) { $data=DB::table('pagebuilder__pages')->where('id',$pageId)->value('data'); return $data; } I get the page data from the function, How can I render the data into blade?

HansSchouten commented 4 years ago

When the $data is rendered it is a full html structure, so I suggest not to pass it to a blade view file since it is already a full webpage. There are multiple ways to render the data from one page, you could do this:

use PHPageBuilder\Theme;
use PHPageBuilder\Modules\GrapesJS\PageRenderer;
use PHPageBuilder\Repositories\PageRepository;

public function viewPage($pageId)
{
    $theme = new Theme(config('pagebuilder.theme'), config('pagebuilder.theme.active_theme'));
    $page = (new PageRepository)->findWithId($pageId);
    $pageRenderer = new PageRenderer($theme, $page);
    $html = $pageRenderer->render();
    return $html;
}
kousar2334 commented 4 years ago

I have copied the function but there is an error. Error Call to a member function findWithId() on null http://127.0.0.1:8000/admin/pages/1

HansSchouten commented 4 years ago

On what line of the code does this error occur? Can you further trace where it went wrong, this error does not reveal to me what causes it.

kousar2334 commented 4 years ago

public function findWithId($id)

{

    return $this->createInstance($this->db->findWithId($this->table, $id));//the error is occur this line

}

Screenshot_2020-06-28_12-40-03

HansSchouten commented 4 years ago

Ok that shows me what is the issue, the DB is not instantiated. This makes sense because this is done when we create a PHPageBuilder instance, which we did not do. I just committed a fix to the master which will be part of the next release. For now you can add this to the boot method of your App\Providers\AppServiceProvider:

$this->app->singleton('phpPageBuilder', function($app) {
    return new PHPageBuilder(config('pagebuilder'));
});
$this->app->make('phpPageBuilder');
kousar2334 commented 4 years ago

Screenshot_2020-06-28_13-08-46 there is an error.

HansSchouten commented 4 years ago

Yes everytime you create an instance of a class that is not inside the current folder, you need to include it with use [namespace]. If you switch to an editor like PhpStorm it warns you of this and helps to fix it. In this case you need to add:

use PHPageBuilder\PHPageBuilder;
kousar2334 commented 4 years ago

it works. thank you so much.

mansmahamat commented 4 years ago

public function viewPage($pageId) { $data=DB::table('pagebuilder__pages')->where('id',$pageId)->value('data'); return $data; } I get the page data from the function, How can I render the data into blade?

When the $data is rendered it is a full html structure, so I suggest not to pass it to a blade view file since it is already a full webpage. There are multiple ways to render the data from one page, you could do this:

use PHPageBuilder\Theme;
use PHPageBuilder\Modules\GrapesJS\PageRenderer;
use PHPageBuilder\Repositories\PageRepository;

public function viewPage($pageId)
{
  $theme = new Theme(config('pagebuilder.theme'), config('pagebuilder.theme.active_theme'));
  $page = (new PageRepository)->findWithId($pageId);
  $pageRenderer = new PageRenderer($theme, $page);
  $html = $pageRenderer->render();
  return $html;
}

Where write this code ?

HansSchouten commented 4 years ago

You can make a Laravel controller method with this snippet.