Laravel router with additional parameters - laravel

I have controller which called by this route website.lrv/products/{category-url}
public function categoryProducts($uri, Request $request) { /** some code **/ }
I have a few links that are responsible for ordering products, i added them directly to blade template, like this:
route('client.category.products', [
'url' => Route::current()->url,
'order' => 'article',
'by' => 'desc' ])
route('client.category.products', [
'url' => Route::current()->url,
'order' => 'article',
'by' => 'asc' ])
And the request link with ordering is:
website.com/products/chargers?order=name&by=asc
Now i want to add products filters. I have many filters, each filter can contain many values. The problem is that i don't know how to make route to get some like that:
website.com/products/chargers?width=10,20,30&height=90,120,150
or something like that.
I need to get request array like that:
$request = [
....
filters => [
width => ['10','20','30'],
height => ['90','120','150'],
],
....
];`
If you need some additional info, i will edit my question.

You can access the array you are looking for by using $request->query() within the categoryProducts function.
Edit: Also worth noting you can access them from within your blade template using the Laravel helper function request(), so {{ request()->query('width') }} would return xx where website.lrv/products/category?width=xx.
Edit 2: Note that if your query string includes commas per your example width=10,20,30 the query() will just return a comma separated string, so you would need to explode(',',$request->query('width')) in order to get the array of widths

I think you have to add them manually like this:
route('client.category.products')."?order=name&by=asc";
Try this I hope it help.

Related

How to enable filters in Backpack for Laravel?

I am looking at the doc page: https://backpackforlaravel.com/docs/4.1/crud-filters.
The simple question is: where must I call $this->crud->filters()? I tried to add at the end of setupListOperation() in my controller, but I don't see filters over my table. For curiosity: I assigned esit of a call to a var and dumped it, and it's an empty collection.
protected function setupListOperation()
{
CRUD::setFromDb(); // columns
CRUD::column('assignmentTypes')->type('relationship');
/**
* Columns can be defined using the fluent syntax or array syntax:
* - CRUD::column('price')->type('number');
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
*/
dd($this->crud->filters()); // <<<<------------- EMPTY COLLECTION!
}
Please note that manually adding a filter works!
$this->crud->addFilter([
'type' => 'text',
'name' => 'first_name',
'label' => 'First Nane'
], false,
function ($value) { // if the filter is active
$this->crud->addClause('where', 'first_name', 'LIKE', "%$value%");
}
);
But I ask you why filters() doesn't do something automatically.
You can use $this->crud->filters() or CRUD::filters() anywhere you'd like, but it makes most sense to use it in your setupListOperation() like you have, but AFTER you've already added some filters. Before the addFilter() statements the result is an empty collection, because there are no filters.
Backpack Filters are enabled automatically if you add one to your List operation.

Laravel 5.6. How to test JSON/JSONb columns

$this->assertDatabaseHas() not working with JSON/JSONb columns.
So how can I tests these types of columns in Laravel?
Currently, I have a store action. How can I perform an assertion, that a specific column with pre-defined values was saved.
Something like
['options->language', 'en']
is NOT an option, cause I have an extensive JSON with meta stuff.
How can I check the JSON in DB at once?
UPD
Now can be done like that.
I have solved it with this one-liner (adjust it to your models/fields)
$this->assertEquals($store->settings, Store::find($store->id)->settings);
Laravel 7+
Not sure how far back this solution works.
I found out the solution. Ignore some of the data label, Everything is accessible, i was just play around with my tests to figure it out.
/**
* #test
*/
public function canUpdate()
{
$authUser = UserFactory::createDefault();
$this->actingAs($authUser);
$generator = GeneratorFactory::createDefault();
$request = [
'json_field_one' => [
'array-data',
['more-data' => 'cool'],
'data' => 'some-data',
'collection' => [
['key' => 'value'],
'data' => 'some-more-data'
],
],
'json_field_two' => [],
];
$response = $this->putJson("/api/generators/{$generator->id}", $request);
$response->assertOk();
$this->assertDatabaseHas('generators', [
'id' => $generator->id,
'generator_set_id' => $generator->generatorSet->id,
// Testing for json requires arrows for accessing the data
// For Collection data, you should use numbers to access the indexes
// Note: Mysql dose not guarantee array order if i recall. Dont quote me on that but i'm pretty sure i read that somewhere. But for testing this works
'json_field_one->0' => 'array-data',
'json_field_one->1->more-data' => 'cool',
// to access properties just arrow over to the property name
'json_field_one->data' => 'some-data',
'json_field_one->collection->data' => 'some-more-data',
// Nested Collection
'json_field_one->collection->0->key' => 'value',
// Janky way to test for empty array
// Not really testing for empty
// only that the 0 index is not set
'json_field_two->0' => null,
]);
}
Note: The below solution is tested on Laravel Version: 9.x and Postgres version: 12.x
and the solution might not work on lower version of laravel
There would be two condition to assert json column into database.
1. Object
Consider Object is in json column in database as shown below:
"properties" => "{"attributes":{"id":1}}"
It can assert as
$this->assertDatabaseHas("table_name",[
"properties->attributes->id"=>1
]);
2. Array
Consider array is in json column as shown below:
"properties" => "[{"id":1},{"id":2}]"
It can assert as
$this->assertDatabaseHas("table_name",[
"properties->0->id"=>1,
"properties->1->id"=>2,
]);
Using json_encode on the value worked for me:
$this->assertDatabaseHas('users', [
'name' => 'Gaurav',
'attributes' => json_encode([
'gender' => 'Male',
'nationality' => 'Indian',
]),
]);

Nice names for array values in attributes for Laravel 5.4

Laravel receives data in this format
['roles' => [1, 4]]
Rules
$rules = ['roles.*' => 'integer|min:1|exists:roles,id']
When Validator fails it shows
[
"roles.0" => [
"The roles.0 must be an integer.",
],
]
How can I make it automatically convert all such cases into array of errors with key (in this case) roles. There will be many such validators and it will be copy-paste to do foreach for every such case.
You could try to edit resources/view/en/validation.php (assuming you want to do this for an english website) and modify attributes section (at the end of the file) like this:
'attributes' => [
'roles.0' => 'Admin',
'roles.1' => 'Moderator',
// etc.
],
One other solution (if you want to avoid duplication) could be to write a small blade with your rules:
#foreach ($errors->get('roles.*') as $message)
// Do what you want with some #if
#endforeach
Then just include this in your different pages:
#if ($errors)
#include('roles-error-messages')
#endif

Laravel 5.2 Validate File Array

I have a form with three fields: title, body and photo[]. I'm trying to validate it so that at least one item is filled in, but I can't seem to get it to work. If I upload a file I still receive an error for title and body.
public function rules()
{
return [
'title' => 'required_without_all:body,photo.*',
'body' => 'required_without_all:title,photo.*',
'photo.*' => 'required_without_all:title,body',
'photo.*' => 'mimes:jpeg,gif,png',
];
}
Update: Jonathan pointed out that I had my rules wrong. I've fixed them and am now using this. It's still not working; when I try to upload a photo I get the error message that the other fields are required.
public function rules()
{
return [
'title' => 'required_without:body,photo.*',
'body' => 'required_without:title,photo.*',
'photo.*' => 'required_without:title,body|mimes:jpeg,gif,png',
];
}
If you're looking to ensure the photo field is an array then you need 'photo' => 'array' and then you can use 'photo.*' => '' for the other validations of the array's children.
The rules are separated by a pipe character | so if you were going to combine the two in your example it would be 'photo.*' => 'required_without_all:title,body|mimes:jpeg,gif,png',. I don't see you using the pipe to separate rules so I can't be sure you are aware of it.
This may have been where you were going wrong in the first place (two keys in the associative array that are identical) and some kind of precedence taking affect negating one of the rules.
You could try something like this (for the record I think you were on the right track to begin with using required_without_all as this stipulates the need to be required if all of the given fields are missing):
public function rules()
{
return [
'title' => 'required_without_all:body,photo',
'body' => 'required_without_all:title,photo',
'photo' => 'array',
'photo.*' => 'required_without_all:title,body|mimes:jpeg,gif,png',
];
}
Reference

Yii2 : Active Record Column Aliases

I'm using Yii2 framework with advanced template.
I get problem with column alias in my controller file, here's my code:
$models = new ActiveDataProvider([
'query' => User::find()->select(['member'=>'fullname'])
]);
The above query equivalent with:
SELECT fullname AS member FROM User;
I send the data to the view using this code:
return $this->render('view', [
'model' => $models,
]);
I want to call the data in my view using GridView widget, here's my code:
echo GridView::widget([
'dataProvider' => $model,
'columns' => [
'member',
],
]);
However, I got an error that tell me the 'member' parameter is not defined.
How can I show the data of my query by calling the column name? (in my case it using alias)
I really appreciate any kind of helps!!
You should simply declare this attribute in your model :
class User extends ActiveRecord
{
public $member;
Read more : https://www.yiiframework.com/doc/guide/2.0/en/db-active-record#selecting-extra-fields
ActiveDataProvider works only with model attributes. member obviously is not presented there.
First of all, maybe it's better to refactor column names to be more clear instead of writing aliases? I don't see any benefit in your example.
If you nevertheless need to use aliases, as alternative for adding additional properties to class, you can work with them with help of ArrayDataProvider and SqlDataProvider.
Examples of usage:
ArrayDataProvider:
use yii\data\ArrayDataProvider;
$dataProvider = new ArrayDataProvider([
'allModels' => User::find()->select(['member' => 'fullname'])->all(),
]);
SqlDataProvider:
use yii\data\SqlDataProvider;
use yii\db\Query;
...
$query = (new Query())
->select(['member' => 'fullname'])
->from('users');
$command = $query->createCommand();
$dataProvider = new SqlDataProvider([
'sql' => $command->sql,
'params' => $command->params,
'totalCount' => $query->count(),
]);
For more details and features of usage please see official docs.
For your case it's better to use ArrayDataProvider, SqlDataProvider is for more complex queries.
In case of one alias and using model methods adding additional attribute as suggested by soju can be better.
But in my opinion it's useless and it's better to refactor column names in case of some ambiguity.

Resources