اذهب إلى المحتوى
  • 0

خطأ تطبيق لارافيل لاختصار الروابط 

محمود سامي حسين

السؤال

<?php
//تطبيق أختصار الروابط الموجود علي قناه الاكادمية علي اليوتيوب
//كود المتحكم 
namespace App\Http\Controllers;

use App\Models\ShortLink;
use Illuminate\Http\Request;

class ShortLinkController extends Controller
{
    public function index(){

        $shortlinks = ShortLink::paginate(10);

        return view('short_links',compact('shortlinks'));
    }

    public function store(Request $request){

        $request->validate([
            'link' => 'required | url | unique:short_links,link'
        ]);

       $data ['link'] = $request->link;
       $data ['code'] = \Illuminate\Support\Str::random(6);

       Shortlink::create($data);

       return redirect('/')->with('success','لقد تم أختصار الرابط بنجاح ');
    }



    public function show($code){

       $row = ShortLink::where('code',$code)->firstOrFail();

       $row->visits_count += 1 ;

       $row->save();

       return redirect($row->link);
    }
}


//ملف العرض view 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="shortcut icon" type="image/x-icon" href="{{ asset('icon.png') }}" />
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <title>{{__('home_page.shortened_links')}}</title>
    <style>
        h1 { text-align: center}
        .card { direction: rtl}
    </style>
</head>
<body dir="rtl" >
    <div class="container">
        <hr>
        <h1 class="text-center">{{__('home_page.shortened_links')}} </h1>
        <hr>
        <div class="card">
            <div class="card-header">
                <form action="{{ route('generate-shortenlink') }}" method="POST">
                    @csrf
                        <div class="input-group mb-3">
                            <input type="text" class="form-control" placeholder="{{__('home_page.write_a_link')}}">
                            <div class="input-group-append">
                            <button class="btn btn-success">{{__('home_page.generate_shortened_link')}}</button>
                            </div>
                        </div>
                        @if ($errors->has('link'))
                        <span class="alert-danger">
                            <strong>{{$errors->first('link')}}</strong>
                        </span>
                        @endif
                </form>
            </div>
            <div class="card-body">
                @if (session('success'))
                    <div class="alert alert-success">
                        <p>{{session('success')}}</p>
                    </div>
                @endif
                <table class="table">
                    <thead>
                      <tr>
                        <th>{{__('home_page.original_link')}}</th>
                        <th>{{__('home_page.shortened_link')}}</th>
                        <th>{{__('home_page.number_of_visits')}}</th>
                      </tr>
                    </thead>
                    <tbody>
                        @foreach ($shortlinks as $row)
                        <tr>
                            <td>{{ $row->link}}</td>
                            <td><a href="{{ route('show-shorten-link' ,$row->code ) }}"> {{ url('') . '/' . $row->code }} </a></td>
                            <td>{{ $row->visits_count }}</td>
                        </tr>    
                        @endforeach
                      
                    </tbody>
                  </table>
                  <div class="d-flex justify-content-center">

                    {{ $shortlinks->links() }}

                  </div>
            </div>
        </div>
    </div>
</body>
</html>
<?php
//المسارات
use App\Http\Controllers\ShortLinkController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', [ShortLinkController::class, 'index']
);

Route::post('/generate-shorten-link', [ShortLinkController::class, 'store'])->name('generate-shortenlink');
Route::get('/{code}', [ShortLinkController::class, 'show'])->name('show-shorten-link');
//ملف التهجير
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateShortLinksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('short_links', function (Blueprint $table) {
            $table->id();
            $table->string('link');
            $table->string('code');
            $table->integer('visits_count')->default(0);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('short_links');
    }
}

                   
                    

رابط المشروع علي جوجل درايف

1.PNG

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

سبب المشكلة لديك أنك لاترسل المفتاح link في الطلب post في هذا السطر

<input type="text" class="form-control" placeholder="{{__('home_page.write_a_link')}}">

قم بإضافة link داخل الخاصية name كالتالي 

<input name="link" type="text" class="form-control" placeholder="{{__('home_page.write_a_link')}}">

بهذه الطريقة سوف تحل المشكلة وسوف ترسل المفتاح link معل القيمة التي ادخلتها في الحقل لكي تتم معالجتها في ال controller

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...