I have a question related to laravel validation, My question is when we apply rules like unique or exists, validator query through eloquent model or execute raw query? I am using Laravel 4.2.
I have found answer of my question. According to my investigation Validators run query through Query builder, and by default query builder uses default connection, If you want change the connection, you can through following code.
$verifier = \App::make('validation.presence');
$verifier->setConnection('other_connection_name');
$validation = $this->validator->make($data, static::$rules);
$validation->setPresenceVerifier($verifier);
if($validation->fails()) throw new ValidationException($validation->messages());
Related
I go through 'SPATIE query builder' documentation and find they cannot describe properly how to post methods implemented. I need to filter data based on multiple parameters including the data add date filter, and I am using the POST method, How I can achieve this for 'SPATIE query builder'.
I can Filter 1 field by writing below code
$data = QueryBuilder::for(EmpData::where('empAddedByAgent', $request->searchAgent))
->get();
The form Look Like Below Image
Flowing query is working fine on mysql database but how can I retrieve this in laravel? code of mysql is given bellow:
select count('brand_id') from products where brand_id=3
You could do
$number = 3;
DB::table('products')->where('brand_id',$number)->count();
But I recommend using Models
Product::where('brand_id',$number)->count();
Following what laravel has on https://laravel.com/docs/5.6/queries at the aggregates topic:
$products = DB::table('products')->where('brand_id', 3)->count();
Welcome to Stack Overflow!
As #PlayMa256 said, you can do exactly this query using
DB::table('products')->where('brand_id', 3)->count();
BUT, if you have defined your models correctly, you can do this directly using you model class. If your model is called Product, then:
Product::where('brand_id', 3)->count();
Another awesome way to do this is by using the magic methods for where clause:
Product::whereBrandId(3)->count();
I am new to Laravel 5.4 and working on some query manipulation. Now I have created an query using query builder which is shown below:
$view = DB::table('blocks')
->leftjoin('programmes', 'blocks.programme_id', '=', 'programmes.id')
->select('blocks.id', 'blocks.programme_id', 'blocks.name', 'blocks.colour', 'blocks.year', 'programmes.title AS programme');
I have two more table "dates" and "modules". Each dates as well as module belongs to blocks.
Now I want to fetch all blocks with programmes, dates and modules. I know i can use with() method to get all of these. But as per my knowledge on Laravel, I can use with() method only if I have model file of each table and have relationship between them.
But do not want to use model and define relationship between them. I just want to know How can I get block data with programmes, dates and modules without creating model and defining relationship betwen them in model? Is there any other way to use with() method without model?
Block dates and modules are conditional, Sometime I dont want get data of this table with block.
Please help me on this.
You can't do it automatically. Eager loading is only for Eloquent model so you cannot use it with query builder. However in most cases you can use Eloquent also for getting more complicated queries (you can also use joins when using Eloquent) so you will be able to use eager loading.
But if you don't want to use Eloquent at all, obviously you will need to create some custom mechanism for eager loading.
I have Question and Answer models. The Question hasMany Answers. Following commands run in the php artisan tinker mode invoke a database query for no apparent reason:
$q = new Question;
$q->answers[] = new Answer; // invokes the below query
// the executed query
select * from `answers` where `answers`.`question_id` is null and `answers`.`question_id` is not null
As you see, there is no need for database call whatsoever. How can I prevent it?
When you do $q->answers, Laravel tries to load all of the answers on the question object - regardless of whether they exist or not.
Any time you access a relationship on a model instance, you can either call it like answers() and get a query builder back, or you can call it like answers without parentheses and Laravel will fetch a collection for you based on the relationship.
You can prevent it easily by doing this:
$q = new Question;
$a = new Answer;
And then, when you're ready to save them, associate them with each other. In its simplest form, that looks like this:
$q->save();
$q->answers()->save($answer);
It's doing that because you're assigning it to the Question object. It wants to see if you're adding an extant record reference. All Laravel Eloquent models contain magic methods for properties, and trying to use them as temporary data storage is a really bad idea unless you've defined a property on them ahead of time for that specific purpose.
Just use a regular array instead and then associate them after the models have been prepared.
Documentation on one-to-many relationships:
https://laravel.com/docs/5.1/eloquent-relationships#one-to-many
I am trying to get related model however i cannot seem to find correct documention. In yii 1.x i can do $jobsprocess->category0, but yii 2.x tell me to do $jobsprocess->getCategory(). This does not return a model but an ActiveQuery. How can I return a model object?
In your query use $model = YourModel::find()->with(['category])->all().
All relations using the getRelation() functions can be access with the with() function, but without the get and lowercase first letter.
You can then access the relational data with $model->category.