我有一个变量,我想与所有控制器共享其价值 - laravel

发布于 2025-01-22 19:41:53 字数 801 浏览 0 评论 0原文

我有一个 $ data1 的变量,我想与所有控制器共享$ data1的值,并在我的secondcontroller中使用

我的firstController

$data1 = reservation::select('user_id')
->where('date',request('date'))
->where('time',request('time'))
->where('room_id',request('room_id'))
->get(); 

(((在其中我想使用the我)数据的值1))

$msg = new message();
        $msg->date = Carbon::now();
        $msg->content = request('content');
        $msg->user_id = Auth::user()->id;
        $msg->reciver = $data1;     // here is where i want to use the value of $data1 

注意:$ data1的值更改。

更新: 我有一个表格(res.blade.php),其中用户在第一个控制器/日期,time,room_id/中插入我请求的数据。然后,我将用户重定向到另一个刀片,他插入了新信息(内容),然后保存新数据 + $ data1 我从第一个刀片的输入中获得

I have a variable that is $data1, i want to share the value of $data1 with all my controllers and use it

in my >FirstController

$data1 = reservation::select('user_id')
->where('date',request('date'))
->where('time',request('time'))
->where('room_id',request('room_id'))
->get(); 

in my >SecondController (( where I want to use the value of data1 ))

$msg = new message();
        $msg->date = Carbon::now();
        $msg->content = request('content');
        $msg->user_id = Auth::user()->id;
        $msg->reciver = $data1;     // here is where i want to use the value of $data1 

Note: the value of $data1 changes.

UPDATE:
I have a form (res.blade.php) where the user inserts the data the data I request in the first controller/date, time, room_id /. then I redirect the user to another blade where he inserts new info (content) then I save the new data + the $data1 I got from the first blade's inputs

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把昨日还给我 2025-01-29 19:41:53

创建中间件Commondata.php具有以下函数:

public function handle($request, Closure $next)
{
        $data1 = reservation::select('user_id')
->where('date',request('date'))
->where('time',request('time'))
->where('room_id',request('room_id'))
->get();

 $request->merge(['data1'=>$data1);
        return $next($request);
}

在这样的任何控制器中使用它:
$ request-> data1或request('data1')

Create a middleware CommonData.php with below function:

public function handle($request, Closure $next)
{
        $data1 = reservation::select('user_id')
->where('date',request('date'))
->where('time',request('time'))
->where('room_id',request('room_id'))
->get();

 $request->merge(['data1'=>$data1);
        return $next($request);
}

Use this in any controller like this:
$request->data1 or request('data1')

时间你老了 2025-01-29 19:41:53

生成带有名称之类的全局中间件

public function handle($request, Closure $next)
{
   $data1 = reservation::select('user_id')
        ->where('date',request('date'))
        ->where('time',request('time'))
        ->where('room_id',request('room_id'))
        ->get();

   $request->request->add(['data1', $data1]);

   return $next($request);
}

您可以 rel =“ nofollow noreferrer”>资源。

protected middleware = [
 'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
 'Illuminate\Cookie\Middleware\EncryptCookies',
 'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
 'Illuminate\Session\Middleware\StartSession',
 'Illuminate\View\Middleware\ShareErrorsFromSession',
 'App\Http\Middleware\VerifyCsrfToken',
 //....
 \App\Http\Middleware\Data1Middleware::class,
]

编辑:

如果“预订”是您的模型,则可能是骆驼(例如:保留),并且在中间件文件的顶部添加了此信息:

<?php
//....
use App\Models\Reservation;
//....

public function handle($request, Closure $next)
{
   $data1 = Reservation::select('user_id')
        ->where('date',request('date'))
        ->where('time',request('time'))
        ->where('room_id',request('room_id'))
        ->get();

   $request->request->add(['data1', $data1]);

   return $next($request);
}

但是,如果“保留”是数据库表名称,那么您就可以可以使用查询构建器获取数据:

use Illuminate\Support\Facades\DB;


public function handle($request, Closure $next)
{
   $data1 = DB::table('reservation')::select('user_id')
        ->where('date',request('date'))
        ->where('time',request('time'))
        ->where('room_id',request('room_id'))
        ->get();

   $request->request->add(['data1', $data1]);

   return $next($request);
}

You can generate a global middleware with name like Data1Middleware.php:

public function handle($request, Closure $next)
{
   $data1 = reservation::select('user_id')
        ->where('date',request('date'))
        ->where('time',request('time'))
        ->where('room_id',request('room_id'))
        ->get();

   $request->request->add(['data1', $data1]);

   return $next($request);
}

and add to $middleware in app/Http/Kernel.php as resource.

protected middleware = [
 'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
 'Illuminate\Cookie\Middleware\EncryptCookies',
 'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
 'Illuminate\Session\Middleware\StartSession',
 'Illuminate\View\Middleware\ShareErrorsFromSession',
 'App\Http\Middleware\VerifyCsrfToken',
 //....
 \App\Http\Middleware\Data1Middleware::class,
]

EDITED:

if "reservation" is your model, maybe it must be CamelCase (eg: Reservation) and in top of the middleware file add this:

<?php
//....
use App\Models\Reservation;
//....

public function handle($request, Closure $next)
{
   $data1 = Reservation::select('user_id')
        ->where('date',request('date'))
        ->where('time',request('time'))
        ->where('room_id',request('room_id'))
        ->get();

   $request->request->add(['data1', $data1]);

   return $next($request);
}

but if "reservation" is the database table name, so you can fetch data with query builder:

use Illuminate\Support\Facades\DB;


public function handle($request, Closure $next)
{
   $data1 = DB::table('reservation')::select('user_id')
        ->where('date',request('date'))
        ->where('time',request('time'))
        ->where('room_id',request('room_id'))
        ->get();

   $request->request->add(['data1', $data1]);

   return $next($request);
}
洛阳烟雨空心柳 2025-01-29 19:41:53

让我们使用a php特征

现在创建一个特质

<?PHP

namespace App\Traits;

use App\Models\Reservation;

trait HasReservationData
{

     public function reservationData()
    {
         // Better keep the model name start's with capital letter i.e. Pascal Case
        return Reservation::select('user_id')
            ->where('date',request('date'))
            ->where('time',request('time'))
            ->where('room_id',request('room_id'))
            ->get();
    }

}

,让我们为需要预订数据的控制器使用特征。

<?PHP

namespace App\Http\Controllers\StackOverFlow;

use App\Traits\HasReservationData;

class FirstController
{

    use HasReservationData;

    public function someMethod()
    {
        // You have the data here and manipulate the data however you want
        $data =  $this->reservationData();

        $msg = new Message();
        $msg->date = now();
        $msg->content = request('content');
        $msg->user_id = Auth::user()->id;
        $msg->reciver = $data;
    }

}

也许是另一个控制器

<?PHP

namespace App\Http\Controllers\StackOverFlow;

use App\Traits\HasReservationData;

class SecondController
{

    use HasReservationData;

    public function someMethod()
    {
        // You have the data here and manipulate the data however you want
        $data =  $this->reservationData();
    }

}

Let's solve this using a PHP Trait.

Create a Trait

<?PHP

namespace App\Traits;

use App\Models\Reservation;

trait HasReservationData
{

     public function reservationData()
    {
         // Better keep the model name start's with capital letter i.e. Pascal Case
        return Reservation::select('user_id')
            ->where('date',request('date'))
            ->where('time',request('time'))
            ->where('room_id',request('room_id'))
            ->get();
    }

}

Now let's use the Trait for our Controllers that need the reservation data.

<?PHP

namespace App\Http\Controllers\StackOverFlow;

use App\Traits\HasReservationData;

class FirstController
{

    use HasReservationData;

    public function someMethod()
    {
        // You have the data here and manipulate the data however you want
        $data =  $this->reservationData();

        $msg = new Message();
        $msg->date = now();
        $msg->content = request('content');
        $msg->user_id = Auth::user()->id;
        $msg->reciver = $data;
    }

}

Maybe another controller

<?PHP

namespace App\Http\Controllers\StackOverFlow;

use App\Traits\HasReservationData;

class SecondController
{

    use HasReservationData;

    public function someMethod()
    {
        // You have the data here and manipulate the data however you want
        $data =  $this->reservationData();
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文