John Davidson

php - Laravel ->put() issue - Mixed content (JSON vs. not JSON)

0 comments
Message:


I'm having an issue with using Laravels put() function, as I want put JSON content in this one single scenario.


$datatable->GroupsCollection = $datatable->GroupsCollection->put($job, '{"grade":'.$grade.'}' );

But when trying to create 'fake' JSON, the inserted value will be:
{\"grade\":'VALUE_OF_$GRADE'}
I've tried using str_replace() and stripslashes() to cut out the backwardslashes, but no bueno.


I've Googled around, and reading something about a cast was needed in the Model.
So I put in this:


protected $casts = [
'dvalue' => 'array',
];

This result in breaking existing functionality of the code.


public function getGroupsCollectionAttribute()
{
return collect($this->dvalue ? $this->dvalue['groups'] : null);
}


public function setGroupsCollectionAttribute($value)
{
$currentValue = $this->dvalue ?? new Collection();
$this->dvalue['groups'] = $currentValue->$value;
}

I 'fixed' the get, but I'm not sure how I should format the 'set' function with this new cast and setting it to an array.


Worth to notice is that we have mixed content in the DB-rows, so it's not always JSON.
Any easier way to go around this?



My Answer:
Laravel is a popular PHP framework that is used for building web applications. It provides a clean and elegant syntax that allows developers to write code quickly and efficiently. Laravel also comes with a wide range of features and tools that make it easy to build and maintain web applications.

Some key features of Laravel include:

1. Eloquent ORM: Laravel comes with an Object-Relational Mapping (ORM) system called Eloquent, which makes it easy to work with databases and perform common database operations.

2. Blade templating engine: Laravel uses the Blade templating engine, which allows developers to write clean and reusable templates for their views.

3. Routing: Laravel provides a simple and expressive way to define routes for your application, making it easy to create RESTful APIs and handle incoming requests.

4. Middleware: Laravel allows you to define middleware that can be used to filter HTTP requests entering your application. This can be used for tasks such as authentication, logging, and more.

5. Authentication: Laravel provides a built-in authentication system that makes it easy to implement user authentication and authorization in your application.

Overall, Laravel is a powerful and flexible framework that is well-suited for building a wide range of web applications. Its ease of use, extensive documentation, and active community make it a popular choice for PHP developers.

Rate this post

5 of 5 based on 4120 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us