How to share $request variable between other function from other url in same controller on laravel - laravel

I want to share $request variable between other function from other url in same controller on laravel like below but how ?
Controller
class ABCController extends Controller
{
public function validation(Request $request)
{
---------------
}
public function save()
{
Log::debug($request)
}
api.php
Route::post('/abc',[ABCController::class,'validation']);
Route::get('/save',[ABCController::class,'save']);
What I tried
class ABCController extends Controller
{
public function validation(Request $request)
{
Session::put('data', $request)
session(['data' => $request]);
Log::debug(Session::get('data'));
Log::debug(session('data'));
}
public function save()
{
Log::debug(Session::get('data'));
Log::debug(session('data'));
}
I tried above but Log::debug in save function show me null in log.
Please give me advice.

Create a global variable
class ABCController extends Controller
{
private $data;
public function validation(Request $request)
{
$this->data =$request;
}
public function save()
{
echo $this->data;
}
}

Related

Problem with posting distance relationship data of an authenticated user in laravel

I have three models as per the code below. I have a problem of posting client data who is a user within the system and should be authenticated.I am thing of something like this:
public function store_application(Request $request)
{
$data = new LoanApplication();
$data->client_id = $client->id;
$data->save();
}
Loan Application Model
class LoanApplication extends Model
{
public function client()
{
return $this->hasOne(Client::class, 'id', 'client_id');
}
}
Client Model
class Client extends Model
{
public function client_users()
{
return $this->hasMany(ClientUser::class, 'client_id', 'id');
}
}
Client User Model
class ClientUser extends Model
{
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
}
Any help will be highly appreciated
Here is the answer to the above question:
public function store_application(Request $request)
{
$user = Auth::user();
$client= ClientUser::where('user_id', $user->id)->pluck('client_id');
$data = new LoanApplication();
$data->$client;
$data->save();
}

Testing Custom Route Model Binding

I'm trying to test a custom class that implements \Illuminate\Contracts\Routing\UrlRoutable and can't get the resolveRouteBinding method invoked.
<?php
namespace Tests\Unit;
use Illuminate\Support\Facades\Route;
use Tests\TestCase;
class BindingExampleClassTest extends TestCase
{
function test_invoke_resolve_route_binding_method()
{
Route::get('/invoke-route-binding/{binding}', function (BindingExampleClass $binding) {
dd($binding);
});
$this->get('/invoke-route-binding/1');
}
}
class BindingExampleClass implements \Illuminate\Contracts\Routing\UrlRoutable
{
public $id;
public function resolveRouteBinding($value, $field = null)
{
$this->id = $value;
}
public function getRouteKey()
{
// TODO: Implement getRouteKey() method.
}
public function getRouteKeyName()
{
// TODO: Implement getRouteKeyName() method.
}
public function resolveChildRouteBinding($childType, $value, $field)
{
// TODO: Implement resolveChildRouteBinding() method.
}
}
The dd response is BindingExampleClass with id still null.
Registering route inside a test function will not include any middleware. When working with route model binding in Laravel, \Illuminate\Routing\Middleware\SubstituteBindings::class middleware must be defined in the router instance.
Route::get('/invoke-route-binding/{binding}', function (BindingExampleClass $binding) {
dd($binding);
})->middleware(\Illuminate\Routing\Middleware\SubstituteBindings::class);

laravel can't retrieve session when redirect

in laravel 5.4
class AdminController extends Controller
{
public function checkLogin(Request $request)
{
Session::put('admin','yes');
return redirect('mobiles');
}
}
class MobilesController extends Controller
{
public function __construct()
{
if( ( Session::has('admin') ) )
{ dd('admin');}
else
{ dd('not admin'); }
}
}
it prints 'not admin' so what happen to the session , if i prints the admin session in class checkLogin it prints normally
Since L5.3, you cannot a access to the session in the controller's constructor. You have to use a closure :
public function __construct()
{
$this->middleware(function ($request, $next) {
echo Session::has('admin') ? 'admin' : 'not admin';
return $next($request);
});
}
https://laravel.com/docs/5.3/upgrade#5.3-session-in-constructors
you have to use laravel flashed-session-data
https://laravel.com/docs/5.4/redirects#redirecting-with-flashed-session-data
return redirect('mtest2')->with('admin','yes');
so modify your code like this
class AdminController extends Controller {
public function checkLogin(Request $request){
return redirect('mobiles')->with('admin',true);
}
}
class MobilesController extends Controller {
public function __construct() {
if( session()->has('admin') ){
dd('admin');
}else{
dd('not admin');
}
}
}
Here is updated code if you do not want to use Flashed data
class AdminController extends Controller {
public function checkLogin(Request $request){
$request->session()->put('admin',true);
return redirect('mobiles');
}
}
it should work.

Laravel Multiple module using one Controller

Now I have few modules created like Product, Sale, Category. I found out they actually using same function with similar process. For example update() in Controller :
Category
public function update($id)
{
$instance = Category::findOrFail($id);
$instance->fill(Input::all())->save();
}
Product
public function update($id)
{
$instance = Product::findOrFail($id);
$instance->fill(Input::all())->save();
}
How can I join it together to BaseController by just make the Model dynamic?
Something like this:
abstract class ResourceController extends BaseController
{
protected $entity;
public function __construct(Model $entity){ //or Eloquent, depending on your import alias
$this->entity = $entity;
}
public function update($id)
{
$instance = $this->entity->findOrFail( $id );
$instance->fill( Input::all() )->save();
}
}
class ProductController extends ResourceController{
public function __construct(Product $entity){
parent::__construct($entity);
}
}
class CategoryController extends ResourceController{
public function __construct(Category $entity){
parent::__construct($entity);
}
}

Laravel 'boot' function is not firing

I have the following in an eloquent model
class Bucket extends \Eloquent {
protected $fillable = ['name'];
protected $appends = ['slug'];
public function __construct(){
$this->key = substr(str_shuffle(MD5(microtime())), 0, 24);
}
public static function boot(){
dd('check');
}
public function users(){
return $this->belongsToMany('User');
}
public function getSlugAttribute(){
return slugify($this->name);
}
}
However I'm able to read and update the model with no problem. I was under the impression boot was supposed to be called every time a model was instantiated, is that wrong?
Here's one of the controllers I use to view all the buckets
public function index()
{
return Auth::user()->buckets;
}
In your constructor, try calling
parent::__construct();

Resources