How to get M:1 relation in Yii2 - activerecord

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.

Related

Query builder with Laravel using Eloquent

I am very new to Laravel and quite confuse with the model and database thing. I understand that a model represent one table. So I created a model using artisan command without migration and it created the code as follow.
namespace App;
use Illuminate\Database\Eloquent\Model;
class RegCars extends Model
{
//
}
Since I have a different table name, so I add protected $table = 'regcars'; and assume that this model can now access the table by running query such as RegCars::where('user_id', $user_id); from controller. But I wasn't able to get anything by running it.
So I am wondering, how does this model able to run the query? Is the migration needed in order to do this? Is there still other area I need to set before I can run any query?
highly recommend to read docs or at least watch a tutorial.
what you get is a query build not a model or a collection of models. laravel(eloquent) doesnt exactly know what you want so you query database then get the data by methods that laravel gives you if assume you want a RegCars you need a method like first:
RegCars::where('user_id', $user_id)->first(); // now you have model
more info: https://laravel.com/docs/8.x/eloquent

get laravel eloquent model relationships

is there any way to get the defined relationships in eloquent model. I have a situation where I need to get the model relationships so I can update all other eloquent models that relies on a specific id before delete it
There's no unified method to iterate over all registered relationships of a class. You can, however, access all the currently loaded relationships of a model instance (via the ->relations attribute or the getRelations() method), but that's not what you're up to. I'd suggest you take a look at laravel's documentation on inserting and updating relationships. So far that's the best laravel provides out of the box, the rest is developing approaches.
Try this function:
public function getRelations()
You can use
$model->getRelations()
function to get all relations
Also refer below link for details https://laravel.com/api/5.7/Illuminate/Database/Eloquent/Concerns/HasRelationships.html#method_getRelations

Laravel Eloquent find and where return two different types?

When I use:
$myModel::find(1);
I'm completely fine using accessors, for example - that's because it returns an instance of the model?
And if I use
$myModel::where('foo', 'bar');
I can no longer access any accessors or model function, or even relationships on myModel Is that expected behavior and if it is - how should I execute a where query so I can keep using the model's relations?
you need to fetch the model.After that everything will work fine.
$myModel::where('foo', 'bar')->firstOrFail();

Where to process data in Laravel

Back when I was using CodeIgniter I had functions in my models like
public function GetArticlesFormatted($inactive = FALSE)
and then in my controller I could have had
$articles->GetArticlesFormatted(true);
And so on.
How should I achieve the same with Laravel 5.4? The database of the app I'm building is already built and full and is a mess so most of the "automated" super-restrictive things that Laravel has don't work out of the box.
For example there is a Country Code that I'm retrieving and I need it as is, but in some instances I need it converted in a Country Name which I had in another table.
Right now I just have in my controller wherever I am retrieving data from the model:
$countryResult = Country::where('country_code', $item['country_code'])->first();
$countryArray = $countryResult->toArray();
$item['country'] = $countryArray['country_name'];
Can I somehow achieve that in a more elegant way?
I tried accessors but for some reason couldn't get anything to work for my purposes.
A select query can be used to limit selection to a particular column.
$countryName = Country::select('country_name')
->where('country_code', $item['country_code'])
->first();

How to use eager loading with custom query builder in Laravel

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.

Resources