Updating a one to many relationship without the use of implode laravel - php

I am trying to update my data into my database where the relationship is that I have many schools, so when I try to save inside my database, I want to get something like this:
[![enter image description here][1]][1]
But I only know how to use implode function to do it, and I can only make it like this:
I tried doing this but it doesn't work:
public function update1(Request $request, $user_id){
$rows = qualification::where('user_id', $user_id)->get();
foreach ($rows as $row){
switch ($row['meta_key']){
case 'school_name':
$row['meta_value'] = $request->input('School');
break;
case 'start_date':
$row['meta_value'] = $request->input('SDate');
break;
case 'end_date':
$row['meta_value'] = $request->input('EDate');
break;
case 'qualification_list':
$row['meta_value'] = $request->input('qualification');
break;
}
$row->save();
}
return redirect('/home');
}
When I try using that code it will give me this error, Array to string conversion
PersonalInfo model:
class PersonalInfo extends Eloquent
{
use SoftDeletes;
protected $table = 'personal_infos';
protected $primaryKey = 'id';
protected $dates = ['deleted_at'];
public function userQualifications()
{
return $this->hasMany('App\Qualification','user_id');
}
Qualification model:
class Qualification extends Model
{
protected $table = "qualifications";
public function PersonalInfos() {
return $this->belongsTo('App\PersonalInfo');
}
}
No error message:

Assuming you have your relationship setup properly in your User model, you could try something like this (not tested):
public function update1(Request $request, $user_id){
// get the user
$user = PersonalInfo::find($user_id);
// create the new array of meta data
$data = array();
// loop through the inputs array count
for($i=0; $i < count($request->input('School')); $i++) {
$data[] = [
'meta_key' => 'school_name',
'meta_value' => $request->input('School')[$i]
];
$data[] = [
'meta_key' => 'start_date',
'meta_value' => $request->input('SDate')[$i]
];
$data[] = [
'meta_key' => 'end_date',
'meta_value' => $request->input('EDate')[$i]
];
$data[] = [
'meta_key' => 'qualification_list',
'meta_value' => $request->input('qualification')[$i]
];
}
// create the relationships
$user->userQualifications()->createMany($data);
return redirect('/home');
}

Related

Save model outside of for-each loop

Assuming the Model Order
class Order extends Model {
use HasFactory;
protected $table = 'order';
protected $primaryKey = 'id';
public $incrementing = false;
protected $keyType = 'string';
protected $guarded = [];
public function extra(){
return $this->hasOne(Extra::class);
}
public function products(){
return $this->hasMany(Product::class);
}
}
and the Model Extra
class Extra extends Model {
use HasFactory;
protected $table = 'extra';
protected $guarded = [];
public function order(){
$this->belongsTo(Order::class);
}
}
and the Model product
class Product extends Model {
use HasFactory;
protected $table = 'product';
protected $guarded = [];
public function order(){
return $this->belongsTo(Order::class);
}
}
Now, from an API I receive data. With these data, I want to feed the models and then store the info to DB.
The approach there is atm is:
foreach ($list as $item) {
$order = new Order();
$order->id = $item['id'];
$order->title = $item['title'];
$order->save();
$extra = new Extra();
$extra->foo= $item['path']['to']['foo'];
$extra->bar= $item['path']['to']['bar'];
$order->extra()->save($extra)
$order->products()->createMany($item['path']['to']['products']);
}
The problem is that this code saves three times for each loop, one for order, one for extra, one for the product.
I would like to know if there is another way that I can use in order to gather the data inside the for-each and outside of it, to make something like
Order::insert($array_of_data);
I imagine it would look something like this, try it and if doesn't work please let me know i'll delete answer
$orders = [];
$extras = [];
$products = [];
foreach ($list as $item) {
$orders[] = [
'id' => $item['id'],
'title' => $item['title'],
];
$extras[] = [
'foo' => $item['path']['to']['foo'],
'bar' => $item['path']['to']['bar'],
];
$products[] = [
'order_id' => $item['id'],
'foo' => $item['path']['to']['products']['foo'] // or data it has
];
}
Order::insert($orders);
Extra::insert($extras);
Product::insert($products); // make sure each product has order id and data which is not visible here
I also suggest looking into converting $list into collection and then iterating over it, if the data is quite big you might make a use of LazyCollection which is the same as collection but better for processing larger data sets
Here's an example how you'd do it using lazy collection
LazyCollection::make($list)
->each(function (array $item) {
$order = Order::create(
[
'id' => $item['id'],
'title' => $item['title']
],
);
Extra::create(
[
'order_id' => $item['id'],
'foo' => $item['path']['to']['foo'],
'bar' => $item['path']['to']['bar'],
],
);
$order->products()->createMany($item['path']['to']['products']);
});
While it doesn't necessarily create many at once, it it memory saviour and will process quite quickly

Saving array of dynamic fields data to DB using oneToMany relationship

I have been trying to save the data coming from the dynamically generated fields in the form of an array. I have a oneToMany relationship for the customer table.
I have tried to loop through each field but I am unable to achieve it, please correct me if I am wrong.
public function store(Request $request)
{
$res = $request->all();
$res['address'] = implode(' ', array_values($request->address));
$customer = Customer::create($res);
if ($res) {
$customerData = [];
foreach ($request->department_name as $key => $n) {
$customerData = array(
'department_name' => $request->department_name[$key],
'person_name' => $request->person_name[$key],
'person_number' => $request->person_number[$key],
'person_email' => $request->person_email[$key],
'notification_flag' => !isset($request->notification_flag[$key]) ? 0 : $request->notification_flag[$key] === "on" ? 1 : 0,
'custinvoice_noti' => !isset($request->outstanding[$key]) ? 0 : $request->outstanding[$key] === "on" ? 1 : 0,
'invoice_noti' => !isset($request->invoice[$key]) ? 0 : $request->invoice[$key] === "on" ? 1 : 0,
);
$deptModel[] = new Department($customerData);
$customer->department()->saveMany($deptModel);
}
}
return redirect('admin/customers');
}
Customer model and Department model have the following relationship.
class Customer extends Model
{
protected $fillable = ['owner_name', 'address', 'country', 'state', 'city', 'pincode', 'number', 'correspondance_check'];
public function department()
{
return $this->hasMany('App\Department');
}
}
Department Model.
class Department extends Model
{
protected $fillable = ['customer_id', 'department_name', 'person_name', 'person_number', 'person_email', 'notification_flag', 'notification_type'];
public function customer()
{
return $this->belongsTo('App\Customer');
}
}
public function store(Request $request)
{
$customer = new Customer();
$customer->owner_name = $request['owner_name'];
$customer->country = $request['country'];
$customer->state = $request['state'];
$customer->city = $request['city'];
$customer->pincode = $request['pincode'];
$customer->number = $request['number'];
$customer->correspondance_check = $request['correspondance_check'];
$res = $customer->save();
$cus = Customer::where(['id'=> $res->id])->firstOrFail();
$dep = new Department();
$dep->customer()->associate($cus);
$dep->save();
return redirect('admin/customers');
// return response()->json($customerData);
}
Customer model and Department model have the following relationship.
class Customer extends Model
{
protected $fillable = ['owner_name', 'address', 'country', 'state', 'city', 'pincode', 'number', 'correspondance_check'];
public function department()
{
return $this->hasMany('App\Department');
}
}
//Department Model.
class Department extends Model
{
protected $fillable = ['customer_id', 'department_name', 'person_name', 'person_number', 'person_email', 'notification_flag', 'notification_type'];
public function customer()
{
return $this->belongsTo('App\Customer');
}
}

How to get collection with Trashed Laravel Resource

I have a problem I try to get list of posts with trashed posts through Resource API. Code example here
public function index(Request $request)
{
$perPage = (int) $request->get('per_page');
if ($perPage < 1 or $perPage > 1000) {
$perPage = config('database.paginate');
}
$clubId = (int)$request->get('club');
$query = Playground::query();
if ($clubId) {
$query = $query->where('club_id', $clubId);
}
return PlaygroundResource::collection($query->paginate($perPage));
}
I cant add ->withTrashed() method in collection, So how can i get all posts???
UPD::
Playground Model
protected $fillable = [
'slug',
'name',
'club_id',
'links',
'playground_types',
'services',
'equipments',
'surfaces',
'sports',
'type',
'description'
];
protected $dates = ['deleted_at'];
Have you tried putting the ->withTrashed() behind the $query? For example right after you define it?
$query = Playground::query();
$query->withTrashed();

Laravel 5.4: Storing the 'Where' clause in a variable

I want to write a dynamic update query in Laravel which accepts arguments and can be used in whole project.
Following is my Controller function:
public function editquery(Request $request)
{
$city_id = $request->input('city_id');
$city_name = $request->input('city_name');
$tbl = 'city';
$data = ['city_name'=>$city_name];
$wher = ('city_id',1);
General_model::editrecord($data,$wher,$tbl);
return redirect()->action('Admin_controller#cities_page')->with('status','Record Updated Successfully!');;
}
Below is my Model function:
public static function editrecord($data,$wher,$tbl)
{
return DB::table($tbl)->where($wher)->update($data);
}
The only problem here is that I cannot store the value ('city_id',1) in the $wher variable. This is the screenshot of the error:
link to the image file
Is there any other way to do this. Please Help.
The where method accepts an array of conditions.
$table = 'city';
$conditions = [
['city_id', '=', '1']
];
$data = ['city_name' => $city_name];
General_model::editRecord($table, $conditions, $data);
// In your model
public static function editRecord($table, $conditions, $data)
{
return DB::table($table)->where($conditions)->update($data);
}
You can also set multiple conditions.
$conditions = [
['city_id', '=', '1'],
['test', '=', 'test'],
];
Edit
This is the default where method
where($column, $operator = null, $value = null, $boolean = 'and')
Setting the fourth parameter to or will make the condition orWhere.
Example
$conditions = [
['city_id', '=', '1'],
['test', '=', 'test', 'or'],
];
You can't do this
public static function editrecord($data,$wher,$tbl)
{
return DB::table($tbl)->where($wher)->update($data);
}
Since, where is a function; it expects 2 or 3 arguments and not just 1 argument.
You will have to pass both the arguments like so
public static function editrecord($data, $where_column, $where_val, $tbl)
{
return DB::table($tbl)->where($where_column, $where_val)
->update($data);
}
Then, in your controller function
$where_column = 'city_id';
$where_val = 1;
General_model::editrecord($data,$where_column,$where_val,$tbl);
Your code is not exactly in the style of Laravel, why would you want to create a separate static function, if such tasks are easily solved by the standard features of Eloquent / Query Builder?
Eloquent example:
app/City.php
<?php
class City extends Model {
protected $table = 'city';
protected $primaryKey = 'city_id';
protected $fillable = ['city_name'];
}
In your controller:
City::findOrFail($city_id)->update([
'city_name' => $city_name
]);
Query Builder example:
DB::table('city')->where(['city_id' => $city_id])->update([
'city_name' => $city_name
]);
This is much easier to read, understand and support than functions that do similar things in an incomprehensible way.

How to get flexibility in laravel sql query

I want to add a sql filter where('comment_id', '=', 1) to php code
$datas = $this->model->ADD HERE->orderBy('created_at', 'DESC')->paginate(15);
Trying to add the string to code take me hours. How to make it?
Here is my code:
CommentResource.php passing the sql filter as string parameter.
<?php
class CommentResource extends BaseResource
{
public function index()
{
$filter = "where('comment_id', '=', 1)";
return parent::index_filter($filter);
}
CommentResource.php
<?php
class BaseResource extends Controller
{
protected function index_filter($filter)
{
$datas = $this->model->ADD HERE->orderBy('created_at', 'DESC')->paginate(15);
return view($this->resourceView.'.index')->with('datas', $datas);
}
}
As I understand you want to use different types of where as filters in your queries. That's why you want to make them dynamic. I would suggest the following solution for your task:
<?php
class CommentResource extends BaseResource
{
public function index()
{
$filter = [ 'operator' => 'where', 'args' => ['comment_id', '=', 1]];
return parent::index_filter($filter);
}
<?php
class BaseResource extends Controller
{
protected function index_filter($filter)
{
$where = $filter['operator'];
$args = $filter['args'];
$datas = $this->model->$where(...$args)->orderBy('created_at', 'DESC')->paginate(15);
return view($this->resourceView.'.index')->with('datas', $datas);
}
}
However, it will work starting from Php5.6+ because of oeprator ...
I am not sure if I got your requirements correctly, but if you rewrite index_filter to accept field and value separately, then you may user a regular where() from laravel:
protected function index_filter($field,$value)
{
$datas = $this->model->where($field,$value)->orderBy('created_at', 'DESC')->paginate(15);
return view($this->resourceView.'.index')->with('datas', $datas);
}
You can find the docs here. In case you really need more flexibility:
protected function index_filter($filter)
{
$datas = $this->model->whereRaw($filter)->orderBy('created_at', 'DESC')->paginate(15);
return view($this->resourceView.'.index')->with('datas', $datas);
}
Have in mind though that this is really dangerous, as you expose the possibility to inject malicious code, it should be definitely properly escaped beforehand.
My latest code works right. I'll post here.
<?php
class CommentResource extends BaseResource
{
public function index()
{
$options = [
'filters'=>[
[ 'operator' => 'where',
'args' => [
[ 'article_id', '=', $article_id ],
[ 'comment_id', '=', $comment_id ],
// add filter args...
],
],
// add filter operators here...
],
'sorts' => [
'column' => $sortColumn, // change sort column...
'order' => $sortOrder, // change sort order...
],
];
return parent::index_filter($options);
}
<?php
class BaseResource extends Controller
{
protected function index_filter($options, $number=15)
{
$result = $this->model;
foreach ($options['filters'] as $filter) {
$operator = $filter['operator'];
$args = $filter['args'];
$result = $result->$operator($args);
}
if ( $options['sorts'] != [] ) {
$column = $options['sorts']['column'];
$order = $options['sorts']['order'];
$result = $result->orderBy($column, $order);
}
return $result->paginate($number);
}
}
The reason I change ...$args to $args is, when 'args' has more than on value, for example,
'args' => [
[ 'article_id', '=', $article_id ],
[ 'comment_id', '=', $comment_id ],
// add filter args...
],
...$args will change 'args' to one array, but $args will remain 'args' as nest array, which is the operator 'where' want.

Categories