中條 美冬

1669523400

Laravel Controller から Route にリダイレクトする方法を学びます

このチュートリアルでは、Laravel Controller から Route にリダイレクトする方法を学びます。ほとんどの場合、laravel プロジェクトのコントローラー メソッドからルートをリダイレクトする必要があります。Laravel では、laravel でルート名を使用してリダイレクトを返す方法がいくつか提供されています。ここでは、コントローラー関数から特定のルートにリダイレクトを返す 4 つの方法を示します。

ルート例:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
use App\Http\Controllers\HomeController;

/*
|--------------------------------------------------------------------------
| 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('users', [UserController::class, 'index']);
Route::get('home', [HomeController::class, 'index'])->name("home");

redirect() でroute()を使用する

App\Http\Controller\UserController:

<?php

namespace App\Http\Controllers;

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

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        $users = User::get();

        return redirect()->route("home");
    }
}

to_route()の使用

App\Http\Controller\UserController:

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::get();
  
        return to_route("home");
    }
}

redirect()の使用

App\Http\Controller\UserController:

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::get();
  
        return redirect("home");
    }
}

routeパラメータで () を使用する

の 2 番目の引数としてルート パラメータを渡すことができる場合の例を次に示しますroute()

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        return \Redirect::route('home', [$user->id])->with('message', 'User id found.');
    }
}

1 つだけの場合は、配列として記述する必要もありません。

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        return \Redirect::route('home', $user->id)->with('message', 'User id found.');
    }
}

ルートに複数のパラメーターがある場合、またはパラメーターが 1 つしかない場合でも、(読みやすくするために) どのパラメーターが各値を持つかを明確に指定したい場合は、いつでも次のようにできます。

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\User;
    
class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        return \Redirect::route('home', ['id'=>$user->id,'OTHER_PARAM'=>'XXX',...])->with('message', 'User id found.');

    }
}

ハッピーコーディング!!!

What is GEEK

Buddha Community

Seamus  Quitzon

Seamus Quitzon

1595201363

Php how to delete multiple rows through checkbox using ajax in laravel

First thing, we will need a table and i am creating products table for this example. So run the following query to create table.

CREATE TABLE `products` (
 `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
 `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
 `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
 `updated_at` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Next, we will need to insert some dummy records in this table that will be deleted.

INSERT INTO `products` (`name`, `description`) VALUES

('Test product 1', 'Product description example1'),

('Test product 2', 'Product description example2'),

('Test product 3', 'Product description example3'),

('Test product 4', 'Product description example4'),

('Test product 5', 'Product description example5');

Now we are redy to create a model corresponding to this products table. Here we will create Product model. So let’s create a model file Product.php file under app directory and put the code below.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'name','description'
    ];
}

Step 2: Create Route

Now, in this second step we will create some routes to handle the request for this example. So opeen routes/web.php file and copy the routes as given below.

routes/web.php

Route::get('product', 'ProductController@index');
Route::delete('product/{id}', ['as'=>'product.destroy','uses'=>'ProductController@destroy']);
Route::delete('delete-multiple-product', ['as'=>'product.multiple-delete','uses'=>'ProductController@deleteMultiple']);

#laravel #delete multiple rows in laravel using ajax #laravel ajax delete #laravel ajax multiple checkbox delete #laravel delete multiple rows #laravel delete records using ajax #laravel multiple checkbox delete rows #laravel multiple delete

Fredy  Larson

Fredy Larson

1595205933

Laravel create model migration and controller in a single command

Hello guys, sometimes while working on the large application, we need to create some very small modules like simple CRUD. So here in this article i will let you know laravel create model migration and controller in a single command.

We will run a single artisan command to create model. migration and controller. We can also create resource controller by giving parameter.

We can these model, migration and controller from three command but for time saving laravel provides feature to create thses three thing by one single command.

So for desired result open your terminal and run the following command.

php artisan make:model --migration --controller Product --resource  

Here, we have create product model, migration and product controller with all the resources methods.

We can also write this command in the short form like below.

php artisan make:model -mc Product --resource

You can also write this command in a more short way like below.

php artisan make:model Product -mcr

**Note: **Here, m represents migration, c represents controller and r represents as resource.

After running the above command you will get the output like

Model created successfully

Created migration (migration file name with current date and migration name )

Controller created successfully.

You can also read the article to create to add a column in the existing table through migration by clicking on the link below.

#laravel #create controller resource and model in a command #laravel artisan command #laravel create model #laravel single command for model and migration #migration and controller

Juned Ghanchi

1621508419

Laravel App Development Company in India, Hire Laravel Developers

Hire our expert team of Laravel app developers for flexible PHP applications across various cloud service providers.

With this easy build technology, we develop feature-rich apps that make your complex business process a lot easier. Our apps are,

  • More secure and scalable.
  • A good framework lets you manage and organize resources better.
  • And have a rich community base.

Get your business a best in classlaravel app. Hire laravel app developers in India. We have the best organizational set-up to provide you the most advanced app development services.

#laravel app development company india #hire laravel developers india #laravel app development company #hire laravel developers #laravel development agency #laravel app programmers

Routing - Laravel 7/8 Routing Example

In this tutorial i will give you information about basic route, named route and advance route in laravel.

Routing is basic and important components of the Laravel framework, All Laravel routes are determined in the file located as the app/Http/routes.php file, here I will show you Routing - Laravel 7/8 Routing Example and How to Create Routes in Laravel. also we will see Laravel Routing Parameter With Example.

Read More : Routing - Laravel 7/8 Routing Example

https://websolutionstuff.com/post/routing-laravel-7-8-routing-example


Read Also : Laravel 8 Yajra Datatable Example Tutorial

https://websolutionstuff.com/post/laravel-8-yajra-datatable-example-tutorial

#routing - laravel 7/8 routing example #routing #laravel #laravel8 #example #middleware

mehul bagada

mehul bagada

1592799468

Laravel add Custom Route File

Hi Guys,

In this blog, You can create Custom laravel route file in your project. we will create you how to Custom route file in laravel. i will show step by step create custom route file in laravel.This is a very easy way to manage laravel custom route files in your projects.i will create different route files like user_route, customer_route.

I will give full example of laravel custom route file, the example.

Link :- https://www.nicesnippets.com/blog/laravel-custom-route-file

#laravel #laravel tutorial #laravel tutorial for beginner #learn laravel #learn laravel for free