I'm looking for some code example which make keyword from post into internal link.
In post table I have body attribute where is text of post. And I make extra table "keyword" where is word and URL attributes.
So i need script which will automatically recognize word which is adding in table "keywords" and found this word in post text and turn this word to hyperlink. Can you guys help me, please?
use App\Post;
use App\Category;
use App\Keyword;
public function getSingle($slug) {
// fetch from the DB based on slug
$categoriesall = Category::all();
$post = Post::where('slug', '=', $slug)->first();
// return the view and pass in the post object
return view('news.single')->withPost($post)->withCategoriesall($categoriesall);
}
This is just a starting point for whatever you are building
public function getSingle($slug) {
$keywords = Keyword::get()->toArray();
$categoriesall = Category::all();
$post = Post::where('slug', '=', $slug)->first();
$post->text = $this->transform($keywords, $post->text);
return view('news.single', compact('post', 'categoriesall'));
}
private function transform($keywords, $text)
{
//takes 2 parameters
//1st parameter is keywords array
//2nd parameter is text to be transformed
$words = explode(" ", $text); //split wherever we have space
foreach( $words as $key => $value ) {
if ( in_array($value, $keywords) ) {
$words[$key] = ". $value .";
}
}
$paragraph = implode(" ", $words);
return $paragraph;
}
Keep in mind, this is a starting point as this solution is not optimized at all. e.g: if your text contains html tags, those tags might be converted as anchors because this algorithm uses space as a delimiter; and most of the time there are spaces in html.
Related
I have a JSON field in my MySQL table column which has an JSON array with part of URLs.
["products/1.jpg", "products/2.jpg", "products/3.jpg"]
I want to get the array with appending a Base URL for each of the values of the array.
["www.example.com/images/products/1.jpg", "www.example.com/images/products/2.jpg", "www.example.com/images/products/3.jpg"]
I have tried with getAttribute() function like nelow code. But was not succeeded.
public function getImagesAttribute(){
$images = json_decode($this->attributes['images']);
$imageP = [];
foreach ($images as $image) {
$imageP[] = "www.example.com/images/" . $image;
}
return $imageP;
}
can you help me.
You probably want to use $this->images instead of $this->attributes['images'].
In this case, I would use Collections like so:
public function getImagesAttribute(){
return collect(json_decode($this->images))
->map(function ($image) {
return "www.example.com/images/" . $image;
})
->all();
}
i'm working on a project for my homework which i'm trying to make it work as a spa, but I've got some problem in making slug for posts.
in the tutorial which i've followed, instructor used this to make a slug from title :
protected static function boot()
{
parent::boot();
static::creating(function ($course){
$course->slug = str_slug($course->name);
});
}
now, if i make this table unique, which this is what i wanna do. how should i prevent app from giving me duplicate entry? or how can i add something to slug, like a number, Every time i get duplicate entry?
if i make a post with This Post name twice, second time, i get duplicate Error.
In my opinion your selected answer is not getting close to something efficient. In large applications the 2 random strings can be overwritten in a short time and then you will have huge issues (code and DB).
A safer approach is to build a service and use that when you save the slug in the DB. Or course this is not 100% perfect but definitely is better then to increment 2 random strings. That, by the way, can also affect the SEO part of the app.
Below you can find my example:
The Model
public static function boot()
{
parent::boot();
static::saving(function ($model) {
$slug = new Slug();
$model->slug = $slug->createSlug($model->title);
});
}
The Service
<?php
namespace App\Services;
use App\Job;
class Slug
{
/**
* #param $title
* #param int $id
* #return string
* #throws \Exception
*/
public function createSlug($title, $id = 0)
{
// Normalize the title
$slug = str_slug($title);
// Get any that could possibly be related.
// This cuts the queries down by doing it once.
$allSlugs = $this->getRelatedSlugs($slug, $id);
// If we haven't used it before then we are all good.
if (!$allSlugs->contains('slug', $slug)) {
return $slug;
}
// Just append numbers like a savage until we find not used.
for ($i = 1; $i <= 100; $i++) {
$newSlug = $slug . '-' . $i;
if (!$allSlugs->contains('slug', $newSlug)) {
return $newSlug;
}
}
throw new \Exception('Can not create a unique slug');
}
protected function getRelatedSlugs($slug, $id = 0)
{
return Model::select('slug')->where('slug', 'like', $slug . '%')
->where('id', '<>', $id)
->get();
}
}
You could use inbuilt Str class, and create some random strings in your Post slug. Example:
static::creating(function ($course){
$course->slug = str_slug($course->name . Str::random( 2 ));
});
This will add 2 random strings on each slug you create, which will ensure there are no duplicates. You can find more about Str class here.
you can use laravel Inbuilt Helper, follow below link.
https://laravel.com/docs/5.8/helpers#method-str-slug
$data = 'My Data'
$slug = Str::slug($data, '-');
dd($slug);
add this in your controller
use Illuminate\Support\Str;
I am working in Codeigniter and I want to hide ID from URL.
my current URL is:
www.localhost/CI/services/1/ac_repair
but need this type of URL in codeigniter:
www.localhost/CI/services/ac_repair
View Page Code:
<?=anchor('services/' . $ser->s_id . '/' . url_title($ser->s_title,'_') ,'View Service');?>
Controller Code:
public function services()
{
$this->load->model('ServicesModel', 'ser_model');
$s_id = $this->uri->segment(2, 0);
if($s_id){
$get_service = $this->ser_model->get_ser($s_id);
return $this->load->view('public/detail', compact('get_service') );
}
else
{
// $services = $this->articles->articles_list( $config['per_page'], $this->uri->segment(3) );
$get_services['result'] = $this->ser_model->all_services_list();
// $this->load->view('public/services', ['services'=>$services]);
$this->load->view('public/services', $get_services);
}
}
Model Code here:
public function get_ser($id)
{
// $q = $this->db
$q = $this->db->select('*')
->from('services')
->where( ['s_id' => $id] )
->get();
if ( $q->num_rows() )
return $q->row();
return false;
}
but need this type of URL in codeigniter:
www.localhost/CI/services/ac_repair
If you want this functionality you have to be able to use your title ac_repair in place of the id. This means the title needs to be marked as unique and therefore not contain any duplicates.
The following pseudo-code should give you an idea:
function services($url_title = null) {
if (!is_null($url_title)) {
// get ser would use title instead of $id
$this->db->where('title', $url_title);
} else {
// all rows
}
}
Other methods would be "hacky" and I cannot think of any off the top of my head that I would consider usable.
Side note: you should never be returning in a view
I trying function add tag for post in laravel. This is update code:
public function update(PostRequest $request, $id)
{
$post = Post::find($id);
$post->update($request->all());
if ($request->tags) {
$tagNames = explode(',', $request->tags);
$tagIds = [];
foreach ($tagNames as $tagName) {
$tagCount = Tag::where('name', '=', $tagName)->count();
if ($tagCount < 1) {
$tag = $post->tags()->create(['name' => $tagName]);
} else {
$post->tags()->detach();
$tag = Tag::where('name', $tagName)->first();
}
$tagIds[] = $tag->id;
}
$post->tags()->sync($tagIds);
}
return back()->with('success', 'Successfully');
}
It works well with pivot table, this has been resolved.
My problem lies in the tag table. When I delete all tags and retype new tag or exist tag, ok it works.
But when I do not change or keeping old tag and continue add new tag will cause an logic error. It will automatically add the record to the tags table.
For example: my post has 3 tags: test1, test2, test3. I keep it and add a tag: test4 then in the table tag automatically add tag: test2, test3, test4.
Is there a solution to my problem? Where was I wrong? I spent almost 2 days for it. I don't want to use package. Vote up for answer useful.
First, use firstOrCreate, it is short and convenient. Then, don't detach, it is useless, sync makes connected tags just like the array tagIds, it removes non-existing elements out of a pivot table and adds new ones.
In addition, you have spaces between commas and words, so you need to trim it.
if ($request->tags) {
$tagNames = explode(',', $request->tags);
$tagIds = [];
foreach ($tagNames as $tagName) {
$tag = Tag::firstOrCreate(['name' => trim($tagName)]);
$tagIds[] = $tag->id;
}
$post->tags()->sync($tagIds);
}
I think I've understood your bug, it is here
if ($tagCount < 1) {
$tag = $post->tags()->create(['name' => $tagName]);
} else {
$post->tags()->detach();
$tag = Tag::where('name', $tagName)->first();
}
It means that when you pass a new tag, it removes all the related tags out of the post. If you pass only old tags, they are not removed.
I have inputs array and i need to make a foreach but laravel $request->all() only return last one:
url:
http://localhost:8000/api/ofertas?filter_pais=1&filter_pais=2&filter_pais=3
controller:
public function filtroOfertas(Request $request){
return $request->all();
}
result:
{"filter_pais":"3"}
result should return 1, 2 and 3 and i need to make a foreach in filter_pais.
Any solution? Thanks
Use [] at the key of query string.
http://localhost:8000/api/ofertas?filter_pais[]=1&filter_pais[]=2&filter_pais[]=3
It will be parsed as array.
Repeated parameters make no sense and should be avoided without exception.
But looking at other solutions, there are several:
routes.php
Route::get('/api/ofertas/{r}', 'Controller#index');
Controller:
public function index($r)
{
$query = explode('&', $r);
$params = array();
foreach($query as $param)
{
list($name, $value) = explode('=', $param);
$params[urldecode($name)][] = urldecode($value);
}
// $params contains all parameters
}
Given that the URL has no question marks:
http://localhost:8000/api/ofertas/filter_pais=1&filter_pais=2&filter_pais=3