My structure is as simple as
One user -> has many notifications
so to fetch the notifications of the logged in user
I type this on the controller
$notifications = $this->getUser()->getNotifications();
Now I need to paginate the results but since this won't work
$notifications = $this->getUser()->getNotifications()->setMaxResults(2)..
I guess I need to use a createQuery to fetch the results?
what query would be the DQL equivalent of
"$this->getUser()->getNotifications()" ?
You can't limit . Try slice
If you have Doctrine 2.1 you can use ->slice() on the collection:
$notifications = $this->getUser()->getNotifications();
$result = $notification->slice(0, 2);
Related
I Want to select all the calls from the incoming_calls table and the outgoing_calls table together. Basically i am creating api and i want to get the data from these two tables and to paginate them.
How can i do that? Anyone!
Thanks in advance!
So, about your case, you'll need to create pagination manually (more info [FR]), so collection should have the same structure.
This code can do the thing.
$calls = IncomingCall::all()->merge(OutgoingCall::all());
//creating of pagination
$page = \Illuminate\Support\Facades\Input::get('page', 1);
$perPage = 10;
$calls = new \Illuminate\Pagination\Paginator(
$calls->slice(($page - 1) * $perPage),
$perPage,
$page,
[
'path' => \Illuminate\Pagination\Paginator::resolveCurrentPath()
]
);
Do the two tables have a field in common? Or do you want the two results separate?
To select data and paginate simply use
$users = DB::table('users')->paginate(15);
See Laravel Docs or you can also use Laravel Eloquent
I'm new to laravel, so I'm searching for how to send a specific id to the controller in order to get data from another table ?
For example
while($shipment = sqlsrv_fetch_array($get_customer_rule,SQLSRV_FETCH_ASSOC)) {
//display some data ......
// inside the loop i will have another query to get data from another table has //relation with shipment table
$id = $shipment['id'];
$customer = "SELECT * FROM [ccctadm].[customer] WHERE id = '$id' ";
$get_customer_info = sqlsrv_query($conn, $customer);
$get_customer_id = sqlsrv_fetch_array($get_customer_info,SQLSRV_FETCH_ASSOC);
$customer_id = $get_customer_id['customerid'];
}
I can't write query in while loop in laravel, so how can I pass shipment id to the controller so I can get customer data related to the shipment
Since you are new to Laravel, maybe you should learn the Laravel way first. Watch this video on how to work with Eloquent and perhaps every other video in that series. https://laracasts.com/series/laravel-from-scratch-2017/episodes/7
Once you get your head around that, you will be able to rewrite your query as
$shipments = Shipement::all();
foreach( $shipments as $shipment ) {
$customer = $shipment->customer;
$customer_id = $customer->id;
}
Even better when you get a bit further with laravel and be able to work with eager loading, you will just do
$shipments = Shipment::with('customer')->get();
And in your view
#foreach($shipments as $shipment)
Customer ID is : {{ $shipment->customer->id }}
#endforeach
You have decided you to work with Laravel. Take advantage of it. It will make everything easier and speed up your development process.
If you want to stick to your raw SQL queries, you can use the query builder
$result = DB::select("SELECT * FROM [ccctadm].[customer] WHERE id = ?", [$id]);
And work with the result
i am using laravel 5.1 and want to retrieve a single row in the database then manipulate it after.
my current code is
$profiles = Profile::where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)
->get();
foreach($profiles as $profile ){
$data['address'] = $profile->address;
}
why cant i do it like this?
$profiles = Profile::where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)
->get();
$data['address'] = $profiles->address;
am i using a wrong function or something?
thanks in advance.
Try this:
$data['address'] = $profiles[0]->address;
When you are using get(), it returns an array of Std class object.
In addition to retrieving all of the records for a given table, you may also retrieve single records using first. Instead of returning a collection of models, these methods return a single model instance:
// Retrieve the first model matching the query constraints...
$flight = App\Flight::where('active', 1)->first();
laravel 5.8
Retrieving A Single Row / Column From Profile
If you just need to retrieve a single row from the database table, you may use the first() method. This method will return a single stdClass object:
$Profile = DB::table('Profiles')->where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)->first();
If you don't even need an entire row, you may extract a single value from a record using the value() method. This method will return the value of the column directly:
$address = DB::table('Profiles')->where('user_id', $this->user->user_id)
->where('profile_code', $this->user->profile_code)->value('address');
I have this code in a laravel project
$state_query = Advertisement::active()
->notExpired()
->inTime()
->network($network)
->textbook($textbook, $textbook_category_id, $book_category_id)
->grade($grade)
->state($state_id);
$city_query = Advertisement::active()
->notExpired()
->inTime()
->network($network)
->textbook($textbook, $textbook_category_id, $book_category_id)
->grade($grade)
->city($city_id);
$district_query = Advertisement::active()
->notExpired()
->inTime()
->network($network)
->textbook($textbook, $textbook_category_id, $book_category_id)
->grade($grade)
->district($district_id);
$result = $state_query
->union($city_query)
->union($district_query);
now, I want to make "$result" variable as a database table and use "where" and "sum" eloquent functions on it
how can I do that?
Simply make a collection :
https://laravel.com/docs/5.3/collections
The results of Eloquent queries are always returned as Collection instances.
I have a problem with the toArray() method in Doctrine. Its doesn't get my relations:
First query :
$q = Doctrine::getTable('posts')->find(1);
debug($q->toArray(true));
Print the postid=1 with out the relations
$q = Doctrine::getTable('posts')->find(1);
$q->Tags->toArray();
debug($q->toArray(true));
...prints the results with tag relation.
But i want to do:
Doctrine::getTable('posts')->findAll()->toArray(true);
...and get all of relations of posts , instead I got an array of post row.
Any idea about how to make it work with the relations?
(notice i added toArray(true) for deep property.
thanks for any help
You could create named query for this table with all relations attached:
Doctrine::getTable('posts')->addNamedQuery('get.by.id.with.relations', 'DQL here...');
And then just use something like this:
Doctrine::getTable('posts')->find('get.by.id.with.relations', array(123));
I beleive you need to do a Join with the query. Otherwise it doesnt hydrate the realated data.
$q = Doctrine_Query::create()
->from('Post p')
->leftJoin('p.RelatedModel1 rm1')
->leftJoin('p.RelatedModel2 rm2');
$q->findAll()->toArray(true);
$q = Doctrine_Query::create()
->from('Post p')
->leftJoin('p.RelatedModel1 rm1')
->leftJoin('p.RelatedModel2 rm2');
$q->findAll()->toArray(true);
Can i Add ->limit()->offset()
to the query ?
I guss that if i first create the query then findAll will act the same as execute right ?