How to change the value in laravel 5 collection - php

I have some problems here.
I get a collection which like:
$VenderData=Vender::where('active', '=', '1')->get();
Inside the collection i have a column called 'type' and the data looks like 'A1,A2,A3,'
or
'A1,A3,'
I want to transfer those codes to real names from
another table 'vender_type'.
code name
A1 XX
A2 OO
A3 ZZ
then add a new column into the original collection $VenderData.
How can i do this?

You must split the type attribute (using array explode(string $delimiter , string $string)) , and get the name of each one from vender_type table, try this :
$VenderData = Vender::where('active', '=', '1')->get();
foreach($VenderData as $vend)
{
$vend->new_type = array();
$types = explode(",", $vend->type);
foreach($types as $type)
{
$t = vender_type::where('code', $type)->first();
if($t)
$vend->name_type[] = $t->name;
// or for example : $vend->name_type[$type] = $t->name;
}
}
I hope that will help you.

I think that you need better query than modifying each value after. This vender_type is probably some foreign key in you database so you can get that value in query builder. To find out about foreign keys and joins in laravel eloquent check this.
Basically you need something like this
$users = DB::table('table1')
->join('table2', 'table1.vender_type', '=', 'table2.vender_type')
->select('table1.code', 'table1.name', 'table2.vender_type')
->where('table1.active', '=', '1')
->get();
Just replace table1 and table2 with values of you table names in database and you're done.
Hope it helps

Related

Laravel eloquent update column using relationship column

How can achieve this query?
Sale::with(['catalog'])
->whereIn('id', $ids)
->update(['price' => DB::raw('catalog.price')]);
This is not working, it shows undefined table... I tried to put the name of the table but it's the same.
On the internet I always found the easy query:
Sale::with(['catalog'])
->whereIn('id', $ids)
->update(['price' => 5]);
Okay! When I want to update all rows with the same value is easy, in addition is easy when you want to update with a column of the same table like:
Sale::with(['catalog'])
->whereIn('id', $ids)
->update(['price' => DB::raw('price_alternative')]);
But how about using a column of another table with a relationship? I haven't found the solution.
I know this can be solved using entire raw query, but I wanted to know if it can be achieved by the eloquent way
You probably need to join in the catalog table on your querybuilder. This is different than using with(). Something along the lines of
Sale::whereIn('id', $ids)
->join('catalog', 'sales.catalog_id', '=', 'catalog.id')
->update(['price' => DB::raw('catalog.price')]);
This is not better than answer of #Qirel, but it is Eloquent way, i like this because that's more clearly.
$Sales = Sale::whereIn('sales.id', $ids)
->with('Cat')->get();
$Sales->map(function($q){
$q->price = $q->Cat->price;
$q->save();
});
Assume you have this relation code in Sale model:
public function Cat()
{
return $this->hasOne(CatModel::class, 'id', 'catalog_id');
}

How to fetch data in laravel 5.4 with out using array_push

its there's a way to optimize this code. I already google it but I don't know what the exact keyword to search, so I always failed to find the answer.
At this code I get the Approver List of ID 512 (requestor)
$approver_list = DB::table('users')
->leftjoin('approver_group_list', 'approver_group_list.user_id', '=', 'users.id')
->leftjoin('approval_roles', 'approval_roles.as_id', '=', 'approver_group_list.as_id')
->leftjoin('approver_requestor_list', 'approver_requestor_list.at_id', '=', 'approval_roles.at_id')
->where('approver_requestor_list.user_id', 512)
->get();
Then I use array_push to extract the data of approver_list, then I use the value of $result to get value in LeaveMain table.
$result = array();
foreach($approver_list as $al)
{
array_push($result , $al->user_id);
}
$leave_list = LeaveMain::whereIn('requestor_id', $result)->get();
My problem is, it is always need to use array_push to to extract data, or laravel have a way to optimize this code.
$approver_list = DB::table('users')
->leftjoin('approver_group_list', 'approver_group_list.user_id', '=', 'users.id')
->leftjoin('approval_roles', 'approval_roles.as_id', '=', 'approver_group_list.as_id')
->leftjoin('approver_requestor_list', 'approver_requestor_list.at_id', '=', 'approval_roles.at_id')
->where('approver_requestor_list.user_id', 512)
->pluck('id');
$leave_list = LeaveMain::whereIn('requestor_id', $approver_list)->get();
Basically you can pluck the id from the table itself, rather than fetching all the data and then taking the id later, and whereIn accepts collection as the second argument. so no need to cast to an array
The whereIn method filters the collection by a given key / value
contained within the given array: Link
You don't need to loop the approver_list values, you can use pluck method to retrieves all of the values for user_id.
$result = $approver_list->pluck('user_id')->toArray();
$leave_list = LeaveMain::whereIn('requestor_id', $result)->get();

Laravel 5 - Change all values in a Column of a table

I have a table with column called "estado" and I want change all the column in one shot.
This is the right method? The faster one?
$vendedor = Vendedor::with('produtos')->find( $idVendedor );
foreach ( $vendedor->produtos as $produto ) {
$produto->pivot->estado = $novoEstado;
};
The column what I want change is "estado". There's a way to do it without the foreach?
Update Specific column you want to upadate using Laravel eloquent.
Way -> 1
[Status-> Column you want to update]
Model::where('status', '=', 1)->update(['status' => 0]);
Way -> 2
[Status-> Column you want to update]
$allData = Model::where('status', 1)->get();
foreach ($allData as $data){
$data->status = 0;
$data->update();
}
The simplest would be to just use the query builder for this (instead of your model)
DB::table('table_name')->where('vendedor_id', $idVendedor)->update('estado', $novoEstado);

Get all data of foreign key in laravel

Currently i am doing this to get branch table data:
$smlist = SM::where('branch_id','=',$branchid)->select('id','name','branch_id')->get();
foreach ($smlist as $sm) {
$sm->b = SM::find($sm->id)->branch;
}
Where branch_id is foreign key, also I set belongsTo in SM table.
This is working fine for me but I am finding way to use it with in single query.
How can i get this data using single query?
You can eager load your relationship using with():
$smlist = SM::with('branch')
->where('branch_id','=',$branchid)
->select('id','name','branch_id')
->get();
You have to use a join, here you can find the documentation http://laravel.com/docs/4.2/queries#joins
I think you can use something like this:
SM::->join('branch', 'branch.id', '=', 'sm.branch_id')
.where('sm.branch_id','=',$branchid)
->select('sm.id','sm.name','sm.branch_id','branch.name')
->get();

How to 'order_by' on second table when using eloquent one-to-many

Of course I can use order_by with columns in my first table but not with columns on second table because results are partial.
If I use 'join' everything works perfect but I need to achieve this in eloquent. Am I doing something wrong?
This is an example:
//with join
$data = DB::table('odt')
->join('hdt', 'odt.id', '=', 'hdt.odt_id')
->order_by('hdt.servicio')
->get(array('odt.odt as odt','hdt.servicio as servicio'));
foreach($data as $v){
echo $v->odt.' - '.$v->servicio.'<br>';
}
echo '<br><br>';
//with eloquent
$data = Odt::get();
foreach($data as $odt){
foreach($odt->hdt()->order_by('servicio')->get() as $hdt){
echo $odt->odt.' - '.$hdt->servicio.'<br>';
}
}
In your model you will need to explicitly tell the relation to sort by that field.
So in your odt model add this:
public function hdt() {
return $this->has_many('hdt')->order_by('servicio', 'ASC');
}
This will allow the second table to be sorted when using this relation, and you wont need the order_by line in your Fluent join statement.
I would advise against including the order by in the relational method as codivist suggested. The method you had laid is functionally identical to codivist suggestion.
The difference between the two solutions is that in the first, you are ordering odt ( all results ) by hdt.servicio. In the second you are retrieving odt in it's natural order, then ordering each odt's contained hdt by servico.
The second solution is also much less efficient because you are making one query to pull all odt, then an additional query for each odt to pull it's hdts. Check the profiler. Considering your initial query and that you are only retrieving one column, would something like this work?
HDT::where( 'odt_id', '>', 0 )->order_by( 'servico' )->get('servico');
Now I see it was something simple! I have to do the query on the second table and get contents of the first table using the function odt() witch establish the relation "belongs_to"
//solution
$data = Hdt::order_by('servicio')->get();
foreach($data as $hdt){
echo $hdt->odt->odt.' - '.$hdt->servicio.'<br>';
}
The simple answer is:
$data = Odt::join('hdt', 'odt.id', '=', 'hdt.odt_id')
->order_by('hdt.servicio')
->get(array('odt.odt as odt','hdt.servicio as servicio'));
Anything you can do with Fluent you can also do with Eloquent. If your goal is to retrieve hdts with their odts tho, I would recommend the inverse query for improved readability:
$data = Hdt::join('odt', 'odt.id', '=', 'hdt.odt_id')
->order_by('hdt.servicio')
->get(array('hdt.servicio as servicio', 'odt.odt as odt'));
Both of these do exactly the same.
To explain why this works:
Whenever you call static methods like Posts::where(...), Eloquent will return a Fluent query for you, exactly the same as DB::table('posts')->where(...). This gives you flexibility to build whichever queries you like. Here's an example:
// Retrieves last 10 posts by Johnny within Laravel category
$posts = Posts::join('authors', 'authors.id', '=', 'posts.author_id')
->join('categories', 'categories.id', '=', 'posts.category_id')
->where('authors.username', '=', 'johnny')
->where('categories.name', '=', 'laravel')
->order_by('posts.created_at', 'DESC')
->take(10)
->get('posts.*');

Categories