Choose particular information depending on the chosen option

In the process of selecting a customer's name, I need to choose from a dropdown list and retrieve their order numbers where the status is 0.

Each order contains a list of vehicles such as T1, T2, T3...

After obtaining the order number, I then access the list of vehicles associated with that customer.

I am hoping to have three select options available for this task.

In my database, I have two related tables:

Customer

Id
Name

Customer Order

CustomerId
VehicleNumber
Status

Ultimately, my goal is to store the vehicle numbers after selecting customers from the dropdown.

Answer №1

To begin, implement the following:


class Customer extends Model
{
    public function orders()
    {
        return $this->hasMany(Order::class);
    }
}

class Order extends Model
{
    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

Within the controller method responsible for displaying the Customer name selection view, include the following code:

class CustomerController extends Controller
{
    public function show()
    {
        return view('some.view', ['customers' => Customer::get(['id', 'name']);
    }
}

Utilize the $customers variable to populate the select options on the view. Upon user interaction, send a request to the backend/server via ajax if using a JavaScript framework or through a plain POST request otherwise. Ensure to include the selected user's id.

Handle the incoming request within the controller as demonstrated below:


class SomeController extends Controller
{
    public function unfulfilledOrders(Request $request, $id)
    {
        $customer = Customer::findOrFail($id);        
        $orders = $customer->orders()->where('status', 0)->get();

        return view('orders.show', ['orders' => $orders, 'customer' => $customer]);
    }
}

Finally, make use of the $customer and $orders variables to render the view accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to align the export button group in DataTables?

Recently, I encountered an issue with aligning a DataTables export button group with the search input box. I have utilized Datatable to automatically render the export buttons within a table, but I am struggling to get them to align properly. Below is the ...

What is the functionality of this.$eval in Vue.js?

During the process of updating an unfamiliar old script from version 0.11 to 2.5.4, an alert popped up stating: To address the warning message saying 'Replace this.$eval('reportData | reportFilter false') with a solution using normal Java ...

Exploring the options for accepting various file formats with Swal SweetAlert

Currently, I am using Swal Sweet Alert within my Vue.js application. I have successfully implemented code to allow image files, but now I am seeking assistance on how to extend this functionality to include multiple file types such as PDFs, PPTs, and Doc ...

Encountered an error during the installation of the [email protected] script while running 'node build.js'

Encountering an error message when I try to run npm install on my project. Below is more information about the issue: npm ERR! <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="234d4c47460e5042505063130d1a0d15">[email p ...

React Native issue: why are my variables resetting mysteriously?

Hey there, I'm currently encountering a peculiar situation involving the usage of var and React.useState() variables. I am utilizing the library https://github.com/tongyy/react-native-draggable#readme to define 2 variables - var color1 = ''; ...

Is there a method available to define a custom auto-generated key when inserting an object into Firebase Realtime Database?

I've been working on pushing data to the firebase rtdb and I want to make sure I can easily access it from other places. Here's the code snippet that I'm using to add data to the rtdb: function addStudentHandler(studentData) { fetch( ...

Tips on updating pinned row information in AG Grid Vue

I have a specific row pinned to the bottom in the grid. Whenever I add or remove some rowData in the grid, I need to update that pinned row. Take a look at the code snippet below: this.gridApi.pinnedBottomRowData = rowNode.setDataValue(date, dataToPush); t ...

Exploring Angular14: A guide to efficiently looping through the controls of strictly typed FormGroups

Currently, I am working on upgrading my formGroups to be strictly typed in Angular v14. Within my FormGroup, there is a specific block of logic that iterates through all the controls and performs an action (this part is not crucial as I am facing issues be ...

Harness the Power of Asynchronous Binding in Knockout.js

I'm really struggling to figure out how to asynchronously bind my cascading Select2 drop-down fields using knockoutJS. Everything works perfectly fine when the data is static within the function, but as soon as I introduce an asynchronous ajax call, ...

Is it possible to obtain the impending exception handling protocol in advance?

In upcoming scenarios, unhandled promise rejections will lead to the termination of the Node.js process using a non-zero exit code. Despite encountering issues, my pipeline erroneously passed and deployed a faulty version that crashes upon launch. If Node ...

Obtain the value of v-model in a child component within VueJS

In my Vuetify App, I have implemented a Vue2Editor using a custom component called text-editor. Here is how it looks: <vue-editor :value="text" @input="updateText" ></vue-editor> The props for this component are defined as follows: props ...

What are the differences between Sitecore Analytics and Adobe Analytics? How do they each utilize JavaScript

I have limited knowledge about Sitecore Analytics (with MongoDB) and I am curious if there exists a Javascript API that can be utilized for non-Sitecore websites. If so, could you please direct me to the relevant documentation? Additionally, any insights ...

Error encountered while connecting to SQL Server from node.js causing the express server to crash due to a ConnectionError

Encountering an issue with node.js tedious error ConnectionError: Failed to connect to sqlserverip:1433 causing unexpected crashes in my express server. Seeking suggestions on how to prevent such crashes. <a href="/cdn-cgi/l/email-protection" class="__ ...

Fetching information using JSON for creating a RangeArea Chart with CanvasJS

I'm currently working on creating a range area chart with CanvasJS and PHP to fetch data from a database. After setting up the php script to retrieve values from the DB, here's what I have: <?php header('Content-Type: application/json&a ...

Issue with ng-maxlength directive

In my Angular 4 Application, I am attempting to implement validation for form inputs. Specifically, I want to restrict inputs with numbers to not accept more than 4 digits. If the input contains more than 4 digits, the form should be marked as invalid. I a ...

Detecting incorrect serialized data entries based on data types

In the scenario where the type MyRequest specifies the requirement of the ID attribute, the function process is still capable of defining a variable of type MyRequest even in the absence of the ID attribute: export type MyRequest = { ID: string, ...

What is the best way to retain data after clicking a button?

I am facing an issue where I need to figure out how to add information to a new page when a button is clicked. For example, let's say I have an "add to cart" button and upon clicking it, I want to store some data. How can I achieve this functionality? ...

Control the outcome of ajax response

I have implemented an ajax post method to retrieve data from the backend. $.ajax({ type: "POST", url: URL_one, data: submitData }).then(function (response) { console.log("Ajax response", response); }); Upon inspecting th ...

Update the page when the React route changes

I am facing an issue with a function in a component that is supposed to load certain variables when the page is fully loaded. Interestingly, it works perfectly fine when manually reloading the page. However, if I use a NavLink to navigate to the page, the ...

Is it the browser's responsibility to convert ES6 to ES5 internally?

Given the support for ES6 in modern browsers, do they internally convert ES6 to ES5 before executing the code? Or can they process ES6 natively using a C++ engine? If they are able to run ES6 directly, how do they guarantee that executing ES6 code produce ...