Php Laravel include model in middle of the program - laravel

I want to add models from one table called "modellist" in middle of the program.
foreach ($trans_infos as $key => $trans_info) {
if($trans_info->heirarchy_type==1 || $trans_info->heirarchy_type==2){
$model=TransactionModules::where('id',$info->module_id)->first();
$model_name=$model->model; // here am getting model name form table
use App\Models\.$model_name; //i used this function to include model on middle of program
$model_name::find($info->transaction_id)->update(['status' => 7]);
}
}

I have not tested this but try:
foreach ($trans_infos as $key => $trans_info) {
if($trans_info->heirarchy_type==1 || $trans_info->heirarchy_type==2){
$model=TransactionModules::where('id',$info->module_id)->first();
($model::class)::find($info->transaction_id)->update(['status' => 7]);
}
}
The class method returns the namespace with model name
I hope this works

You can do this direct on eloquent.
no need to declare use first
remember to use namespace as a string => "App\Models\\"
foreach ($trans_infos as $key => $trans_info) {
if($trans_info->heirarchy_type==1 || $trans_info->heirarchy_type==2)
{
$model=TransactionModules::where('id',$info->module_id)->first();
"App\Models\\".$model->model::find($info->transaction_id)
->update(['status' => 7]);
}
}

Related

Laravel 9 and Livewire 2 multiple storeAs methods throwing "Call to a member function storeAs() on null" error

I have 6 distinct images that a user needs to upload, so I have 6 different storeAs methods. When I have just one, everything works without fault, but 2 or more, I get a Call to a member function storeAs() on null error. Honesty, my code looks repetitive and dirty, so I'm not surprised.
public function createTentry($id)
{
$trial = Trial::find($id);
$image_plant_general = $this->image_plant_general->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_plant_general->getClientOriginalExtension(), 'trial-entry-photos');
$image_plant_closeup = $this->image_plant_closeup->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_plant_closeup->getClientOriginalExtension(), 'trial-entry-photos');
$image_fruit_in_plant = $this->image_fruit_in_plant->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_fruit_in_plant->getClientOriginalExtension(), 'trial-entry-photos');
$image_fruit_in_plant_closeup = $this->image_fruit_in_plant_closeup->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_fruit_in_plant_closeup->getClientOriginalExtension(), 'trial-entry-photos');
$image_fruit_in_harvest_single = $this->image_fruit_in_harvest_single->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_fruit_in_harvest_single->getClientOriginalExtension(), 'trial-entry-photos');
$image_fruit_in_harvest_group = $this->image_fruit_in_harvest_group->storeAs('/', $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.'.$this->image_fruit_in_harvest_group->getClientOriginalExtension(), 'trial-entry-photos');
Tentry::create([
...
'image_plant_general' => $image_plant_general,
'image_plant_closeup' => $image_plant_closeup,
'image_fruit_in_plant' => $image_fruit_in_plant,
'image_fruit_in_plant_closeup' => $image_fruit_in_plant_closeup,
'image_fruit_in_harvest_single' => $image_fruit_in_harvest_single,
'image_fruit_in_harvest_group' => $image_fruit_in_harvest_group,
]);
return redirect()->route('trial.show', [$trial->evaluation_id, $trial->id]);
}
I would recommend that you use an array and a loop, rather than checking individual properties. Makes for cleaner code, and easier to check the same thing over and over.
Another thing, I added model-route-binding, so that the Trial model is injected as a parameter directly without having to look it up explicitly.
public function createTentry(Trial $trial)
{
$prefix = $this->trial->id.'_'.$this->id.'_'.now()->timestamp.'.';
$images = [
'image_plant_general',
'image_plant_closeup',
'image_fruit_in_plant',
'image_fruit_in_plant_closeup',
'image_fruit_in_harvest_single',
'image_fruit_in_harvest_group'
];
$data = [
// Other Tentry-fields here
];
foreach ($images as $image) {
$data[$image] = $this->{$image}
? $this->{$image}->storeAs('/', $prefix.$this->image_plant_general->getClientOriginalExtension(), 'trial-entry-photos')
: null;
}
Tentry::create($data);
return redirect()->route('trial.show', [$trial->evaluation_id, $trial->id]);
}

CodeIgniter 3 code does not add data to database into 2 different tables (user_info & phone_info)

The problem is when I entered a new name no data is added. A similar thing happen when I entered an already existing name. Still, no data is added to the database. I am still new to CodeIgniter and not entirely sure my query builder inside the model is correct or not.
In the Model, I check if the name already exists insert data only into the phone_info table. IF name does not exist I insert data into user_info and phone_info.
Controller:
public function addData()
{
$name = $this->input->post('name');
$contact_num = $this->input->post('contact_num');
if($name == '') {
$result['message'] = "Please enter contact name";
} elseif($contact_num == '') {
$result['message'] = "Please enter contact number";
} else {
$result['message'] = "";
$data = array(
'name' => $name,
'contact_num' => $contact_num
);
$this->m->addData($data);
}
echo json_encode($result);
}
Model:
public function addData($data)
{
if(mysqli_num_rows($data['name']) > 0) {
$user = $this->db->get_where('user_info', array('name' => $data['name']))->result_array();
$user_id = $user['id'];
$phone_info = array(
'contact_num' => $data['contact_num'],
'user_id' => $user_id
);
$this->db->insert('phone_info',$phone_info);
} else {
$user_info = array(
'name' => $data['name']
);
$this->db->insert('user_info', $user_info);
$user = $this->db->get_where('user_info', array('name' => $data['name']))->result_array();
$user_id = $user['id'];
$phone_info = array(
'contact_num' => $data['contact_num'],
'user_id' => $user_id
);
$this->db->insert('phone_info', $phone_info);
}
}
DB-Table user_info:
DB-Table phone_info:
Extend and change your model to this:
public function findByTitle($name)
{
$this->db->where('name', $name);
return $this->result();
}
public function addData($data)
{
if(count($this->findByTitle($data['name'])) > 0) {
//.. your code
} else {
//.. your code
}
}
Explanation:
This:
if(mysqli_num_rows($data['name']) > 0)
..is not working to find database entries by name. To do this you can use codeigniters built in model functions and benefit from the MVC Pattern features, that CodeIgniter comes with.
I wrapped the actual findByName in a function so you can adapt this to other logic and use it elswehere later on. This function uses the query() method.
Read more about CodeIgniters Model Queries in the documentation.
Sidenote: mysqli_num_rows is used to iterate find results recieved by mysqli_query. This is very basic sql querying and you do not need that in a MVC-Framework like CodeIgniter. If you every appear to need write a manual sql-query, even then you should use CodeIgniters RawQuery methods.

Create Relationship inside the create function

I have a model that has a one to many relationship to the versions of the description.
In my Controller
$tag = Tags::create([
'name' => $request->get('name'),
'user_id' => \Auth::id(),
]);
$tag->update([
'content' => $request->get('description')
]);
In my Model:
public function setContentAttribute(string $value)
{
$this->versions()->create([
'user_id' => \Auth::id(),
'value' => $value
]);
}
So I can't put content directly as an attribute in the create method because there is no Model right now.
But is it possible to overwrite the create Method?
When I try to overwrite something like this in my Model it will do an infinity loop
public static function create($attr) {
return parent::create($attr);
}
So my question is if it is possible to have something like this:
$tag = Tags::create([
'name' => $request->get('name'),
'user_id' => \Auth::id(),
'content' => $request->get('content')
]);
and in the Model:
public static function create($attr) {
$value = $attr['content'];
$attr['content'] = null;
$object = parent::create($attr);
$object->content = $value;
$object->save();
return $object;
}
Update
I didn't overwrite the create method but called it customCreate. So there is no infinity loop anymore and I can pass all variables to the customCreate function that handles the relationships for me.
Solution
After reading the changes from 5.3 to 5.4 it turns out that the create method was moved so you don't have to call parent::create() anymore.
The final solution is:
public static function create($attr) {
$content = $attr['content'];
unset($attr['content']);
$element = static::query()->create($attr);
$element->content = $content;
$element->save();
return $element;
}
I don't see why not and you could probably implement a more general approach? Eg. checking if set{property}Attribute() method exists, if it does - use it to assign a value, if it doesn't - use mass assigning.
Something like:
public static function create($attr) {
$indirect = collect($attr)->filter(function($value, $property) {
return method_exists(self::class, 'set' . camel_case($property) . 'Attribute');
});
$entity = parent::create(array_diff_key($attr, $indirect->toArray()));
$indirect->each(function($value, $property) use ($entity) {
$entity->{$property} = $value;
});
$entity->save();
return $entity;
}
I haven't really tested it but it should work. I use something like this in one of my Symfony apps.

CodeIgniter - How to add strings to pagination link?

I would like to add some strings/values to the end of the generated pagination link.
For example, I get this
http://localhost/products/lists/5
I would like to have
http://localhost/products/lists/5/value/anothervalue
So, I need to send those values somehow... :)
Thank u all.
The pagination class has an undocumented configuration option called suffix that you can use. Here's how I use it in one of my apps:
// get the current url segments and remove the ones before the suffix
// http://localhost/products/lists/5/value/anothervalue
$args = $this->uri->segment_array();
unset($args[1], $args[2], $args[3]);
$args = implode('/', $args);
// $args is now 'value/anothervalue'
$base_url = 'http://localhost/products/lists';
$this->pagination->initialize(array(
'base_url' => $base_url,
'suffix' => '/'.$args,
'first_url' => $base_url.'/1/'.$args,
'uri_segment' => 3
));
The application/config/routes.php
$route['products/lists/(:num)/value/(:any)'] = "products/lists/$1/$2";
The controller code application/controllers/products.php
class Products extends CI_Controller {
public function index() {
$this->load->view('welcome_message');
}
public function lists($page = 1, $value = null) {
$this->load->view('product_lists', array('page' => $page, 'value' => $value));
}
}
In this way if your url is like http://localhost/products/lists/5/value/anothervalue
in function lists will be $page = 5 and $value = 'anothervalue' and they will be available in template product_lists ($page, $value)

load multiple models in array - codeigniter framework

<?php
class Home extends CI_Controller
{
public function __construct()
{
// load libraries //
$this->load->library('session');
$this->load->library('database');
$this->load->library('captcha');
// alternative
$this->load->library(array('session', 'database', 'captcha'));
// load models //
$this->load->model('menu_model', 'mmodel');
$this->load->model('user_model', 'umodel');
$this->load->model('admin_model', 'amodel');
// alternative
$this->load->model(array(?));
}
}
?>
How can i load all models in array? is it possible?
For models, you can do this:
$models = array(
'menu_model' => 'mmodel',
'user_model' => 'umodel',
'admin_model' => 'amodel',
);
foreach ($models as $file => $object_name)
{
$this->load->model($file, $object_name);
}
But as mentioned, you can create file application/core/MY_Loader.php and write your own method for loading models. I think this might work (not tested):
class MY_Loader extends CI_Loader {
function model($model, $name = '', $db_conn = FALSE)
{
if (is_array($model))
{
foreach ($model as $file => $object_name)
{
// Linear array was passed, be backwards compatible.
// CI already allows loading models as arrays, but does
// not accept the model name param, just the file name
if ( ! is_string($file))
{
$file = $object_name;
$object_name = NULL;
}
parent::model($file, $object_name);
}
return;
}
// Call the default method otherwise
parent::model($model, $name, $db_conn);
}
}
Usage with our variable from above:
$this->load->model($models);
You could also allow a separate DB connection to be passed in an array, but then you'd need to have a multidimensional array, and not the simple one we used. It's not too often you'll need to do that anyways.
I don't have any idea about the CodeIgniter 2.x but in CodeIgniter 3.x, this will also works :
$models = array(
'menu_model' => 'mmodel',
'user_model' => 'umodel',
'admin_model' => 'amodel',
);
$this->load->model($models);
Not natively, but you can easily extend Loader->model() to support that logic.
This work fine for me:
$this->load->model(array('menu_model'=>'menu','user_model'=>'user','admin_model'=>'admin'));

Resources