I was hoping I could do this on one line:
$users = [];
$rawUsers = User::select('id','first_name','last_name')->where('company_id',Auth::user()->company_id)->get();
foreach($rawUsers as $u) {
$users[$u['id']] = $u['first_name'].' '.$u['last_name'];
}
I see the Collection class has a ->map function which would let me do the concatenation, but I don't know how to get the results indexed by id. Is there a way to do what I want?
You can do this using lists method on the query:
User::select(DB::raw('concat(first_name," ",last_name) as full_name'), 'id')->lists('full_name', 'id');
// returns array(id => full_name, ...)
Or use accessor on the model and lists on the collection:
// User model
public function getFullNameAttribute()
{
return $this->attributes['first_name'].' '.$this->attributes['last_name'];
}
// then on the collection call lists:
$usersRaw = User::where(...)->get()->lists('fullName','id');
Related
I am trying to write a query that selects columns from a model then selects some columns from a morph relationship table. But I have no idea to select columns, and relation tables have different columns. So some column has no slug, some have.
public function index()
{
$menus = Menu::whereActive(true)
->with([
'menuable' => function ($q) {
// This gives error if there is no relation Pages model
$q->whereActive(true)->select('pages.id', 'pages.slug');
// Below not working
// if($q->type === Page::class){
// $q->whereActive(true)->select('pages.id', 'pages.slug');
// } else if($q->type === Category::class){
// $q->whereActive(true)->select('categories.id',
'categories.slug');
// }
}
])
->get(['id', 'menuable_id', 'menuable_type', 'name']);
$response = [
'menus' => $menus,
];
return $this->sendResponse($response);
}
Models
class Menu extends Model
{
public function menuable()
{
return $this->morphTo();
}
}
class Page extends Model
{
public function menu()
{
return $this->morphOne(Menu::class, 'menuable');
}
}
class Category extends Model
{
public function menu()
{
return $this->morphOne(Menu::class, 'menuable');
}
}
How can I select specific columns from morph relation with checking morph type? I am using Laravel version 8.
The polymorphic relation is something the Eloquent aware of, and DBMS hasnot implemented this feature in it.
so there cannot be a sql query which join a table to another tables based on the morph column.
so you have to use distinct queries for every polymorphic join relation on your models:
//you can retrieve distinct menu based on their relation
Menu::whereActive(true)->hasPage()->with('pages');
//and having the ralations in the menu model:
public function posts
Menu::whereActive(true)->hasCategory();
//scope in menu class can be like:
public function scopePage($query){
return $query->where('menuable_type',Page::class);
}
public function scopeCategory($query){
return $query->where('menuable_type',Category::class);
}
//with these you can eager load the models
Menu::whereActive(true)->hasPage()->with('page');
Menu::whereActive(true)->hasCategory()->with('category');
public function page(){
return $this->belongsTo(Page::class);
}
public functioncategory(){
return $this->belongsTo(Category::class);
}
if you want a common interface to use one of these dynamically.
you can use:
$menu->menuable->id
$menu->menuable->slug
I am not sure which columns you want to response, but as i can guess from your question, I suppose you want id and slug from both models.
public function index(){
$pagedMenu = Menu::whereActive(true)->hasPage()->with('page');
$categoriedMenu = Menu::whereActive(true)->hasCategory()->with('category');
$menues = $pagedMenu->merge($categoriedMenu);
$response = [
'menus' => $menus,
];
return $this->sendResponse($response);
}
You can perfom separate filters based on the morph-class. This can be achieved with the whereHasMorph (https://laravel.com/docs/8.x/eloquent-relationships#querying-morph-to-relationships).
If you need to automatically resolve the relationship you can use with. This will preload morphables automatically into your resulting collection.
The following example uses an orWhere with 2 separate queries per morphable.
$menus = Menu::whereActive(true)
->whereHasMorph('menuable', [Page::class], function (Builder $query) {
// perform any query based on page-related entries
})
->orWhereHasMorph('menuable', [Category::class], function (Builder $query) {
// perform any query based on category-related entries
})
->with('menuable')
;
An alternative way is to pass both classes to the second argument. In that case you can check the type inside your closure.
$menus = Menu::whereActive(true)
->whereHasMorph('menuable', [Page::class, Category::class], function (Builder $query, $type) {
// perform any query independently of the morph-target
// $q->where...
if ($type === (new Page)->getMorphClass()) {
// perform any query based on category-related entries
}
if ($type === (new Category)->getMorphClass()) {
// perform any query based on category-related entries
}
})
->with('menuable')
If required you can also preload nested relationships. (https://laravel.com/docs/8.x/eloquent-relationships#nested-eager-loading-morphto-relationships)
$menus = Menu::whereActive(true)
->with(['menuable' => function (MorphTo $morphTo) {
$morphTo->morphWith([
Page::class => ['calendar'],
Category::class => ['tags'],
]);
}])
I am trying to build a Laravel Nova Filter for my Model "Question", based on data from two different tables. I have figured out the exact SQL query I would need to use, but cannot figure out how to use it in the apply() method of the Nova filter.
Table "questions" columns:
id, ...
Table "themes" columns:
id, question_id, theme_id
The SQL Query I am trying to use:
SELECT questions.*
From questions
Inner JOIN themes on questions.id=themes.question_id
WHERE themes.theme_id = 3 (value taken from the Nova Filter options)
My Filter in Laravel Nova QuestionsThemeFilter.php:
class QuestionsThemeFilter extends BooleanFilter
{
public $name = 'filter by theme';
public function apply(Request $request, $query, $value) {
// This is what I'm missing...
}
public function options(Request $request) {
$themes = Themes::all();
$themesArray = array();
foreach($themes as $item) {
$themesArray[$item['title']] = strval($item['id']);
}
return $themesArray;
}
}
So, how exactly should I do this?
Other question: Since I am a beginner in Laravel, how exactly should I start debugging this? Using dd() doesn't work (the filter just breaks) and I don't know how I can debug this. Is there a way to dump some values from the apply method (for example the $value)?
Thanks in advance for any help, highly appreciated :-D
Check the comments in the code
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
class QuestionsThemeFilter extends BooleanFilter
{
public function apply(Request $request, $query, $value) {
// Since you are using BooleanFilter $value will be an associate array with true/false value
$selectedThemes = Arr::where($value, function ($v, $k) {
return $v === true;
});
$selectedThemeIds = array_keys($selectedThemes)
// For debugging print some logs and check
Log::info($value)
Log::info($selectedThemeIds);
return $query->join('themes', 'questions.id', '=', 'themes.question_id')
->whereIn('themes.theme_id', $selectedThemeIds);
// If you are using normal filter then below code will be enough
/*
return $query->join('themes', 'questions.id', '=', 'themes.question_id')
->where('themes.theme_id', $value);
*/
}
public function options(Request $request) {
// You can use Laravel collection method pluck
return Themes::pluck('id', 'title')->all();
}
}
References:
Pluck
Boolean Filter
Arr::where
I am using Laravel. I am trying to relate user model and message model to many-many relation.
When I access a users message it is saying function not defined. But the that function is defined.
This is the error I am getting
Call to undefined method Illuminate\Database\Eloquent\Collection::messages()
User Model.
public function docs(){
return $this->belongsToMany('Doc');
}
public function messages(){
return $this->belongsToMany('Message');
}
Message Model.
public function users(){
return $this->belongsToMany('User');
}
I am trying to store message for selected users. This is where the error rises.
I have also set the pivot table.
Messages Controller.
public function store()
{
//
$input = Input::only('title', 'body', 'sel_users');
$msg = Input::only('title', 'body');
$sel_users = Input::only('sel_users');
$this->messageForm->validate($input);
$insert = Message::create($msg);
$id = $insert['id'];
foreach ($sel_users as $userid) {
$user = User::find($userid);
$user->messages()->attach($id);
}
return Redirect::route('messages.index');
}
Your problem is that userid in the loop is an array not single id:
foreach ($sel_users as $userid) {
$user = User::find($userid); // find() provided with array returns collection...
$user->messages()->attach($id); // .. so here you can't call messages() on that collection
}
// it works the same as:
// User::whereIn('id', $userid)->get();
This is because Input::only(...) returns array, and you must have had an array of ids in sel_users too, so:
$sel_users = Input::only('sel_users');
// $sel_users = array('sel_users' => array( id1, id2 ...) )
What you wanted here is this:
$sel_users = Input::get('sel_users');
// $sel_users = array( id1, id2 ...)
Then the rest of your code will work as expected.
I have a complex Model with multiple defined relations. In this example I would want to count the Like model and create a property named likes so it can be returned from a REST service.
Is it possible to eager load a model count into a dynamic property?
$beat = Post::with(
array(
'user',
'likes' => function($q){
$q->count();
}
))
->where('id', $id)
->first();
Assuming you are having Post->hasMany->Like relationship and you have declared likes relationship as:
class Post{
public function likes(){
return $this->hasMany('Like');
}
}
create a new function say likeCountRelation as:
public function likeCountRelation()
{
$a = $this->likes();
return $a->selectRaw($a->getForeignKey() . ', count(*) as count')->groupBy($a->getForeignKey());
}
now you can override __get() function as:
public function __get($attribute)
{
if (array_key_exists($attribute, $this->attributes)) {
return $this->attributes[$attribute];
}
switch ($attribute) {
case 'likesCount':
return $this->attributes[$attribute] = $this->likesCountRelation->first() ? $this->likesCountRelation->first()->count : 0;
break;
default:
return parent::__get($attribute);
}
}
or you can use getattribute function as :
public function getLikesCountAttribute(){
return $this->likesCountRelation->first() ? $this->likesCountRelation->first()->count : 0;
}
and simply access likesCount as $post->likesCount you can even eager load it like:
$posts=Post::with('likesCountRelation')->get();
foreach($post as $post){
$post->likesCount;
}
NOTE: Same logic can be used for morph many relationships.
You should use the SQL Group By statement in order to make it works. You can rewrite your query like the following one.
$beat = Post::with(
array(
'user',
'likes' => function($q) {
// The post_id foreign key is needed,
// so Eloquent could rearrange the relationship between them
$q->select( array(DB::raw("count(*) as like_count"), "post_id") )
->groupBy("post_id")
}
))
->where('id', $id)
->first();
The result of likes is a Collection object with one element. I'm assuming the relationship between model Post and Like is Post hasMany Like. So you can access the count like this.
$beat->likes->first()->like_count;
I'm not tested code above but it should works.
I need to get the id of a row based on the constraints of the parents. I would like to do this using eloquent and keep it elegant. Some things to note when this process starts:
I have - country_code(2 digit iso), lang_code(2 digit abbreviation for language)
i need - country_id, lang_id (primary keys)
so i can get - market_id (needed for last query)
I am able to retrieve the data I need with the following, sorry for the naming of the variables (client had weird names):
// Only receive desired inputs
$input_get = Input::only('marketCode','langCode');
// Need the country based on the "marketCode"
$countryId = Country::where('code',$input_get['marketCode'])->pluck('id');
// Get the lang_id from "langCode"
$languageId = Language::where('lang_abbr',$input_get['langCode'])->pluck('lang_id');
// Get the market_id from country_id and lang_id
$marketId = Market::where('country_id', $countryId)
->where('lang_id',$languageId)->pluck('market_id');
// Get All Market Translations for this market
$marketTranslation = MarketTranslation::where('market_id',$marketId)->lists('ml_val','ml_key');
I've tried the following, but this only eager loads the country and language based on the constraints. Eager Loading only seems to be helpful if the market_id is already known.
class Market extends Eloquent {
protected $primaryKey = 'market_id';
public function country() {
return $this->belongsTo('Country');
}
public function language(){
return $this->belongsTo('Language','lang_id');
}
}
$markets = Market::with(array(
'country' => function($query){
$query->where('code','EE');
},
'language'=> function($query){
$query->where('lang_abbr','et');
}
))->get();
You'd have to use joins in order to do that.
$market = Market::join( 'countries', 'countries.id', '=', 'markets.country_id' )
->join( 'languages', 'languages.id', '=', 'markets.language_id' )
->where( 'countries.code', '=', 'EE' )
->where( 'languages.lang_abbr', 'et' )
->first();
echo $market->id;
If this is something that happens frequently then I'd probably add a static method to the Market model.
// in class Market
public static function lookup_id( $country_code, $language_abbreviation ) { ... }
// then later
$market_id = Market::lookup_id( 'EE', 'et' );
So after looking at the relationships, I was able to get it working without the use of manual joins or queries, just the relationships defined in the ORM. It seems correct, in that it uses eager loading and filters the data needed in the collection.
// Get A country object that contains a collection of all markets that use this country code
$country = Country::getCountryByCountryCode('EE');
// Filter out the market in the collection that uses the language specified by langCode
$market = $country->markets->filter(function($market) {
if ($market->language->lang_abbr == 'et') {
return $market;
}
});
// Get the market_id from the market object
$marketId = $market->first()->market_id;
Where the models and relationships look like this:
class Country extends Eloquent {
public function markets() {
return $this->hasMany('Market')->with('language');
}
public static function getCountryByCountryCode($countryCode)
{
return Country::with('markets')->where('code',$countryCode)->first();
}
}
class Market extends Eloquent {
protected $primaryKey = 'market_id';
public function country() {
return $this->belongsTo('Country');
}
public function language(){
return $this->belongsTo('Language','lang_id');
}
}
class Language extends Eloquent {
protected $primaryKey = 'lang_id';
}