why when i select all data is showing but when i use find or where data not showing even if i check with dd - laravel

some make me confusing in laravel when i use all() data is showing but when i select find or where data not showing when i use dd function in laravel
//$regency = Regency::find(1101);
//$regency = Regency::where('number','=',$prov_id);
$regency = DB::table('regencies')->where('province_id', $prov_id);
dd($regency);
i try use eloquent and query builder but still same result.
those is result in dd if i using query builder, but why when i use select all data is showing

What you are showing is the Query Builder you need to execute it with ->get() to get the Collection.

You have to get the data from the query builder in order to receive the results as a collection of objects
// $regency = Regency::findOrFail(1101);
// $regency = Regency::where('number', $prov_id)->get();
$regency = DB::table('regencies')->where('province_id', $prov_id)->get();
dd($regency);
Writing a query builder just produces the SQL query but doesn't actually execute/run it
Hope this helps

Related

How to optimize the following query Laravel?

I have the code which is working perfectly fine.
$amenityCategoryMapping1 = AmenityCategoryMapping::where('property_id', $property->id)
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping2 = AmenityCategoryMapping::where('company_id', $property->company_id)
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping3 = AmenityCategoryMapping::whereNull('property_id')
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();
$amenityCategoryMapping = array_merge($amenityCategoryMapping3, $amenityCategoryMapping2, $amenityCategoryMapping1 );
However, currently I am executing 3 different query to extract data from the same table just using different parameters. Is there any way to minimize this query requests and still receiving the same results?
Currently, while plucking if two array has the same key then I am replacing amenityCategoryMapping3 by amenityCategoryMapping2 and that by amenityCategoryMapping1. That is more priority should be given to amenityCategoryMapping1
Try using orWhere function
$amenityCategoryMapping1 = AmenityCategoryMapping::where('property_id', $property->id)
->orWhere('company_id', $property->company_id)
->orWhereNull('property_id')
->orderBy('amenity_name','asc')
->orderBy('updated_at','desc')
->pluck('category_id', 'amenity_name')->toArray();

Laravel Query Result Issue: query is returning empty array even there is data exist into database

I am working on laravel 5.4. I am trying to get data from database using below query:
$obj = \Illuminate\Support\Facades\DB::table('A')
->join('B', 'A.Intake', '=', 'B.Id')
->join('C', 'B.StreamId', '=', 'C.StreamId')
->select(\Illuminate\Support\Facades\DB::raw('DISTINCT(C.StreamId) As StreamId'))
->where('A.YearId', $yearId);
$streamIds = $obj->get()->toArray();
The above query is returning empty results. I have also debug the query generated by laravel. Below is the raw query generating by laravel on the basis of above conditions:
select DISTINCT(C.StreamId) As StreamId from A
inner join B on A.Intake = B.CentreStreamId
inner join C on B.StreamId = C.StreamId
where A.YearId = '12'
When I run the above raw query directly in my database, then it returns me some records. But when I try to get records in laravel then it returns me empty results.
I am not able to get the issue with the above query. Can someone please tell me why it is returning empty results set? If there is any issue with above query builder syntax then please correct my query.
Thanks in Advance.

Where Is My Raw Query?

Here is my controller code;
$temp_table_data = $temp_table
->setTempTable($generated_temp_table)
->newQuery()
->with(['payment' => function ($query) use ($column_values) {
$query->select($column_values);
}])->get();
My toSql query is right below it;
$sql = str_replace(['%', '?'], ['%%', "'%s'"], $temp_table->toSql());
$fullSql = vsprintf($sql, $temp_table->getBindings());
print_r($fullSql);
My code prints out;
select * from `selected_postcodes_1434968225_1`
Where are the details of the payments information that I am "with"ing? If I want to print out the raw query now, to show another developer, to get some help, what am I supposed to do here?
The eager loaded relationships are fetched in a separate query. You can use DB::getQueryLog() to get all run queries. Note that you have to enable it first using with DB::enableQueryLog().
Another alternative is the Laravel Debugbar package that shows you all queries and much more.

How to combine and & or operators in grocery crud

I am using codeigniter and grocerycrud, I want to set a table with grocerycrud in this way:
SELECT * FROM `dashboard`.`medidas_ludlum`
where FK_ludlum='190.26.88.131'
and date>= '2014-03-04 09:40:00'
and date<= '2014-03-05 09:40:00'
and (error_code=1 or audio_status=1);
I tried to do that in this way:
$uno='1';
$crud2 = new grocery_CRUD();
$crud2->set_theme('datatables');
$crud2->where('FK_ludlum',$ludlum_id);
$crud2->where('date>=',$fechainicio);$crud2->where('date<=',$fechafin);
$crud2->where('error_code =',$uno);
$crud2->or_where('audio_status =',$uno);
$crud2->set_table('medidas_ludlum');
$crud2->columns('measurement', 'fecha', 'audio_status', 'high_alarm_status', 'low_alarm_status','over_range_status','monitor_status','error_code');
$crud2->set_language("spanish");
$crud2->unset_add();$crud2->unset_delete();$crud2->unset_edit();$crud2->unset_read();
$data['crud2'] = $crud2->render();
However it's not giving the right results, for example I am getting rows that have a date out of the range, is there a way to get that CRUD set up?
Grocery curd is also using codeigniter's active record so you can write your where() with grouped conditions as below,no need to use or_where() function
$crud2->where('(error_code ='.$uno.' OR audio_status ='.$uno.')',null,FALSE);
or
$crud2->where('(error_code =1 OR audio_status =1)',null,FALSE);
The problem in your query is when you use or_where() function it produces where clause as and error_code=1 or audio_status=1 and using approach that i have provide will give you the where clause as and (error_code=1 or audio_status=1) a grouped condition
API and Functions list

Using Laravel's toSql on queries using 'with' clause

I'm working in Laravel and I'm interested in checking the SQL statements generated by a Eloquent query that includes a with() statement. For some reason I'm getting only the main query. For example, when I run
class Child extends EloquentVersioned {
public function childRequests()
{
return $this->hasMany('ChildRequest');
}
}
$childQuery = Child::orderBy('last_name')->orderBy('first_name')->with( 'childRequests');
return $childQuery->toSql();
I get back:
select `children`.* from `children` order by `last_name` asc, `first_name` asc
How do I get back the SQL for the with('childRequests') query?
Actually, when using with then Laravel uses another query for that so you are not getting that query output but if you use DB::getQueryLog() then you'll get all the query logs and to get your log you may run the actual query, for example:
Child::orderBy('last_name')->orderBy('first_name')->with( 'childRequests')->get();
Now try this:
dd(DB::getQueryLog()); // an array of all queries
You'll get an output of your queries and you may find the last query using:
$queries = DB::getQueryLog();
dd(end($queries)); // only last query
Troubleshooting
If you get no results on DB::getQueryLog() then the Query Logger may be disabled at all and you have to use DB::enableQueryLog() before.

Resources