how to retrieve data from db for registation on laravel?
I have two tables database, users and kode_instansi, I want to continue at the time of registration there is a form select that displays all data from the table kode_instansi. help master
Controller:
View:
Please follow the official documentation of laravel5. This page will clearly help you in retrieving results from DB.
The page clearly explains the different ways to retrieve data:
Retrieving All Rows From A Table
$users = DB::table('users')->get();
Here users is the table and this particular query would get you all the data from the users table.
Similarly,
Retrieving A Single Row / Column From A Table
$user = DB::table('users')->where('name', 'John')->first();
I would also suggest you to configure the database in order to use the database in laravel. Please perform this step initially.
Related
new to laravel.
My use case:
Update multiple rows (say: resources table).
Create multiple users (users table).
Retrieve ids of created users
What I currently did:
First, Update the resources table whereIn('id', [1,2,3,4]). (Update eloquent)
Second, Get array of the updated resources (Another eloquent: Resource::whereIn('id', [1,2,3,4])->get()->toArray())
Bulk create a users. Refers to the resources collection above. (Another eloquent: User::create($resources))
Lastly, get the ids of the created users (Not resolved yet. But I might have to use another eloquent query)
All in all, there are 4 Eloquent queries, which I want to avoid, because this might have performance issue.
What I wanted to do is that, On first step(Update), I should be able to get a collection of models of the affected rows (But I can't find any reference on how to achieve this). And then use this collection to create the users, and get the users ids in one query with User::create() so that I will have 2 queries in total.
There is no need to invent performance problems that do not exist.
Update or Insert return only count of affected rows. Commands Select, Insert, Update performed separately. This is a SQL issue, not Laravel.
For single inserts (if you want add one row) you can use insertGetId method of a model.
For example,
$id = User::insertGetId([
'email' => 'john#example.com',
'name' => 'john'
]);
But you get only ID of record. You need to run select to get full data of the row.
To save multiple records with one query you can use
DB::table('table_name')->insert($data);
Since this won't be an eloquent method, you should pass all the columns including created_at and updated_at.
I don't know what is the method name for update.
How to combine POST and UPDATE commands in one action in laravel 8 with ajax?
I have one column "status" in another table that I need to update when I save the data.
In this case basically I have two different tables... thank you for your answer!
Update from the comments:
I have two different database tables, let's say the "Quotation" and "Detail Quotation" tables, where in the "Detail Quotation" table there is a Status column that contains TRUE or FALSE...
Well, I want when I add data to the Quotation table, I also run the command to change the contents of the status column in the "Detail Quotation" table.
you must use observer for this case
https://laravel.com/docs/8.x/eloquent#observers
When you save the POST data in table. Laravel return ID then, with that ID you can call the update function for another table to update the status.
I am making a multi-vendor ecommerce website using Laravel 7. I am trying to make an Order History page where in a single can user can view only his/her own orders. I am having a problem building the query on my OrderHistoryController. These are the codes on my controller:
$users = User::get();
$orders = Order::find($users);
return view('order-history', compact('orders'));
And I'm trying to loop $orders on my blade file. I have:
#foreach ($orders as $order)
<div>{{ $order->id}}</div>
<div>{{ $order->grand_total}}</div>
#endforeach
I am trying to pass the order id and order grand total to my view. I don't get any error however it shows a different order details from a different customer. How do I do this?
You should use MySQL (or any other database you are using) table relationships and foreign keys.
So, your orders table, should have columns id, user_id, date, and so on. The important thing is user_id column. You want to make that column a foreign key. That basically means that user_id will hold an id value from another table, in this case user. See how to make foreign key columns in laravel migrations here.
Next up is to use Laravel's built in model relationships. You can read more about them here. Basically, in your orders model you want to have a function user which returns $this->belongsTo(App\Models\User::class) and in your user model you want to have a function orders which returns $this->hasMany(App\Models\Order::class) (or whatever the namespace is for both of them).
That way, you can call $user->orders and get a collection of Order models which will contain only the orders of that particular user.
Definitely read the documentation carefully and learn basic concepts of the framework first and how relational databases function!
You can get a lot of this information just by googling it or reading the documentation of Laravel or any other framework you plan on using! Good luck learning :)
In my application a user can add other users to the "favorites" list. All information about favorite users is stored in a table with the following structure:
user_id
favorite_id
In addition, each user has its services with prices. There are users and services tables in my application that are related to each other with service_user table which has the following structure:
user_id
service_id
price
In order to get all favorite users of a particular user with their services and prices I do the following:
$user->favorites()->with('services')->get();
However, there's no price column in each service object of the resulting list of services (this column is located in pivot).
Therefore, I want to know if there's a way to get the price column as part of each service object, instead of being in pivot table. The following question already solves my problem, but I want to know if there is way to do this without map() and/or foreach() to make performance better?
Currently, I have the following solution:
$user->favorites()->with('services')->get()
->map(function ($favorite) {
foreach ($favorite['services'] as &$item) {
$item['price'] = $item->pivot->price;
}
return $favorite;
});
If there any particular column in tables where Magento stores this data? For example if i want to fetch the data from somewhere else. By running raw php mysql queries.
Not using Magento layer.
I know there is a function getTotal(). But hope you understand what I am trying to say.
Is there any way other than creating own custom API?
Thanks
You're probably looking for the sales_flat_order table. Specifically, if you want the sum of all sales taken by your store:
SELECT SUM(grand_total) FROM sales_flat_order WHERE is_active = 1;