0
I’m trying to convert a result of a database query to json
yet every time I make a resquest
to get the value of the same the server goes always below and I believe it is because it is a lot of information.
The consultation I am carrying out is as follows:
$page = Page::with('view',
'texts.translations',
'translations',
'activeListings.activeListingItems.translations',
'activeBanners.activeBannerItems.translations',
'activeGalleries.images'
)
->active()
->find($translation->page_id)
->toArray()
Is there any way to return this result in json
I’ve tried through the Eloquent Resource Api
only that the way the BackOffice
is built not allowing me to return as above.
I tried with a simpler query and always returns error 500 in the logs does not appear anything.
Simplest consultation.
$translation = PageTranslation::where('slug', 'home')
->first()->toArray();
Pagetranslation Model
<?php
namespace App\Models;
use App\Traits\Imageable;
use Laravel\Scout\Searchable;
class PageTranslation extends ElementTranslation
{
use Searchable,
Imageable;
public $timestamps = false;
protected $fillable = ['page_id', 'title', 'description', 'keywords', 'slug', 'starred_title', 'starred_text', 'image'];
protected $appends = ['last_slug', 'localized_complete_url', 'complete_url', 'image_url'];
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
return [
'id' => $this->id,
'title' => $this->title,
'description' => $this->description,
'keywords' => $this->keywords,
];
}
//RELATIONS
public function page()
{
return $this->belongsTo(Page::class);
}
//ElementTranslationInterface
public function getLastSlugAttribute(): string
{
return $this->attributes['slug'];
}
public function getCompleteUrlAttribute(): string
{
$slug = $this->last_slug;
if (optional($this->page)->page_id != null) {
$slug = $this->page->parentPage->translate($this->locale)->complete_url . '/' . $slug;
}
/*
if ($this->locale != \LaravelLocalization::getCurrentLocale()) {
$slug = $this->locale . '/' . $slug;
}
*/
return $slug;
}
public function getLocalizedCompleteUrlAttribute(): string
{
return $this->locale . '/' . $this->complete_url;
}
}
Controller
<?php
namespace App\Http\Controllers;
use Storage;
use Carbon\Carbon;
use App\Models\Page;
use App\Models\Text;
use App\Models\View;
use App\Models\Listing;
use Dingo\Api\Routing\Helpers;
use Illuminate\Routing\Controller;
use Illuminate\Contracts\Support\Arrayable;
class PageController extends Controller {
// use \App\Traits\GetViewNameTrait;
use \App\Traits\GetIndentedPageList;
use Helpers;
function __construct() {
$this->locales = config('translatable.locales');
$this->legal = config('lk.legalPages');
}
public function index($string = null) {
$pages = Page::without('activeListings', 'activeBanners', 'activeGalleries', 'texts')->with('parentPage', 'author', 'menuItems')->get();
if ($string == null) {
$filtered = $pages->whereNotIn('id', $this->legal);
} else {
$filtered = $pages->whereNotIn('id', $this->legal)->union($pages->whereIn('page_id', config('reserved.id')));
}
return view('admin.pages.index')->with([
'pages' => $filtered,
]);
}
public function legal() {
$pages = Page::with('author')->findMany($this->legal);
return view('admin.pages.index')->with([
'pages' => $pages,
]);
}
public function create() {
if (auth()->user()->cannot('create-pages')) {
abort(403);
}
$now = Carbon::now();
return view('admin.pages.create')->with([
'pages' => $this->indentList(),
'views' => View::all()->pluck('description', 'id'),
'locales' => $this->locales,
'date' => $now->toDateString(),
'time' => $now->toTimeString(),
'schedule' => 0,
'now' => 1,
'forever' => 1,
'until' => 0,
]);
}
public function store(\App\Http\Requests\CreatePage $request) {
if (auth()->user()->cannot('create-pages')) {
abort(403);
}
$page = (new Page)->fill($request->all());
if (auth()->user()->cannot('publish-all-pages')) {
$page->published_at = $page->published_until = '';
}
if (count($request->files) > 0) {
foreach ($request->files as $locale => $file) {
$imageExtension = $file['image']->getClientOriginalExtension();
$imageName = setImageName($page->translate($locale)->title, $imageExtension);
$page->translate($locale)->image = Storage::disk('public')->putFileAs('pages', $file['image'], $imageName);
}
}
$page->save();
return redirect('admin/pages/edit/' . $page->id)->with('ntsuccess', 'Criada com sucesso!');
}
public function edit($id) {
$page = Page::findOrFail($id);
if (($page->active && auth()->user()->cannot('edit-published-pages'))) {
abort(403);
}
return view('admin.pages.edit')->with([
'page' => $page,
'views' => View::all()->pluck('description', 'id'),
'locales' => $this->locales,
'pages' => $this->indentList($id),
]);
}
public function update(\App\Http\Requests\CreatePage $request, $id) {
if (auth()->user()->cannot('edit-published-pages') || auth()->user()->cannot('edit-pages')) {
abort(403);
}
$page = Page::findOrFail($id);
$page->fill($request->all());
if (!$request->has('starred')) {
$page->starred = 0;
}
if (auth()->user()->cannot('publish-all-pages')) {
$page->published_at = $page->published_until = '';
}
if (count($request->files) > 0) {
foreach ($request->files as $locale => $file) {
$translated = $page->translate($locale);
$imageExtension = $file['image']->getClientOriginalExtension();
$imageName = setImageName($translated->title, $imageExtension);
if ($translated->getOriginal('image') != '') {
Storage::disk('public')
->delete($translated->getOriginal('image'));
}
$translated->image = Storage::disk('public')->putFileAs('pages', $file['image'], $imageName);
}
}
$page->save();
$this->updateText($request);
return redirect('admin/pages/edit/' . $page->id)->with('ntsuccess', 'Editada com sucesso!');
}
public function destroy($id) {
$page = Page::findorFail($id);
if (auth()->user()->cannot('delete-all-pages')) {
abort(403);
}
$page->delete();
return redirect()->back()->with('ntsuccess', 'Eliminada com sucesso!');
}
/*
* gets an array like:
* [
* 55 => "<p>principal</p>\r\n",
* 56 => "<p>secundário</p>\r\n"
* ]
*
* the key is the text id, the value is the value to store
*/
private function updateText(\App\Http\Requests\CreatePage $r) {
foreach ($this->locales as $locale) {
foreach ($r[$locale]['text'] as $key => $value) {
$text = Text::findOrFail($key);
$text->translateOrNew($locale)->body = $value;
$text->save();
}
}
}
public function pagecontent(){
$translation = Listing::with('translations', 'listingItems.translations')->find(4)->toArray();
dd($translation);
return $this->response->array($translation->attributesToArray());
}
}
If you change the toArray()
for attributesToArray()
works.
I think you’re running too much stuff, I’d like to understand why you’re carrying so many relationships?
– novic
Because the result of this modal is the need to send
– Thepeter
It was to work, post the models too, there’s no way to know so, have to look at the tables and maybe the relationships to try to find the problem identified by you
– novic
I tested with a simpler query and gives the same problem yet with attributesToArray works. I added this detail to the question and the model too.
– Thepeter
Please help me I’m already in despair :)
– Thepeter
Puts every controller
– novic
I’ve updated the question
– Thepeter
is the Index method?
– novic
I don’t have that method
– Thepeter
In your controller you have an index method?
– novic