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
CodePudding user response:
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')
CodePudding user response:
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);
}
CodePudding user response:
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();
}
}