Laravel won't let me save pagination variable in an array - laravel

I need to return a json with all products and stores.
I tried this
$products = $products->paginate(20);
$stores=$this->getStores();
return ['product'=> $products, 'stores' =>$stores];
but $products returns empty. (If i run "return $products" it works fine)
Is there something i can do? Why laravel doesn't let me have the pagination in an array?

You could do something like this :
return Response::json(
array(
'products' => $products,
'stores' => $stores,
), 200
);
You also need to 'use' the Response facade:
use Illuminate\Support\Facades\Response;

Solution:
return ['product'=> $products->toArray(), 'stores' =>$stores];
or
return Response::json(
array(
'products' => $products->toArray(),
'stores' => $stores,
), 200
);

Related

Updating a document in laravel firestore

Hi I've been trying out things in updating aa document in firestore from laravel 9. But it doesn't seem to work at all.
What I have tried so far:
$product = app('firebase.firestore')
->database()
->collection('Products')
->document('15da5077cf8940f797db')
->update([
'path' => 'status',
'value' => 'archive'
]);
and
$status = "archive";
$currentData = json_decode(json_encode([
'status' => $status,
]));
$newData = [
'status' => $status,
];
$postData = app('firebase.firestore')
->database()
->collection('Products')
->document('15da5077cf8940f797db');
$postData->update([[
'path' => 'Products',
'value' => FieldValue::arrayRemove([$currentData])
]]);
$postData->set([
'Products' => FieldValue::arrayUnion($newData)
], ['merge' => true]);
and, javascript
const app = initizalizeApp(firebaseConfig);
const db = getFirestore(app);
btnArchive.addEventListener('click', (e) =>{
archiveDoc(doc(db, "Products", "15da5077cf8940f797db"), {
status: archive,
})
alert('Product archived!')
});
I want to update the status of a product from active to archive. But this doesn't work at all. Also is there another way to edit a document without specifying it?
this is how my collection looks like, each document has its own product details:

Undefinde offset:1 when importing laravel excel

this my code cause the trouble,
$cust = Customer::where('name', '=', $data[$i][0]['customer_name'])
->pluck('customer_id')[0];
this one for get customer id when i do store to sales order
$sales = array(
'customer_id' => Customer::where('name', '=', $data[$i][0]['customer_name'])->pluck('customer_id')[0],
'logistics_id' => Logistic::where('logistics_name', '=', $data[$i][0]['logistics'])->pluck('logistics_id')[0],
'subtotal' => $data[$i][0]['subtotal_rp'],
'shipping_cost' => $data[$i][0]['shipping_cost_rp'],
'discount_code' => 0,
'date_of_sales' => $data[$i][0]['date'],
'grand_total' => $data[$i][0]['grand_total_rp'],
'tax' => $data[$i][0]['tax_rp'],
'status' => $data[$i][0]['status'],
'discount_amount' => $data[$i][0]['discount_amount_rp']
);
$store_so = SalesOrder::create($sales);
but, when i do dd(), i get the right data
First of all, you need to check if the $data variable returns the data as you expect.
dd($data);
Next, you need to check that the $data array has the number of elements according to $total_data.
dd(count($data) == $total_data));
So basically, you just need to give condition or try-catch (recommended) :
if (isset($data[$i][0])) {
$customer = Customer::where('name', $data[$i][0]['customer_name'])->first();
$logistic = Logistic::where('logistics_name', $data[$i][0]['logistics'])->first();
if(!$customer){
dd('No customer found!');
}
if(!$logistic){
dd('No logistic found!');
}
$sales = [
'customer_id' => $customer->customer_id,
'logistics_id' => $logistic->logistics_id,
'subtotal' => $data[$i][0]['subtotal_rp'],
'shipping_cost' => $data[$i][0]['shipping_cost_rp'],
'discount_code' => 0,
'date_of_sales' => $data[$i][0]['date'],
'grand_total' => $data[$i][0]['grand_total_rp'],
'tax' => $data[$i][0]['tax_rp'],
'status' => $data[$i][0]['status'],
'discount_amount' => $data[$i][0]['discount_amount_rp'],
];
$store_so = SalesOrder::create($sales);
}
else{
dd('No $data[$i][0] found!');
}
PS : I recommend using the first() method instead of pluck('customer_id')[0].
It seems you need to get a customer_id from a customer_name.
Try to make everything simple:
$sales = array(
'customer_id' => Customer::where('name', $data[$i][0]['customer_name'])->first()->id,
...
);

Pass Data With Array in Laravel 6?

In CodeIgniter 3.x, we can pass data with array by using this code:
$data=array(
'apartment' => $this->apartmentmodel->get_all(),
'title' => "List Apartement"
);
$this->load->view('apartment/index',$data);
But, this code is not working when implement on my Laravel 6 project
$data=array(
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
);
return view(apartment.index,$data);
What is the problem and how to fix it? Anyone can help me?
Just remove ; from line number 2 before , your it will be work
$data=array(
'apartment' => DB::table('apartment')->get(),
'title' => "List Apartement"
);
return view('apartment.index', $data);
or you can use compact too, see below docs
https://laravel.com/docs/6.x/views
In Laravel we can use eloquent for fetch data from database.
$data=[
'apartment' => Apartment::get();,
'title' => "List Apartment"
];
return view('apartment.index', ['data' => $data]);
It will help full for you.
Thanks
PHpanchal
return view('apartment.index',compact('data'));
You can use compact() method, and you have an error (if this is project code).
Try:
$apartment = DB::table('apartment')->get();
$title = 'List Apartement';
return view('apartment.index', compact('apartment', 'title'));
Or
$data=[
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
];
return view('apartment.index', compact('data'));
Or:
$data=[
'apartment' => DB::table('apartment')->get();,
'title' => "List Apartement"
];
return view('apartment.index', ['data' => $data]);
Update:
In template file you can use it by calling $data, because in return statement you pass the variable name that will be shared with current template. So, to get some data from your array $data you just need to do:
#foreach($data['apartment'] as $apartment)
{{ $apartment }}
#endforeach
And to get your title use $data['title'] as well

POST, Response and assertJson in phpunit testing

I have following test function to check the update data is correct or not.
It has no problem in updating.
My question is how to check the given parameters are correct after updated.
for example
if response.id == 1 and response.name = 'Mr.Smith'
assertcode = OK
else
assertcode = NG
public function user_update_info(){
$this->post('login',['email' => config('my-app.test_user'),
'password' => config('my-app.test_pass')]);
$response = $this->post('/update_info',[
'id' => 1,
'name' => 'Mr.Smith',
'post_code' => '142-4756',
'prefectural_code' => '15',
'address' => 'Merchat St.',]);
$response->assertStatus(200);
}
Assume your update_info route update User model.
Try below after your code,
$user = User::find(1);
static::assertTrue($user->id == 1 && $user->name = 'Mr.Smith');
To check if the response returns a json data you expect, you can use assertJson() method of the response object like so:
$response->assertJson([
'id' => 1,
'name' => 'Mr.Smith'
]);

is it possible i create post and also want to redirect on same post after creation in laravel

DB::table('job_post')->insert(['user_id'=>$user->id,'job_title'=>$data['job_title'],'job_skill'=>$data['skill'],'max_sal'=>$data['max_sal'],'min_sal'=>$data['min_sal'],'job_type'=>$data['job_type'],'shift'=>$data['shift'],'last_date'=>$data['last_date'],'job_image'=>$imagename,'job_description'=>$data['job_description'],'latitude'=>$data['latitude'],'longitude'=>$data['longitude'],'location'=>$data['location']]);
//return redirect()->to('employer/congrats/......here i want id of job_post which i create');
return view('employer/congrats',compact('id','navbar_data'));
Yes, You Can Get last inserted id in Laravel by insertGetId:
$id = DB::table('users')->insertGetId(
array('email' => 'john#example.com', 'votes' => 0)
);
return redirect('employer/congrats',compact('id','navbar_data'));
You could try something like this
$id = DB::table('job_post')->insertGetId([
// ... data to insert e.g. 'user_id' => $user->id,
]);
return view('employer.congrats', compact('id','navbar_data'));
or
DB::table('job_post')->insert([
// ... data to insert e.g. 'user_id' => $user->id,
]);
$id = DB::getPdo()->lastInsertId();
return view('employer.congrats', compact('id','navbar_data'));
For more, check out this blog post.

Resources