Create public funtion in controller using for loop laravel - laravel

I am trying to create public funtions using for loop
here is an example about what I want to do:
class databaseController extends Controller
{
for ($i=0; $i < 5; $i++) {
# code...
public function create()
{
// dd('hoi_tb1_create');
if (Auth::guard('admin')->user()->level == 2) {
Schema::connection('mysql')->create('tb' . $i, function (
$table
) {
$table->increments('id');
});
}
// get all products
}
}
}
have you any idea how to do that?
Thanks in advance
Somur

You could use a second function to get this to work
class LoopController extends Controller
{
public function create($i)
{
if (Auth::guard('admin')->user()->level == 2) {
Schema::connection('mysql')->create('tb' . $i, function ($table) {
$table->increments('id');
});
}
}
public function createMany()
{
for($i = 0; $i < 5; $i++) {
$this->create($i);
}
}
}
The create function takes in a parameter of $i, but is not called directly from the route, instead you'd call createMany() which has a for loop in it. This will call create 5 times, each time passing its iterater value in e.g. 0, 1, 2, 3, 4.
Alternatively, if you're wanting these functions to be invoked independently, you could use PHP's magic method to dynamically call the create function. For example you could grab all of the numbers after create and then call create(numbers);
class LoopController extends Controller
{
public function create($i)
{
if (Auth::guard('admin')->user()->level == 2) {
Schema::connection('mysql')->create('tb' . $i, function ($table) {
$table->increments('id');
});
}
}
public function __call($method, $parameters)
{
if (preg_match('/create([\d]+)/', $method, $matches)) {
return $this->create($matches[1]);
}
throw new \BadMethodCallException("Method " . get_class($this) . "::$method does not exist");
}
}
So now if you call LoopController->create48(); It will then call ->create(48);

Related

Problem for working with DTO - Laravel, PHP

For example I have code like this:
$this->users = $data['data'];
$this->month = $data['month'];
$this->year = $data['year'];
But I need to use DTO. For example I used this function in DTO class:
public function getUsers(): string
{
return $this->users;
}
And as I understand I need to add it to the first code. But I don't understand how to use DTO for my the first code. Can you explain me please?
upd
Now I have:
public function __construct($data, $jobWatcherId)
{
$this->jobWatcherId = $jobWatcherId;
$jobsDTO = new JobsDTO($data['data'], $data['month'], $data['year'],
$data['working_days'], $data['holiday_hours'],
$data['advance_payroll_date'], $data['main_payroll_date']);
}
public function handle()
{
$jobWatcher = JobWatcher::find($this->jobWatcherId);
try {
$startedAt = now();
$jobWatcher->update([
'status_id' => JobWatcherStatusEnum::PROCESSING,
'started_at' => $startedAt,
]);
$redmineService = new RedmineAPIService();
foreach ($jobsDTO->getUsers() as $user) {
}
And for line foreach ($jobsDTO->getUsers() as $user) I have Undefined variable '$jobsDTO'
Your question is a bit unclear, but as I understand it, you want to instantiate a DTO with the above data?
You could have a class like:
class UsersDTO
{
public array $users;
public int $month;
public int $year;
public function __construct(array $users, int $month, int $year)
{
$this->users = $users;
$this->month = $month;
$this->year = $year;
}
public function getUsers(): array
{
return $this->users;
}
public function getMonth(): int
{
return $this->month;
}
public function getYear(): int
{
return $this->year;
}
}
and then somewhere else call:
$usersDTO = new UsersDTO($data['data'], $data['month'], $data['year']);
// Do something with $usersDTO->getUsers();

How to use this keyword for store variable

How to use this keyword in codeigniter to store values. Beacuse i use same value multiple time in the same page.
So if the this keyword is help to solve this problem.
function1() {
$value = '123456';
$assign1 = $value;
.....
}
function2() {
$value = '123456';
$assign2 = $value;
.....
}
Yes. We store value using this keyword inside the _construct() function to over come this problem.
The sample code,
public function __construct() {
parent::__construct ();
$this->value = '123456';
}
function1() {
$assign1 = $this->value;
.....
}
function2() {
$assign2 = $this->value;
.....
}
You can declare it as a global variable and use with $this, you can do this in a constructor or above constructor, both will work nice
class MyClass extends CI_Controller {
var $value = '123456';//global variable
function __construct() {
parent::__construct();
//$this->value = '123456';
}
function1() {
$assign1 = $this->value;
.....
}
function2() {
$assign2 = $this->value;
.....
}
}

Codeigniter pass variable between functions

How can i pass variable from function a to b in codeigniter controller.
function a()
{
$id = $this->input->post('id');
}
I want $id from function a to be passing to b
function b()
{
if($id)
{
.....
}
}
How can i do that?
create $id
public $id = false;
then use
function a(){
$this->id = $this->input->post('id');
}
function b(){
if($this->id){.....}
}
in controls class
class TestApi extends MY_Controller {
public function __construct() {
parent::__construct();
}
public t1 = false;
public function a () {
$this->t1 = true;
}
public function b () {
a();
if($this->t1){
...
}
}
}
or try global var? but not a good idea in framework
$a1 = 5;
function Test()
{
$GLOBALS['a1'] = 1;
}
Test();
echo $a1;
In the Same Class? maybe should check var or what u get from input?
Class Test{
public $t = 1;
public function a(){
$this->t = 20;
}
public function b(){
echo $this->t;
}
}
$TestClass = new Test;
echo $TestClass->t;
echo "-";
echo $TestClass->a();
echo ">";
echo $TestClass->t;
function a(){
$id = $this->input->post('id');
$this->b($id);
}
function b($id){
if ($id) {
//....
} else {
//.....
}
}
i used $this->session->set_userdata('a', $a); to pass the variable and it can be accessed by all method within the controller. to access it i used $b = $this->session->userdata('a');
thanks for all the help :D

Controller method's result is not accessible on view in CodeIgniter

I am trying to print a controller method's result on view but it is giving me an error:
Undefined variable: $states. Can someone help me to point out what is wrong in my code?
Model code:
public function state_names() {
$query = $this->db->select('name')
->get('place')
->where('parent','India');
$query->db->get();
return $query->result();
}
Controller Code:
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states']) && $st['states']->num_rows() > 0) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}
View code:
<?php foreach ($states as $state) {
echo $state->name;
}
try this one
public function state_names() {
$this->db->select('name')
$this->db->get('place')
$this->db->where('parent','India');
$query=$this->db->get();
return $query->result_array();
}
In your model you are running query twice - for each method get(). You should run it once:
public function state_names() {
$query = $this->db->select('name')
->where('parent','India')
->get('place');
return $query->result();
}
In your controller you can't check num_rows() because there are results - not full response from database.
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states'])) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}
Your Problem is 2 place First in Model and second in controller
if (isset($st['states']) && $st['states']->num_rows() > 0)
and Model
Model Solution
public function state_names() {
$query = $this->db->select('name')
->where('parent','India')
->get('place');
// $query->db->get();
if($query->num_rows() > 0){
return $query->result();
}
}
N.B [ Here Where will be first and get will be last its good practice ];
Controller Solution :
public function state_names() {
$st['states'] = $this->place_model->state_names();
if ($this->form_validation->run('resource_signup') == TRUE) {
if (isset($st['states']) ) {
$this->load->view('/web/resource_signup',$st);
}
}
return array();
}`

Laravel 4: Passing data from make to the service provider

The code below says it all...
// routes.php
App::make('SimpleGeo',array('test')); <- passing array('test')
// SimpleGeoServiceProvider.php
public function register()
{
$this->app['SimpleGeo'] = $this->app->share(function($app)
{
return new SimpleGeo($what_goes_here);
});
}
// SimpleGeo.php
class SimpleGeo
{
protected $_test;
public function __construct($test) <- need array('test')
{
$this->_test = $test;
}
public function getTest()
{
return $this->_test;
}
}
You can try to bind the class with the parameters directly into your app container, like
<?php // This is your SimpleGeoServiceProvider.php
use Illuminate\Support\ServiceProvider;
Class SimpleGeoServiceProvider extends ServiceProvider {
public function register()
{
$this->app->bind('SimpleGeo', function($app, $parameters)
{
return new SimpleGeo($parameters);
});
}
}
leaving untouched your SimpleGeo.php. You can test it in your routes.php
$test = App::make('SimpleGeo', array('test'));
var_dump ($test);
You need to pass your test array to the class inside of the service provider
// NOT in routes.php but when u need it like the controller
App::make('SimpleGeo'); // <- and don't pass array('test')
public function register()
{
$this->app['SimpleGeo'] = $this->app->share(function($app)
{
return new SimpleGeo(array('test'));
});
}
YourController.php
Public Class YourController
{
public function __construct()
{
$this->simpleGeo = App::make('SimpleGeo');
}
}

Resources