seeding relational database with factories in laravel 9 - laravel-9

I have some relations in my database but I can't handle relations data in DatabaseSeeder.php with factories.
This is my database schema.
I try with this code:
$categories = Category::factory()->count(3)->create();
$users = User::factory()->count(2)->create();
$products = collect();
$payment = array();
foreach ($users as $user) {
$payment = array_merge($payment, Payment::factory(4)->create([
'user_id' => $user->id,
])->toArray());
foreach ($categories as $category) {
$products->push(Product::factory()->state([
'user_id' => $user->id,
'category_id' => $category->id
]));
}
}
$prices = collect();
$first = true;
foreach ($products as $product) {
$prices->pull(Price::factory(2)->create([
'product_id' => $product->id,
'index' => $first
]));
$first = false;
}
But I got multiple errors. is there anyone to help me?

Related

get data value from jsonResponse Laravel 8

I have two controller
the first one has a register method:
public function register(Request $request)
{
$generalTrait = new GeneralTrait;
$user = new User;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->type = $request->type;
$user->save();
return $generalTrait->returnData('user',$user);
}
and the second also has register method:
public function register(Request $request)
{
$generalTrait = new GeneralTrait;
$user = (new UserAuthController)->register($request);
$admin = Admin::create([
'admin_name' => $request->admin_name,
//'user_id' => $response->user->user_id,
'user_id' => $user_id
]);
//Admin created, return success response
return $generalTrait->returnSuccessMessage('Admin created successfully');
}
when I try to get data from (JsonResponse) $user I find this error:
ErrorException: Undefined property: Illuminate\Http\JsonResponse::$user
returnDate method in GeneralTrait return:
public function returnData($key, $value, $msg = ""){
return response()->json([
'status' => true,
'errNum' => "5000",
'msg' => $msg,
$key => $value
]);
}
I find same Error when I try to get the status from the $response
How can I fix it?
I fix it by replacing returnData with:
public function returnData($key, $value, $msg = ""){
return [
'status' => true,
'errNum' => "5000",
'msg' => $msg,
$key => $value
];}
so to get user_id from user I said:
'user_id' => ($response["user"])->user_id
I wish I knew what my mistake was, and how I could have fixed it some other way

First page of product got retrieved with rest admin api in shopify

I have almost 300 products in my development store.
But when I use product.json to retrieve, only 250 products are retrieved.
Please help me in solving this.
I have followed this link
My code is
$nextPageToken = null;
do {
$response = $this->shopify->call1('GET', '/admin/api/2019-07/products.json?
limit=250&page_info='.$nextPageToken.'&rel=next' );
foreach($response['resource'] as $product):$images = array();
foreach ($product['images'] as $image):
$images[] = $image['src'];
endforeach;
$products[] = array(
'shop' => $this->session->userdata('id'),
'handle' => $product['handle'],
'title' => $product['title'],
'product' => $product['id'],
'vendor' => $product['vendor'],
'type' => $product['product_type'],
'images' => json_encode($images),
'published_at' => $product['published_at']
);
endforeach;
$this->load->model('shop_products_model');
if ($this->shop_products_model->update($products)) {
$this->load->model('customers/customers_model');
$updated = array(
'shop' => $this->session->userdata('shop'),
'products_updated' => date('Y-m-d H:i:s')
);
$this->customers_model->update($updated);
return true;
} else {
return false;
}
$nextPageToken= isset($response['next']['page_token']) ? $response['next']
['page_token'] : null;
} while($nextPageToken != null);

Codeigniter post data from declared variable

function index_post() {
$sql = "SELECT id_so FROM so_detail ORDER BY id_so DESC LIMIT 1";
$last_id2 = $this->db->query($sql)->result();
foreach ($last_id2 as $row) {
$last_id = $row->id_so;
}
//echo $last_id;
$data = array(
'id_so' => $this->post($last_id),
'id_product' => $this->post('id_product'),
'harga' => $this->post('harga'),
'harga_dasar'=> $this->post('harga'),
'modal' => $this->post('modal'),
'pajak' => $this->post('pajak'),
'qty' => $this->post('qty'),
'keterangan' => $this->post('keterangan'),
'create_user'=> $this->post('create_user'),
'create_time'=> $this->post('create_time'),
'update_user'=> $this->post('create_user'),
'update_time'=> $this->post('create_time'));
$insert = $this->db->insert('so_detail', $data);
if ($insert) {
$this->response($data, 200);
} else {
$this->response(array('status' => 'fail', 502));
}
}
i got a problem that the posted id_so is "null". when i echo $last_id it show correct id ex: 120. but when i call it to $this->post($last_id), it just be null..
How to post id_so with a string that already declared before ($last_id) ??
there are many array in $last_id2 variable.data will insert multiple time based on your last_id2.try this:
foreach ($last_id2 as $row) {
$data = array(
'id_so' =>$row->id_so,
'id_product' => $this->post('id_product'),
'harga' => $this->post('harga'),
'harga_dasar'=> $this->post('harga'),
'modal' => $this->post('modal'),
'pajak' => $this->post('pajak'),
'qty' => $this->post('qty'),
'keterangan' => $this->post('keterangan'),
'create_user'=> $this->post('create_user'),
'create_time'=> $this->post('create_time'),
'update_user'=> $this->post('create_user'),
'update_time'=> $this->post('create_time'));
}
$this->db->insert_batch('so_detail',$data);

Displaying CSV file content in Yii Framework's CGridView

I am trying to display CSV file content in CGridView, I want to display CSV file header as CGridView "Column" and its content as DataProvider. Please provide any idea to achieve this?
You can use CArrayDataProvider.
http://www.yiiframework.com/doc/api/1.1/CArrayDataProvider
$dataProvider = new CArrayDataProvider(str_getcsv(file_get_contents('file.csv')));
You can do something like this.
$file = fopen('test.csv', 'r');
$data = array();
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
$data[] = $line;
}
fclose($file);
$columns = array();
foreach ($data[0] as $key => $value) {
$columns[] = array(
'name' => $key,
'header' => $value,
);
}
$data = array_slice($data, 1);
$dataProvider = new CArrayDataProvider($data, array(
'keyField' => 0,
));
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => $columns
));
If you want to create a generic data provider for CSV, you can create a new class.
class CsvDataProvider extends CArrayDataProvider {
private $_columns = array();
public function __construct($file, $config = array()) {
$handler = fopen($file, 'r');
$data = array();
while (($line = fgetcsv($handler)) !== FALSE) {
$data[] = $line;
}
fclose($handler);
$this->_columns = array();
foreach ($data[0] as $key => $value) {
$this->_columns[] = array(
'name' => $key,
'header' => $value,
);
}
$data = array_slice($data, 1);
parent::__construct($data, array_merge($config, array(
'keyField' => 0,
)));
}
public function getColumns() {
return $this->_columns;
}
}
Then you can do something like this.
$dataProvider = new CsvDataProvider('file.csv');
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'columns' => $dataProvider->getColumns(),
));

Kohana ORM Display Information

I'm trying to learn how to display information from two tables.
Tables:
categories {category_id, category_title}
forums {forum_id, forum_title}
categories_forums {id_category, id_forum}
Models:
class Model_Forum extends ORM {
protected $_primary_key = 'forum_id';
protected $_belongs_to = array(
'categories'=> array(
'model' => 'category',
'through' => 'categories_forums',
'far_key' => 'id_category',
'foreign_key' => 'id_forum'
),
);
}
class Model_Category extends ORM {
protected $_primary_key = 'category_id';
protected $_has_many = array(
'forums'=> array(
'model' => 'forum',
'through' => 'categories_forums',
'far_key' => 'id_forum',
'foreign_key' => 'id_category'
),
);
}
I'm unsure how to display.
So far I have the following:
$categories = ORM::factory('category');
$forums = $categories->forums->find_all();
I don't how to display category_id, category_title, forum_id, forum_title.
You can use foreach loops, like this:
$categories = ORM::factory('category');
foreach ($categories->find_all() as $category){
echo $category->category_title, ' ', $category->id;
}
The following seems to work:
$categories = ORM::factory('category')->find_all();
$view = new View('default/index');
$view->categories = $categories;
$this->response->body($view);
foreach ($categories as $category) :
echo $category->category_title;
echo $category->category_id;
foreach ($category->forums->find_all() as $forum) :
echo $forum->forum_title;
echo $forum->forum_id;
endforeach;
endforeach;