vendredi 2 avril 2021

How To submit form by one submit button in foreach loop in laravel?

This is my create page. Here there is form in foreach loop having on submit button. (This is my create page. Here there is form in foreach loop having on submit button.)

<form method="post" action="">
        @csrf
        <table>
            @foreach ($students as $student)

                <tr>
                    <td>Name : </td>
                    <td><input type="text" class="form-control" name="name" value="" readonly></td>
                </tr>
                <tr>
                    <td>Type : </td>
                    <td>
                        <select name="type" id="type">
                            <option value="present">present</option>
                            <option value="absent">absent</option>
                            <option value="half_day">half day</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Date : </td>
                    <td><input type="date" class="form-control" name="date"></td>
                </tr>

            @endforeach
            <button class="btn btn-primary" name="submit" id="submit">Submit</button>
        </table>
    </form>

This is my TypeController. This is my TypeController. I want to store data in foreach loop. (This is my TypeController. This is my TypeController. I want to store data in foreach loop)

public function create()
{
    $students = Student::all();
    return view('types.create', compact('students'));
}
public function store(Request $request)
{
    $request->validate([
        'name' => 'required',
        'type' => 'required',
        'date' => 'required',
    ]);

    $type = Type::create([
        'name' => $request->input('name'),
        'type' => $request->input('type'),
        'date' => $request->input('date'),
    ]);

    return redirect()->route('types.index')->withSuccess('Done');
}

This is my Type model which has relation with Student table (This is my Type model which has relation with Student table)

use HasFactory;

protected $table = 'types';

protected $fillable = [
    'name',
    'date',
];

public function student()
{
    return $this->belongsTo(Student::class);
}


via Chebli Mohamed

Can I use mike42 php library for m813 which is not found on the library

I am developing a pos application using laravel and I want to use thermal printer for printing

The printer the company bought is x-printer m813 which is not on mike42 library.

I want to know if I can still use the library for the printer or

If there is other ways I can write code that can print on the thermal printer



via Chebli Mohamed

Unable to login as admin in laravel

I have used laravel default authentication by using composer require laravel/ui and I have copy the default authentication and made new table called admin so that I can login as admin I am able to register but I am unable to login

web.php

Route::get('/admin/login',[App\Http\Controllers\admin\Auth\LoginController::class,'showLoginForm'])->name('admin.login');
Route::post('/admin/login',[App\Http\Controllers\admin\Auth\LoginController::class,'login']);
Route::get('/admin/register',[App\Http\Controllers\admin\Auth\RegisterController::class,'showRegistrationForm'])->name('admin.register');
Route::post('/admin/register',[App\Http\Controllers\admin\Auth\RegisterController::class,'register'])->name('admin.register');

Logincontroller.php

<?php

namespace App\Http\Controllers\admin\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function showLoginForm()
    {
        return view('admin');
    }

    public function login(Request $request)
    {
        $this->validateLogin($request);

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }
        return $this->sendFailedLoginResponse($request);
    }
    protected function sendLoginResponse(Request $request)
    {
        $request->session()->regenerate();

        $this->clearLoginAttempts($request);

        if ($response = $this->authenticated($request, $this->guard('admin'))) {
            return $response;
        }
//        if ($response = $this->authenticated($request, $this->guard()->admin())) {
//            return $response;
//        }
        return $request->wantsJson()
            ? new JsonResponse([], 204)
            : redirect()->intended($this->redirectPath());
    }

    public function guard()
    {
        return Auth::guard('admin');
    }

}

Registercontroller.php

<?php

namespace App\Http\Controllers\admin\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\Admin;
class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function showRegistrationForm()
    {
      return view('admin-register');
    }
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

//        $this->guard()->login($user);

        if ($response = $this->registered($request, $user)) {
            return $response;
        }

        return $request->wantsJson()
            ? new JsonResponse([], 201)
            : redirect($this->redirectPath());
    }
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\Models\User
     */
    protected function create(array $data)
    {
        Admin::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

create_admin_tables.php

<?php

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

class CreateAdminsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admins', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->timestamps();
        });
    }

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


via Chebli Mohamed

jeudi 1 avril 2021

Get How much amount to pay for any product after prorating

I am using business cashier for stripe payment in laravel. Suppose i have created so many products for subscriptions on Stripe. Suppose a user has a subscription for one of any product for a year and that prodyct has a recurring payment but in mid user is upgrading plan to higher price so how can i prorate the amount means how much amount i have to pay for upgrade my account to a higher product plan.

Basiclly i need a checkout page where i can get amount for that product subscription Prorate amount means how much duscount we are getting . Total amount to pay.



via Chebli Mohamed

Laravel Schedule Commands are not Creating Mutex lock files & WithoutOverlapping not working

Laravel Artisan Schedulers configured in app/console/kernel.php are not Creating Mutex files, thus WithoutOverlapping never working. However I never get errors. Those files are supposed to be at storage/frameworks/ right?

Due to overlaps of Artisan Commands, my data geting duplicated.

Sample commands written in kernel.php :

$schedule->command('clear:logFiles')->everyMinute()->withoutOverlapping();
$schedule->command('validate:sslCommerzTransactions')->withoutOverlapping()->everyTenMinutes();
$schedule->command('send:queuedTelegramNotifications')->withoutOverlapping()->everyMinute();
$schedule->command('send:queuedRewardNotifications')->withoutOverlapping()->everyMinute();

I also made a custom function to test, but I see no mutex file:

$schedule->call(function () {
            sleep(900);
        })->name("test")->withoutOverlapping(2);

Is there anything I can do to confirm is my mutex lock files really generating/working or not?

My Use case: I want to prevent overlapping. I mean if a command execution is running, Laravel should it run it until the previous execution is finished, whater timely is configured.

Please help. any info needed? I will provide.

My Laravel version is 5.8 and Cache Driver: file



via Chebli Mohamed

Laravel : Search with posted parameters

I am new to Laravel so please be patient with me.

i have to implement search functionality in Laravel. i am able to send form data to the search controller and receiving it as well

public function search_results(Request $request){
        if ($request->has('gender')) {
           echo $request->gender;
        }
        .....
}

Now when i am following tutorials on the web i am getting such examples :

public function filter(Request $request, User $user)
{
    // Search for a user based on their name.
    if ($request->has('name')) {
       $user->where('name', $request->input('name'))->get();
    }

}

or

public function index()
{
    $users = User::where('status', 1)
                ->where('is_banned', 0)
                ->get(['name']);
    
    dd($users);
}

now i am not sure from where this User:: or User $user is being added, and what should i do in my scenario.

Your suggestions would be helpful ..

Thanks



via Chebli Mohamed

I call Controller and view through route first time but it show error like this [closed]

home.php //controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class home extends Controller
{
    
   public function index($home)
    {
        return view('home',['home'=>$home]);

    }
}

web.php

Route::get('home/{home}',[home::class,'index']);

home.blade.php

  

ErrorException

Use of undefined constant home - assumed 'home' (this will throw an Error in a future version of PHP) (View: C:\Users\RASHEED1612E\project\resources\views\home.blade.php)



via Chebli Mohamed