laravel How to select articles with current language? - php

i'm new to laravel and i downloaded a starter site here,
https://github.com/mrakodol/Laravel-5-Bootstrap-3-Starter-Site
The site save language in table includes id, lang_code (e.g en), and the article database has a column "language_id" which seems to be used for filtering by language. But in the homecontroller, i see this line:
$articles = Article::with('author')->orderBy('position', 'DESC')->orderBy('created_at', 'DESC')->limit(4)->get();
This line select all the articles and print it out without language filter. How can i select article only in current language?

You can use where for filtering columns. It of course depends on what kind of id's given for languages. You need to check db.
$articles = Article::with('author')->where('language_id',1)->orderBy('position', 'DESC')->orderBy('created_at', 'DESC')->limit(4)->get();

Related

search in appropriate Mysql fields based on current App Locale in laravel

In a multilingual website I have these fields for Products table
product_id
title_fa
title_en
title_ar
desc_fa
desc_en
desc_ar
.
.
.
Now I want to search in (for Example) title fields based on Current laravel Locale.
I know that we can use this way :
$products = Product::where('title_'.App::getLocale(),'LIKE' , '%'.$data['search_value'].'%');
But I thinks that is not clean and easy for number of large fields is not suitable.
I'm looking for a way that was and simple as normal search field and then laravel search based on that field in background According to current App Locale like:
$products = Product::where('title','LIKE' , '%'.$data['search_value'].'%');
How Can I do that and what is best way ?
Hi if you want really easy way for search products than use this following laravel multi language package
https://github.com/dimsav/laravel-translatable
in this package you have to create 2 tables one Product and Product_translate, product table will have your basic product info, translate will have language data, in product_translate table you can use search queries, ease is table column name will remain same in where you have use language filter.

Display Dynamic List of Articles in Joomla

In a category list, I'd like to be able to display the list of articles dynamically. The website I'm developing allows a user to "submit" an article once they have read it. When they hit the submit button, the ID of that article is stored in the database in a table called "completed_quests." What I need to do is have Joomla check to see if the article ID exists in both the "content" and the "completed_quests" tables. If the article ID exists in both tables, then the article should not be displayed in the category list as that article or "quest" has already been submitted. If the article ID exists in the "content" table, but NOT in the "completed_quests" table, then the article SHOULD be displayed in the category list as it has note yet been submitted.
I'm wondering if there is a specific core Joomla file I should override to alter the category list output, or if I should develop a custom module to create this dynamic list. Any guidance would be much appreciated. If you have any other thoughts about how to display this dynamic list, I'm all ears.
edit: I've started by developing a custom module within Joomla, at least for testing. The below code is not working as expected. When I take out the following line "WHERE arp2i_completed_quests.id IS NULL" it displays a list of articles that exist in both tables. But what I need it to do is display the rows that exist in the content table but NOT in the completed_quests table. When I add the WHERE , it displays the text "ID:" and "TABLE:" for each row that exists, but the actual id and title from that row is not echo'ed to the screen. Please help.
Working code (see comments).
<?php
$query = "SELECT * FROM arp2i_completed_quests RIGHT JOIN arp2i_content ON arp2i_content.id=arp2i_completed_quests.id WHERE arp2i_completed_quests.id IS NULL LIMIT 0, 30 "; // prepare query
$db = &JFactory::getDBO(); // get database object
$db->setQuery($query); // apply query
$articles = $db->loadObjectList(); // execute query, return result list
foreach($articles as $article){ // loop through articles
echo 'ID:' . $article->id . ' Title: ' . $article->title . '<br />';
}
?>
You could override the com_content's listings view in your template.
You do that by copying default.php from /components/com_content/views/categories/tmpl/ to /templates/yourtemplate/html/com_content/categories/
However, I believe that such template overrides only allow you to update the display files of modules and components (the default.php files), and not the models, which would mean you have to add database queries into this layout file (or into a library), which feels a very hacky approach. It should work, though.
A better solution would be to create a bespoke component with a single view, and a model that queries your database to create the appropriate listings. It may even make sense to just make the whole thing a bespoke component (saving the articles in that component) and not use Joomla's articles manager at all.
If your listings page is not paginated, or if you do not care about an SEO friendly URL on secondary pages, then you also could create a module to display the listings and insert that into an article area. Modules are often simpler to make than components. You enter the database query into the helper file of a module.
You should just develop a content view that does exactly what you want. Should be very simple if you have some programming experience. You needn't change any core files.

PHP/MySQL Activerecord, get relationship row count

I have two tables:
media_folders
id, title
media
id, folder_id, title
They each have their own model
Media, MediaFolder. Nothing special about them except they have the static $table_name property set. Now I can assign a $has_many to the MediaFolder class which will find all associated Media. However, All i need is to get the number of media within a mediafolder and not the actual objects themselves. I want to end up with an attribute in a MediaFolder object called files which has a count of how many media rows has a folder_id of the current folder.
How would i do this?
Understand your case, but not your exact problem so can you provide some more details like:
1. does your problem is with building SQL query?
2. does your problem with building query using active records.. (if yes which framework you are using)
Need some more description.
If it's just some SQL :
$sql = "SELECT mf.folder_id, count(m.id) as files
FROM media_folders mf, media m
WHERE mf.id = m.folder_id
GROUP BY mf.folder_id"

zend pagination using DbSelect and DbTableSelect adapter

I'm a beginner to zend framework...until now i was doing pagination by fetching all details from DB..my friend says that it is not a good way for pagination....
he give me the following reference from Zend site...now i have some doubts...
1.what is setRowCount?is it a field in the table?
2.what is item_counts and RowCount?
my DB name is sreejith.
my table name is employee.
the code is:
$adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from('posts'));
$adapter->setRowCount(
$db->select()
->from(
'item_counts', array(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN => 'post_count')
)
);
$paginator = new Zend_Paginator($adapter);
OP code pasted from:
http://framework.zend.com/manual/1.12/en/zend.paginator.usage.html
That example is showing the ability to optimize how the total number of rows is calculated (needed to perform pagination, well to render the UI navigation at least).
As it says, "For example, if you keep track of the count of blog posts in a separate table, you could achieve a faster count query with the following setup:".
Look at the more simple examples passing a Zend_Db_Select to the factory... like: Zend_Paginator::factory() - That will probably be all you'll need.

How do I use the category name rather than the category id in my url?

I am using the CodeIgniter framework and am having trouble figuring out how to get my urls to display the category name but still have my queries reference the category id.
I have a controller set up called jobs. The structure of the url is "example.com/jobs/listings/category_id" example: "example.com/jobs/listings/3" where 3 would be the category id for "software".
I'd rather the url say "example.com/jobs/listings/category_name" example: "example.com/jobs/listings/software".
The problem I'm running into is that the "jobs" table holds stores the category_id that the job belongs to and not the category_name. I have a separate table that holds just the category id's and their names. I'm not sure how to A.) Write my query and B.) Make an SEO friendly url structure.
My current query looks something like "SELECT * FROM jobs WHERE cat_id = 3;
Also, I'm not sure if I should be using mod_rewrite in an .htaccess file or if I should be using the routes.php file in CodeIgniter.
Any help would be greatly appreciated!
-Thanks
Just join to your other table.
SELECT * FROM jobs JOIN categories ON jobs.cat_id = categories.id WHERE categories.name = ?;
Make sure to put a unique key on the category name, if you don't have one already.
mod_rewrite is a poor solution to app routing and wouldn't help much here anyway. Stick to whatever your framework provides.
Have a look here http://codeigniter.com/user_guide/helpers/url_helper.html at URL_title()
Convert you category name using the url_title() function making sure you have no duplicates.
When a url is called Look up your category from your model using a simple where statement
Eg
$this->db->from('categories');
$this->db->where('name', $url);
Etc...
Use this to load your page titles etc then use the categoryid returned to do the look up on your jobs table.

Categories