Laravel Selecting and Aliasing Columns in Eloquent’s query

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.

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.   Learn Laravel (Best Laravel Tutorials for Beginners)
  2.   PHP Technical Interview Questions and Answers
  3.   Internal server error with Laravel FIX
  4.   PHP GD Library Resize Images Manipulating
  5.   jQuery Validate Multiple File Image Upload validation
  6.   Simple JavaScript Form Validation Example
  7.   Laravel 6 Set Timestamps using Eloquent Model
  8.   How to Upload Images on Server in PHP?
  9.   Laravel Calculate multiple totals in conditional aggregates
  10.   Enable/Disable debug mode in cakephp