Undefined index: in laravel 4 - laravel

this is my code ..hope you guys help me because i want to get the value of country['CC_FIPS'] to use in my where statement in city
$local = Input::get('locality');
$country = Input::get('country');
$resultCountry= Country::where('COUNTRY_NAME', '=', $country)->get();
//echo $resultCountry;
$resultCity= City::where('FULL_NAME_ND', '=', $local)
->where('CC_FIPS', '=', $resultCountry['CC_FIPS'])
->get();
}
echo $resultCity;

Problem is that you array $resultCountry does not have any element with index CC_FIPS.
You can see what elements are in this array if you use function dd() as
dd($resultCountry);

$local = Input::get('locality');
$country = Input::get('country');
$resultCountry= Country::where('COUNTRY_NAME', '=', $country)->first()->toArray();
//echo $resultCountry;
$resultCity= City::where('FULL_NAME_ND', '=', $local)
->where('CC_FIPS', '=', $resultCountry['CC_FIPS'])
->get();
}
print_r($resultCity);
Instead of get use first and keep the rest t=of the code as it is.. Hope this helps..

$local = Input::get('locality');
$country = Input::get('country');
$resultCountry= Country::where('COUNTRY_NAME', '=', $country)->first();
//echo $resultCountry;
$resultCity= City::where('FULL_NAME_ND', '=', $local)
->where('CC_FIPS', '=', $resultCountry['CC_FIPS'])
->get();
}
print_r($resultCity);
When Using Get in your first query The result will be returned something like this..
[
[0]=>[
]
]
So instead of Get Use first which will get only one array
Also on the second query you cannot echo the result you need to print_r it

You can convert $resultCountry to an array like this :
$resultCountry= Country::where('COUNTRY_NAME', '=', $country)->get()->toArray();
then $resultCountry['CC_FIPS'] should return your value properly.

Related

laravel does not exist on this collection instance

$orders = Orders::select('orders.id as order_id', 'collection_color.color_name as color', 'collection_color.id as collection_color_id', DB::raw('SUM(order_piece.piece) As piece'))
->join('order_piece', 'order_piece.order_id', '=', 'orders.id')
->join('collection_color_size_barcode', 'collection_color_size_barcode.id', '=', 'order_piece.collection_color_size_barcode_id')
->join('collection_color', 'collection_color.id', '=', 'collection_color_size_barcode.collection_color_id')
->whereIn('orders.id', $request->order_id)
->groupBy('order_piece.order_id')
->orderBY('orders.delivery_date', 'ASC')
->get();
return $orders; => [{"order_id":30,"color":"Kahverengi","collection_color_id":21,"piece":"500"}]
return $ccfc = CollectionColorFabricColor::whereIn('collection_color_id', $orders->collection_color_id)->get();
Property [collection_color_id] does not exist on this collection instance. i am getting error can you help me
The error is most likely due to this in your second code snippet: $orders->collection_color_id. $orders is a collection, so the property doesn't exist in that object. What you actually need is to pluck those values from that collection like so:
return $ccfc = CollectionColorFabricColor::query()
->whereIn('collection_color_id', $orders->pluck('collection_color_id'))
->get();
Because your $orders is collection, you need to get collection_color_id array like this :
$arrayColors = $orders->pluck('collection_color_id')->toArray();
then
return $ccfc = CollectionColorFabricColor::whereIn('collection_color_id', $arrayColors)->get();

Laravel eloquent Query Reuse extra condition

I have a query which gives me list of total counts of different items, as:
$data = DB::table($Table1)
->join($table2,$Table1.'.id','=',$Table2.'.device_key')
->where($Table1.'.created_at', '>', $value)
->select('item', DB::raw('count(*) as total'))
->groupBy('item')
->lists('total', 'item');
now i want to fetch same data with extra condition as >where($Table1.'.status', '=', 'SUCCESS') .
how do i do that ??
I don't think you'll get away with anything nice than this.
$query = DB::table($Table1)
->join($table2,$Table1.'.id','=',$Table2.'.device_key')
->where($Table1.'.created_at', '>', $value)
->select('item', DB::raw('count(*) as total'))
->groupBy('item');
$data1 = $query->lists('total', 'rem');
$data2 = $query->where($Table1 . '.status', '=', 'SUCCESS')->lists('total, 'rem');
Use clone to copy the query object for modifications.
$query = DB::table()->where();
$clonedQuery = clone $query;
$clonedQuery->where(..);

laravel join query is not working

I have the next query and I want to get the price_types.name but is not returned:
$projects = Project::with('projectsTask')
->select('projects.*',
'price_types.name as name_type'
)
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get();
Here an image query is retrievng
This on picture "type_list" must be string text
Maybe somebody can help me.
Many thanks!
Try this:
$projects = Project::with('projectsTask')
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get([''projects.*',
'price_types.name as name_type'']);
get method receive as parameter an array with fields that you want.
$projects = Project::join('tasks', 'projects.id', '=', 'tasks.project_id')
->select('tasks.*',
'price_types.name as name_type',
'statuses.name as name_status'
)
->where([['client_id', $client->id], ['tasks.status_type', '!=', 2]])
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->join('statuses', 'tasks.status_type', '=', 'statuses.type')
->orderBy('tasks.id', 'DESC')
->get();

Why does this return only one result?

I have the following query. The query should be returning all five rows that I have where user_id = 1, but it's only returning the first result. Why?
$servers = DB::table('posts as p')
->select('p.id', 'p.content', DB::raw('COUNT(i.id) as num_results'))
->leftJoin('images as i', 'i.post_id', '=', 'p.id')
->where('p.user_id', Auth::id())
->get();
Thank you.
public function index(User $user)
{
$users = $user
->where('rol','2')//for normal user
->leftjoin('cityes', 'cityes.id', '=', 'users.cityes_id')
->select('users.id', 'users.name','cityes.name as city')
->get();
}
this code is work for me try this way

LIKE doesn't return results

I am trying to find "sam" inside FullName in the table users with this eloquent operation:
$user = User::where('id', '=', $input)->orWhere('fullName', 'LIKE', "%$input%")->find(10);
finding through id works as expected but the Where with LIKE isn't returning any results.
(if $input is 1 the first where returns a result but if $input is sam the second where doesn't return anything)
In the database fullName has the value "Sam Pettersson".
Anything I am doing wrong?
For some reason laravel doesn't want find() and like queries in the same query so using this instead worked:
$user = User::where('id', '=', $input)->orWhere('fullName', 'LIKE', "%$input%")->take(10)->get();
$user = User::where(function($query) use($input)
{
$query->where('id', '=', $input)
->orWhere('fullName','LIKE', '%'.$input.'%');
})->get();
This worked for me. Hope this work for you.

Resources