Posted inTechnology / Laravel / Mysql / Mysqli / php / Programming

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.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

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

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype