اضافه کردن یک کلید خارجی(foriegn key) به جدولی که از قبل ساخته شده – Laravel

اول یه ماگریشن درست میکنیم:

php artisan make:migration create_foreign_key_for__table --table=products

حالا داخل ماگریشن کد زیر زیر مینزیم:

Schema::table('products', function (Blueprint $table) {
    $table->integer('user_id')->unsigned()->change();

    $table->foreign('user_id')->references('id')->on('users');
});

و در آخر :

php artisan migrate

استفاده از transaction در حلقه foreach در لاراول

به شکل زیر عمل کنید:

$dbConn->begin_transaction();

try{

    foreach($ítems as $item){
        //Whatever you are going to do
    }

    if($dbConn->commit()){

    //Notify buyer about all the transactions, the user doesn't receives the notification unless the commit was succesful

   }

catch(Exception ex){
    $dbConn->rollback();
}

منبع: کلیک کنید

Laravel. Redirect intended to post method

4

I’m using laravel 5 and this is my problem. User fill in form X and if he isin’t logged in, he gets redirected to fill in more fields form OR he gets possibility to log in. Everything works just fine, if user fill in additional fields, but if he login, laravel redirects user to form X with GET method instead of POST.

This is how my middleware redirect looks like:

return redirect()->guest('user/additional-fields');

This redirect appears on successfull log in:

return redirect()->intended();

You can use a named route to solve this issue:

Lets make a named route like this:

For Get

Route::get('user/additional-fields',array(
    'uses' => 'UserController@getAdditionalFields',
    'as'   => 'user.getAdditionalFields'
));

For post

Route::post('user/additional-fields',array(
    'uses' => 'UserController@postAdditionalFields',
    'as'   => 'user.postAdditionalFields'
));

So we can now ensure Laravel uses the right route by doing this

return redirect()->guest(route('user.getAdditionalFields'));

Also note that its not possible to redirect a POST because Laravel expects form to be submitted. SO you can’t do this:

return redirect()->guest(route('user.postAdditionalFields'));

except you use something like cURL or GuzzleHttp simulate a post request

منبع : کلیک کنید

فرستادن متغیر از کنترلی به کنترل دیگر در لاراول8

تو یکی از پروژه هام میخواستم اگر کاربر ادمین بود ، عملیات دیگه ای رو تو کنترل دیگه انجام بده( چون موضوع کمی پیچیدست از جزئیات بیشتر صرف نظر میکنم.) بنابراین به شکل زیر کد رو زدم:

pass variable to another controller in laravel8.0

به جای [$id] میتونید از compact(‘id’) هم استفاده کنید.

مسیرهاRoute هم به صورت زیره:

Routes

توجه کنید مسیر دوم با نام showProduct حتما باید ایدی داخل مسیر قرار داده بشه.

و کنترلر مقصد :

با برگردوندن ایدی متوجه میشیم انتقال متغیر به درستی انجام شده.

خوشحال میشم نظر بدید.

حل ارور “message”: “Method Illuminate\\Auth\\SessionGuard::factory does not exist.”, در jwt laravel

وقتی با ارور “message”: “Method Illuminate\Auth\SessionGuard::factory does not exist.”, مواجه شدید بدونید تو پیکربندی jwt خطا داشتید.

برید قسمت config\auth.php و تنظیمات default رو به شکل زیر تغییر بدید:

'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],

حل مشکل route login not defined در api passportلاراول 8

اول باید لاکین کرده باشید و توکن رو داشته باشید.

فقط کافیه تو نوارheader در postman قسمت Key ، کلمه Authorization رو بنویسید.

حل ارورErrorException Array to string conversion هنگام ران کردن دستور php artisan در لاراول

این مشکل برای این بود که روت ایجاد کرده بودم که ساختار درستی نداشت.

Route::apiResource('save', [PostsController::class, 'save']);

و با ارور زیر مواجه شدم:

ErrorException

Array to string conversion

at C:\xampp\htdocs\MyProjects\bessi\bessi2\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:410
406▕ protected function getResourceAction($resource, $controller, $method, $options)
407▕ {
408▕ $name = $this->getResourceRouteName($resource, $method, $options);
409▕
➜ 410▕ $action = [‘as’ => $name, ‘uses’ => $controller.’@’.$method];
411▕
412▕ if (isset($options[‘middleware’])) {
413▕ $action[‘middleware’] = $options[‘middleware’];
414▕ }

1 C:\xampp\htdocs\MyProjects\bessi\bessi2\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:410
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError(“Array to string conversion”, “C:\xampp\htdocs\MyProjects\bessi\bessi2\vendor\laravel\framework\src\Illuminate\Routi
ng\ResourceRegistrar.php”)

2 C:\xampp\htdocs\MyProjects\bessi\bessi2\vendor\laravel\framework\src\Illuminate\Routing\ResourceRegistrar.php:187
Illuminate\Routing\ResourceRegistrar::getResourceAction(“save”, “index”, [])

فقط کافیه روت رو اصلاح کنید همین :/