Reputation: 55
I am working on a site where a new content is generated when a user uploads an xml feed. The view creates the page, with the id is part of the url eg. news/culture/sa/73
and news/culture/us/79
.
First ask was to make a single view active,
I added was an active checkbox which is checked in the controller before rendering the view, what i would like to do is this. if i create news/culture/sa/74
, and i set it to active, in the database it should select id 73 (and all other id tagged with sa) and make them inactive.
I tried the following query in my controller:
$culture = Culture::with('saculture.usculture')
->where('id', $cultureId)
->where('template_id', $type)
->where('publish_date', '<=', $now->toDateTimeString())
->where('unpublish_date', '>=', $now->toDateTimeString())
->where('is_live', '=', 1)
->where('id', '<>', $cultureId)
->update('is_live', '=', 0)
->first();
What i am trying to achieve with the above is the following. select * from culture table where the template_id (which is category id) is the the same as the new created one. but what i want to update are all the other ids except this one to not active.
The second ask is the url based questions.
currently the urls generated from the upload have the category_id and post id: www.site.com/2/1
, www.site.com/2/2
, www.site.com/2/3
...
what i'm trying to achieve is the following. override the url eg www.site.com/2/1
to www.site.com/culture/sa
(which is the active and the user can not access the other pages www.site.com/2/2
or www.site.com/2/3
) as these will be disabled via the first query.
hope it makes more sense now.
Upvotes: 1
Views: 49
Reputation: 2043
$culture = Culture::with('saculture.usculture')
->where('id', $cultureId)
...
->where('id', '<>', $cultureId)
->first();
This is technically impossible to return any results. id cannot be equal to $cultureId and different from $cultureId at the same time. (At least not while you're observing it ;) - Greetings from Mr. Schroedinger and his cat)
Mark 1 item as active and reset all other items to inactive:
Culture::where('template_id', $type)
->where('id', '<>', $cultureId)
->update(['is_live' => 0]);
Don't worry too much about the other conditions. If an item isn't published, you can just disable it if you only ever want a single item to be active. The is_live = 1 check to only deactivate entries that were active before (there should only be 1 entry anyways if I understand your concept correctly) is unneccessary. The database can take care of not updating rows unless neccessary.
Override routes
As far as I understood you, this is what you want to achieve:
User opens www.site.com/2/1, where 2 is the template_id of sa and 1 is not the active culture in that category. Then user is redirected to www.site.com/culture/sa, which is the default landing page for the currently active culture of category sa.
If www.site.com/2/1 is actually the current active culture for template_id 2, then the site will return that culture object (or a view for it).
Here's a snippet that should at least outline how to solve that:
Route::get('{templateId}/{cultureId}', function ($templateId, $cultureId) {
$activeMatchingCultures = Culture::where('template_id', $templateId)->where('is_active', 1)->first();
if ($activeMatchingCulture->id === $cultureId) {
return $activeMatchingCulture;
}
else {
return redirect('/culture/'.$templateId); // You didn't outline how to find the slug for a templateId, you'll have to replace that yourself
}
})->where(['templateId' => '[0-9]+', 'cultureId' => '[0-9]+']);
Upvotes: 1