Laravel Query DB Get Results from 2 Users with ID - laravel

I work on an Message System and i want Query the conversation between 2 Users. I cant use Controller now, soo i must try it on this way.
I want display the Results for example Title between my User ID Auth::user and the Senders ID.
Where "to" is my ID= Auth::user : ->where('to',
How can i change this Code, because with my Code i see all Messages from me and not only between my ID and the Other part ID:
<?php $subjects = DB::table('messages')->where('to', '=', Auth::user()->id)->pluck('title'); echo $subject; ?>
Thanks

You can add a second where clause, just chain them like this
<?php $subjects = DB::table('messages')->where('to', '=', Auth::user()->id)->where('from', '=', $senders_id)->pluck('title'); echo $subject; ?>
Check documentation for more information about query building
https://laravel.com/docs/5.5/queries#where-clauses

Related

Many to many Eloquent

Ok so I tried whith your answers, I can return Invoices associated to a user but i cant get the products inside this invoice, I want to get the Invoice associated to the user with his products only.
If i mention the id of the user who creates a product and the id of the invoice, i want the query to return me the invoice with the products associated to the id of the user mentionned.
when Iam using this :
$Invoices = Commande::find(1)->whereHas('Product', function($query){
return $query->where('products.user_id', 3);
})->with('Product')->get();
I get only the Invoices.
But i want the Invoice and the products in this invoice that are associated to a certain user.
That's where the whereHas() method comes into place. You should be able to do something like this:
$user = User::find($userId); // Let's assume `$userId` is `1`
$invoices = Invoice::whereHas('products', function ($query) use ($user) {
return $query->where('products.user_id', $user->id);
})->get();
That will return all Invoice records that are associated with the $user record returned by User::find($userId) (again, in this case, user_id: 1)
You can see the full documentation here:
https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence
Note: You might need to adjust the model/relationship names I used in the code above to fit your needs, but the basic idea remains the same.
You can use this query to get invoices and their products belonging to a specific user
// my user id is currently authenticated, user
Invoice::query()->where('user_id', auth()->user()->id)->with('products')->paginate();

Laravel Eloquent Only Get One Row

So I want to show id from order table where user id is the same as currently login user id, then later used to show orders have been made by the user
$orderId = Order::select('id')->firstWhere('user_id', auth()->id())->id;
$orders = SubOrder::where('order_id', $orderId)->orderBy('created_at', 'desc')->get();
it works but it only shows the first record, after some digging later I found out that the problem is on the $orderId, it only shows the first record. but I want it to be all the records. if I change the id to get(), it shows nothing since it give the result like "id = 1" instead of the number only. also have tried to change the firstWhere into where and got error like "Property [id] does not exist on the Eloquent builder instance."
please help, thanks
If you are going to use the other Orders associated with the User soon after you get the first Order, return all the relevant Orders and then just grab the first one when you need it.
$orders = Order::where('user_id', auth()->user()->id)->get();
$firstOrder = $orders->first();
$subOrders = SubOrder::whereIn('order_id', $orders->pluck('id'))->get();
Alternatively, you could use a subOrders relationship defined on your Order model.
class Order extends Model
{
public function subOrders()
{
return $this->hasMany(SubOrder::class);
}
}
$orders = Order::where('user_id', auth()->user()->id)->get();
$firstOrder = $orders->first();
$firstOrderSubOrders = $firstOrder->subOrders;
If you're confident you're going to be working with SubOrder records, you can use eager loading on your Order to improve performance.
$orders = Order::where('user_id', auth()->user()->id)
->with('subOrders')
->get();
$firstOrder = $orders->first();
$firstOrderSubOrders = $firstOrder->subOrders;
first() will return the first id queried and stop execution.
$firstOrder = $orders->first();

Laravel - Get users name from ID

I have a Laravel 5.6 app that is logging various information into a database table, I am then displaying this data in a HTML view. I am calling this view like this...
$logs = Log::all();
return view('logreport.index')->with('logs', $logs);
The table only contains the user ID, is there a way to get the users first name and last name for the users table? Or do I need to set a relationship up?
if the log table contain the user_id and you want to get user name you can get the user name through relation as the following:
in log model add the following method:
public function user(){
return $this->belongsTo('App\User','user_id','id');
}
and then you can access the user information in view file such as firstname as the following
#foreach($logs as $log)
<h1>{{$log->user->first_name .' '.$log->user->last_name }}</h1>
#endforeach
You don't need to set a relationship. Just use eloquent.
$users = User::whereIn('id', array(...))->get();
Or with query builder
$users = DB::table('users')->whereIn('id', array(...))->get();
Replace the dots by the values of your IDs
You can use Eloquent relationship as answered by Ali or you can use "Joins" with your result as:
$logs = Log::join('users', 'users.id', '=', 'logs.user_id')->get();
return view('logreport.index')->with('logs', $logs);
and then access the result as:
<h1>{{ $log->first_name .' '. $log->last_name }}</h1>

Form Model Binding with relationship

I already looked at this post but I can't seem to make it right:
Laravel form model binding
I get this error:
https://gyazo.com/2ea7b7bb6a19d588829447ee1a92053e I use laravel 5.2
for this.
some screenshots:
https://gyazo.com/1b2c35e660dfe1aae69a02703733d083
https://gyazo.com/3d6f294473f6e54650a4a4403dc2777e
https://gyazo.com/a59aebc7362f51f9ac27852ea032f962
To expand on my comment:
Your problem is here:
$user = User::where('id', $id) // Here more specifically, Laravel does not know if you mean id of users table or details table
->leftJoin('user_details', user_details.user_id', '=', 'users.id')
->first();
Rewrite your where statement like this:
$user = User::where('users.id', $id)....
And it should work. Basically since you're joining 2 tables and they both got id you need to specify which id you want to query by.

database fetch value show in dropdown using laravel 4

hello I am in big trouble please help me about database fetch value show in drop down using laravel 4 my code is below
<?php //echo "<pre>"; print_r($user);exit; ?>
{{ Form::select($user, $user ) }}
in print_r($user); showing all database value fetch from database but i want to show only value = id , user name field I try for each but not working any idea how to show value=id , user name filed
and result showing in to the drop down like this
<select name="[{"id":"1","username":"skaka","name":"sandip","avatar_id":"1","gender":"1","dob":"1989-10-20","grade":"A","school":"kbpv","created_at":"2014-07-22 08:45:59","updated_at":"2014-07-22 08:48:04","parent_email":""},{"id":"2","username":"ttt","name":"test2","avatar_id":"2","gender":"2","dob":"1989-10-20","grade":"A","school":"kbpv1","created_at":"2014-07-22 08:58:25","updated_at":"2014-07-22 08:58:25","parent_email":""}]">
In order to use {{Form::select()}} the easiest way to achieve this is by calling your data from the database with lists() method.
So
$users = User::lists('username', 'id');
Now you have an array with data like this:
$users = [1 => 'skaka', 2 => 'ttt', ....];
So you are ready to use efficiently Form::select() by doing:
{{Form::select('dropdown_name', $users)}}

Resources