Laravel Tips: Begini Cara Saya Membuat Sebuah URL di CMS Buatan Sendiri


Indonesia
English

Kasus ini untuk CMS yang sedang saya buat, jadi proses ini akan membantu saya men-generate url untuk dimasukan ke dalam database. Berikut adalah controller yang sudah saya buat:

    public function store(Request $request)
        {
            $url = $this->generateUrl($request->title);
            $project = Project::create([
                'title'         => $request->title,
                'subtitle'      => $request->subtitle,
                'description'   => $request->description,
                'url'           => $url
            ]);

            return redirect()->route('projects.index')->with(['success'=>'Data successfully saved.']);
        }

        public function generateUrl($title){
            //make all text to lower
            $lowerText = mb_strtolower($title);

            //replace all space with dash
            $dashText = preg_replace('/\s+/','-',$lowerText);

            //get web domain
            $appUrl = env('APP_URL');

            //combining
            $url = $appUrl.'/projects/'.$dashText;

            return $url;
        }
    

Semoga membantu ^^

I create a CMS for my client and I need to generate a url from title and store it to database. This is what I do:

    public function store(Request $request)
        {
            $url = $this->generateUrl($request->title);
            $project = Project::create([
                'title'         => $request->title,
                'subtitle'      => $request->subtitle,
                'description'   => $request->description,
                'url'           => $url
            ]);

            return redirect()->route('projects.index')->with(['success'=>'Data successfully saved.']);
        }

        public function generateUrl($title){
            //make all text to lower
            $lowerText = mb_strtolower($title);

            //replace all space with dash
            $dashText = preg_replace('/\s+/','-',$lowerText);

            //get web domain
            $appUrl = env('APP_URL');

            //combining
            $url = $appUrl.'/projects/'.$dashText;

            return $url;
        }
    

Hope it help you too ^^

Please Select Embedded Mode To Show The Comment System.*

Lebih baru Lebih lama

Ads

Free Hosting

Hottest Hosting

Niagahoster-Banner