I have a table named advertisments which has views column.
I want to fetch the data from advertisments table and increment each row's views in a single query is it possible ?.
The following code is what I wrote to achieve the desired result but the views aren't updating.
Advertisement::with('sponsor')
->whereDeleted(false)
->whereHeader(false)
->orderBy('views')
->raw("update views ".DB::raw('views + 1'))
->get();
Related
Here, is my eloquent
$contacts = Contact::where('property_id',$commercial->id)->where('property_type','commercial_lease')
->with('contact_log')
->with('user_name')
->with('contact_log.contact_log_name')
->get();
->with('contact_log')
with this relationship...I got each and every data from that table using ->with('contact_log'). But i want last inserted data via with relationship.
Others will remains same. But need to get only last data via this relation.
You need to apply extra constraints on your contact_log relation in order to fetch only the last entry.
Replace
->with('contact_log')
with
->with('contact_log', function($query) {
// order latest entries first
$query->orderBy('created_at', 'DESC');
// take only the first entry
$query->take(1);
})
I am trying to build a coupon site in Laravel. Each merchant has their own deals/coupons. I have been able to print deals/coupons for a merchant on their specific pages.
Here's my query
$deals = DB::table('deals')
-> join ('merchants', 'deals.merchant_id', '=', 'merchants.merchant_id')
-> where ('merchant_url_text', $merchant_url_text)
-> get();
So far so good.
Now this is where it starts getting complex.
Each deal has 2 more pieces associated with it. Click counts and Votes associated with deals.
The click counts are in a table called clicks which records each click on the website. The click record will have a click id associated it. So I would need to get a count of clicks each deal gets.
The second piece is votes. The votes around a deal are stored in a deal_votes table. The deal_votes table has deal_id, vote (1 or 0)
How do I combine click counts and deal votes to return in the same query so that I can display the info in my view?
Do you have models and relationships set up for merchants, deals, coupons, and clicks? This is trivial if you use Eloquent models with relationships, for which the docs are here: https://laravel.com/docs/5.2/eloquent-relationships
This would look like:
$merchant = Merchant::where('merchant_url_text', $merchant_url_text)
->with('deals','deals.votes','deals.clicks')
->first();
The with() function adds all of the nested information, ie query joins, into a single query.
In your view:
#foreach($merchant->deals as $deal)
Deal: {{$deal->name}}
Clicks: {{count($deal->clicks)}}
Votes: {{$deal->votes->sum('vote')}}
#endforeach
I need to skip the first n records, say 5 from a table and paginate the rest of it. The following is the code that i am using.
$skipNewsListArr = array(1,2,3,4,5);
$wrongPaginationLinks = DB::table('news')
->whereNotIn('news_id',$skipNewsListArr)
->orderBy('news_date','desc')
->paginate(5);
But the above code outputs the pagination links corresponding to the whole table. How can i achieve pagination by skipping records based on some condition.
I have a doubt doing pagination in CakePHP.
Well, I will try to be concise.
I have three tables, one table is which I paginate (1st table), another table (2nd table) dependent of this one (1st table have FK to 2nd table), and the most important, 3rd table, that has FK to 1st table. So: 3rd table-> 1st table-> 2nd table.
I do pagination of 1st table (model) from his controller, so far everything ok, the pagination list all the records from table bd and no problems.
I need also to get records from 1st table but ordering by a record from 2nd table. No problem. The problem IS: I WANT not to get in pagination, records from 1st table (which i paginate it) that arent present in 3rd table.
The problem is that CAKEPHP first make a query getting records from 1st table + 2nd table. After this, it get the records from 3rd table according to the records (PK) obtained from the first query, so I cant say it: not take records from 1st table that arent present in 3rd table.
¿How can I do this?
I hope I explained.
Thank you very much.
Regards.
what you'll want to use is a Counter Cache. Go to http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html and search for 'counterCache,' and you'll find instructions.
Basically, you want to implement a "counter cache" field in model 1, that automatically keeps count of the number of linked records in model 3. Then, to exclude records from Model 1 that have no related records in Model 3, you just add a pagination condition to say that the counter cache field must be greater than 0.
UPDATE:
If you can't use CakePHP's counter cache, then you should emulate the behaviour of the counter cache using a virtual field - see http://book.cakephp.org/2.0/en/models/virtual-fields.html
I have some problem while listing categories from database.
First i have a table called "Videos" where i store som videos-information like v_name, v_description and category_name.
In the second table called "Categories" where i store categories-information like c_name and c_description.
OFC i have id's in every table :)
But now i want to list the categories and in the same query count every videoitem in every category.
This is the code and i can't figure out how to do in the model now and later how to show the numbers in the view file, so pleace help me!
Thanks for your time and support :D
$this->db->select('c.*');
$this->db->from('categories as c');
$this->db->join('videos as v', 'c.c_name = v.v_category', 'right');
return $this->db->get()->result_array();
For your code to work you need two changes:
First you join type should be a "left join". Than way you still will get a count result (0) even if a category has no videos yet.
Second you need to group your results to be able to use the aggregate function count().
Try with this:
$this->db
->select('categories.c_name, COUNT(videos.id) as num_videos')
->from('categories')
->join('videos', 'categories.c_name = videos.v_category', 'left')
->group_by('categories.c_name');
Also you should reconsider your DB design. If you have id columns in both tables (wich I assume are the primary key) then you should define the relationship between the tables (foreign keys) using the id column, not the name.