I am generating a query that collects an invoice, and the related invoice customer.
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
return view('invoiceView', ['invoice'=>$invoice]);
It works correctly, through $invoice->clients, I can access the client.
But I would like to be able to separate the client from the collection invoice. To send it through another variable. So that it looks something like this:
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
//Some magic here
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
It is to take advantage of an invoice creation view (to be able to edit), which is expected by the $clients collection.
I have searched, to do this in the view, but it is not possible or it is not recommended.
I guess I could do something like thisbefore sending it to view:
$client = $invoice->customer
But then I would be sending it twice on view.
To unset the relationship from a model you can use unsetRelation($relation) method
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
$client = $invoice->fullClient;
$invoice->unsetRelation('fullClient');
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
you could use pluck()
$invoice = Invoice::with(['lines', 'fullClient'])->findOrFail($idInvoice);
//The magic
$client = $invoice->pluck('fullClient');
return view('invoiceView', ['invoice'=>$invoice,'client'=>$client]);
docs
Related
What patterns can I use for 'automatic' resource CRUD operations for given Models in Laravel?
Say I have two models SomeModel and SomeRelatedModel where some_related_model.some_model_id is an FK to SomeModel.
The standard method on the SomeModelController for handling the create POST /api/someModel might look like this:
public function store(Request $request)
{
$user = Auth::guard('api')->user();
$data = $request->get('data');
$data['user_id'] = $user->id;
$someModel = SomeModel::create($data);
// has this request been made with the data for the
// related model? If so create this too.
if($data['relatedModel']){
SomeRelatedModel::create(array_merge(
['some_model_id' => $someModel->id]
$data['relatedModel']
));
}
// has this request been made expecting to get related
// models back in the response? If so load these
if($request->has('with')){
$someModel->load($request->get('with'));
}
return (new PostResource($post))
->toResponse($request)
->setStatusCode(201);
}
This works but is very verbose and for models with a sub-sub relation would need changing further. Similar work will need to be done for the other endpoints for all resources.
Is there a more versatile (or tidy) pattern using out-of-the box classes to get a similar effect?
Have a look at Laravel Orion. Fits your use case.
I've checked the Q&A about this and can't find anything, so thought i'd ask.
I have a very simple Laravel controller returning all results from a table as below via the 'Name model'. There is then also a further call to my controller, via the model to count the rows and all works and sends to the result set fine...
// All results from my 'Name' model:
$results = $this->name->getAllResults(); // All works fine.
// I then use my controller again, count the rows via the model and add them to $results:
$results['count'] = $this->countNames(); // Again fine
BUT, when i try to add a string to the $results array before i pass it off to th view, as in:
$results['test'] = 'Test'; // This fails in the view
$results['test'] = 124; // But this passes in the view and renders.
It only seems to allow me to add an INT to my result set array. as $results['test'] = 124 also fails.
I then finally, have this sending to my view via:
return view('names', compact('results')); // And that works fine.
Can anyone see what it is I am missing and why integer added to $results works and not a string?. Many thanks in advance.
You are updating collection data. The following line will give collection of models.
$results = $this->name->getAllResults(); // This may give collection of the model
And below, you are updating the collection object.
$results['count'] = $this->countNames();
You can do the following to safely send data to view, without modifying any.
$results = $this->name->getAllResults();
$count = $this->countNames();
$test = 'Test';
$test2 = 124;
return view('names', compact('results','count','test','test2'));
I'm using laravel API handler
and I was wondering on how to send a request to order by a column from 'with' table
Here is my backend code
$query = Product::query();
$query->company();
$query->with('category'); $result=ApiHandler::parseMultiple($query);
return $result->getResponse();
and the request-url
http://localhost/public/api/v1/product/view/0?&_offset=0&_limit=10&_config=meta-total-count,meta-filter-count&_sort=-category.category_name
$query = Product::getModel()
->newQuery()
->select('products.*','categories.*')
->join('categories','product.category_id,'=','categories.id')
->orderBy('category.sort_order');
Something like that should do.
Is there a way to chain the method with and create together?
I have tried to to do something like this and it doesn't work..
return Post::with('User')->create($inputs);
if I use:
return Post::create($inputs)->user();
I'll only get the user not the post.
The last option is:
$post = Post::create($inputs);
return Post::with('User')->find($post->id);
There must be a better way to do it, I guess.
If you will do it like in your example
Post::with('User')->create($inputs);
how do Laravel will know to what exact user attach this new post?
That's why you have do it like this:
// Get your user
$user = Auth::user();
// Create new post
$post = new Post($inputs);
// Attach your post to a user and save it
$user->posts()->save($post);
I'm trying to write a Magento (CE 1.4) extension to export order data once an order has been paid for. I’ve set up an observer that hooks in to the sales_order_invoice_save_after event, and that is working properly - my function gets executed when an invoice is generated. But I’m having trouble getting information about the order, such as the shipping address, billing address, items ordered, order total, etc.
This is my attempt:
class Lightbulb_Blastramp_Model_Observer {
public function sendOrderToBlastramp(Varien_Event_Observer $observer) {
$invoice = $observer->getEvent()->getInvoice();
$order = $invoice->getOrder();
$shipping_address = $order->getShippingAddress();
$billing_address = $order->getBillingAddress();
$items = $invoice->getAllItems();
$total = $invoice->getGrandTotal();
return $this;
}
}
I tried doing a print_r on all those variables, and ended up getting a lot of data back. Could someone point me in the right direction of getting the shipping address of an order?
Thanks!
Many Magento objects are based on Varien_Object, which has a method called getData() to get just the usually interesting data of the object (excluding the tons of other, but mostly useless data).
With your code you could either go for all the data at once:
$shipping_address = $order->getShippingAddress();
var_dump($shipping_address->getData());
or directly for specific single properties like this:
$shipping_address = $order->getShippingAddress();
var_dump(
$shipping_address->getFirstname(),
$shipping_address->getLastname(),
$shipping_address->getCity()
);
To understand how this works, I'd recommend to make yourself more familiar with the Varien_Object and read a bit about PHPs magic methods, like __call(), __get() and __set().
Try print_r($shipping_address->toArray());