Laravel Selecting and Aliasing Columns in Eloquent’s query

Laravel Mysql Mysqli php Programming Technology

Today, We want to share with you Laravel Selecting and Aliasing Columns in Eloquent’s query.In this post we will show you laravel 6 eloquent select column as, hear for laravel 6 query builder alias column we will give you demo and example for implement.In this post, we will learn about Laravel 6 Multiple Eloquent How to select column as with an example.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

Laravel Selecting and Aliasing Columns in Eloquent’s query

There are the Following The simple About Alias Columns on Related Models in Laravel Full Information With Example and source code.

As I will cover this Post with live Working example to develop Aliasing columns in Eloquent’s query builder, so the How to alias a table in Laravel Eloquent queries (or using Query Builder) is used for this example is following below.

Laravel 6 column alias Examples

Products::where("status", "=", true)
    ->joinWithCategories
    ->get(['categories.name AS category_name', 'products.*'])
    ->toArray();

Laravel Selecting and Aliasing Columns in Eloquent's query
Laravel Selecting and Aliasing Columns in Eloquent’s query

How to alias the name of a column in Laravel Eloquent

Laravel Be careful to argument set your bindings to the other parameter of simple use selectRaw(). From the docs:

$products = DB::table('products')
->selectRaw('price * ? as price_with_tax', [12.0758])
->get();

Alias Columns on Related Models in Laravel 6

Standard.php

class Standard extends Eloquent {

	protected $table = 'standards';
	protected $fillable = array();
	protected $guarded = array('id');
	
	public function student()
	{
		return $this->belongsTo('Student');
	}
	
	public function giftPrice()
	{
		return $this->hasMany('GiftPrice');
	}
}

GiftPrice.php

class GiftPrice extends Eloquent {

	protected $table = 'giftPrice';
	protected $fillable = array('title', 'qty', 'product_id', 'cart_id');
	protected $guarded = array('id');
	
	public function standard()
	{
		return $this->belongsTo('Standard');
	}

}	

  public function giftPrice()
    {
        return $this->hasMany('GiftPrice')->select(['id', 'range', 'qty', 'title', '(range * qty) as itemTotal' ]);
    }

 public function giftPrice()
    {
        return $this->hasMany('GiftPrice')->select(['id', 'range', 'qty', 'title', DB::raw('(range * qty) as itemTotal') ]);
    }

Standard.php

	class Standard extends Eloquent {
	
		protected $table = 'standards';
		protected $fillable = array();
		protected $guarded = array('id');
		
		public function student()
		{
			return $this->belongsTo('Student');
		}
		
		public function giftPrice()
		{
                        return $this->hasMany('GiftPrice')->select(['id', 'range', 'qty', 'title', DB::raw('(range * qty) as itemTotal') ]);
		}
	}	

Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about rename column in query laravel 6.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   PHP Laravel MySQL Joins Tutorial with Examples
  2.   PHP Laravel 7 Inner Join Query Example (join query in laravel)
  3.   Laravel 6 Inner Join Query Example - MySQL
  4.   Laravel 5.8 Global Blade view variable
  5.   PHP Dynamically generate QR code Library
  6.   jQuery Functions Not Working after PostBack in Asp.Net
  7.   Multiple file Upload in Laravel 5.7 with validation
  8.   Free Google Syntax Highlighter in wordpress
  9.   Laravel 6 Group By Timestamps using Query Builder
  10.   Laravel Calculate multiple totals in conditional aggregates

Leave a Reply

Your email address will not be published. Required fields are marked *