Laravel Where Search Query - php

I have a Spaces and an Interests table.
I am currently able to get a list of the Space Id's saved as $spaceList but I want my $query variable to retrieve a list of interests that the space_id foreign key matches one of the space_id's from my $spaceList variable.
public function index() {
$user_id = auth()->user()->id;
$user = User::find($user_id);
$spaceList = Space::where('user_id', $user_id)->pluck('space_id')->toArray();
$query = Interest::where('space_id', $spaceList);
$interests = $query->get();
return view('dashboard')->with('space', $user->space)->with('interest', $interests);
}
Thanks, I've been at this for ages now.

you should use whereIn instead of where
$spaceList = Space::where('user_id', $user_id)->pluck('space_id')->toArray();
$query = Interest::whereIn('space_id', $spaceList);

In Laravel Eloquent, this is what the Querying Relationship Existence working on
You won't need the $spaceList variable here if it's not used in other places
$query = Interest::whereHas('spaces', function($query) use ($user_id) {
$query->where('user_id', '=', $user_id);
});
Please notice, to get this work, you'll need to declare the spaces one-to-many relation in the Interest Module
Should be something like this, more details see document here
namespace App;
use Illuminate\Database\Eloquent\Model;
class Interest extends Model
{
public function spaces()
{
// space_id is the column name in your space database table
// id the the foreign-key target, generally is the primary-key of space table
return $this->hasMany('App\Space', 'space_id', 'id');
}
}

Related

Null is given when trying to find data from pivot table

I have a Many To Many relationship between User & Wallet Models:
Wallet.php:
public function users()
{
return $this->belongsToMany(User::class,'user_wallet','wallet_id','user_id')->withPivot('balance');
}
User.php:
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
}
And the pivot table user_wallet goes like this:
Then at the Controller, I need to access the balance field:
public function chargeWallet(Request $request, $wallet, $user)
{
// $wallet is wallet_id (2) & $user is user_id (373)
$bal = Wallet::with("users")
->whereHas('users', function ($query) use ($user) {
$query->where('id',$user);
})->where('id', $wallet)->first();
dd($bal->balance);
}
But now I get null as the result of dd($bal->balance) !!
So what is wrong here? How can I properly get the balance ?
Since it is a Many to Many relationships, you have Users attached to many Wallets, and Wallets attach to many Users. For each relation between a single Wallet and a single User: you have a pivot value (pivot relation). From your query, you'll retrieve Wallet with Users, each user's having the pivot relation value attached. So to retrieve the pivot table data (for each relation) you have to use a loop (added a with callback to make sure that only Users with matching user_id are eager loaded with the Wallets):
$bal = Wallet::with(["users"=>function ($query) use ($user) {
$query->where('user_id',$user);
}])
->whereHas('users', function ($query) use ($user) {
$query->where('user_id',$user);
})->find($wallet);
To avoid repeating same callback (where user_id =) you can assign it to variable:
$callback = function ($query) use ($user) {
$query->where('user_id',$user);
};
then use it in your query:
$bal = Wallet::with(['users' => $callback])
->whereHas('users', $callback)->find($wallet);
and then use a foreach loop:
foreach ($bal->users as $value){
dd($value->pivot->balance);
}
or
if you only want to return the pivot value for the first User of the first Wallet of your query, then:
$user = $bal->users->first();
dd($user->pivot->balance);

How Laravel generates SQL queries

I have created a one-to-many relationship. Here are the model classes.
class Photo extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
}
class User extends Authenticatable
{
public function photos(){
return $this->hasMany('App\Photo');
}
}
Then I try to retrieve photos:
$photos = User::find(1)->photos->where('photo', 'ut.jpg')->first();
Here is a query log I got. I do not see the photo='ut.jpg'. So how laravel generate SQL?
select * from `photos` where `photos`.`user_id` = 1 and `photos`.`user_id` is not null
Try this
$photos = User::find(1)->photos()->where('photo', 'ut.jpg')->first();
must be use ->photos() instead of ->photos.
For see sql query use
$sql = User::find(1)->photos()->where('photo', 'ut.jpg')->toSql();
You queried all photos by using this:
$photos = User::find(1)->photos->where('photo', 'ut.jpg')->first();
By using User::find(1)->photos you receive a Laravel Collection. Those collections have a where method as well. So basically, you are running SQL to get all photos of User 1 and then you just filter that collection to only show you the item with photo ut.jpg.
Instead, you can use brackets to get the relationship, and then query that.
Your query then becomes
$photos = User::find(1)->photos()->where('photo', 'ut.jpg')->first();
Instead of naming it $photos you should name it $photo, as you are querying with first - which will result only in one object (or null).
Can you please try this:
$photo = 'ut.jpg';
$photos = User::find(1)->whereHas('photos', function ($query) use($photo){
return $query->where('photo', $photo);
})->first();
your query $photos = User::find(1)->photos->where('photo', 'ut.jpg')->first(); is incorrect, laravel didnt see the where condition if you do this
User::whereHas('photos', function($q) {
$q->where('photo', 'ut.jpg');
})->where('id',1)->first();
thats the correct query to get the user photo
You could:
Run A Select Query
$photos = DB::select('select * from photos where id = ?', [1]);
All this is well-documented in :
--https://laravel.com/docs/5.0/database

How to join table with req->id

public function getTourDetail(Request $req)
{
//Get link detail
$tour = Tour::where('id',$req->id)->first();
//I want to take location.city of the location table
$detail = Tour::join('location','tour.id_location','=','location.id')
->whereColumn([
['tour.id_location','=','location.id']
])
->get(array(
'tour.id as id_tour',
'location.image',
'tour.name',
'tour.id_location',
'location.city'
));
return view('page.tour-detail',compact('tour','detail'));
}
I would like to be able to combine two query statements to get information from the location table ($ detail) like the id of the link request ($ tour).
Since you use models, you can use Eloquent relationships to load related data. First, define a relationship in the Tour model:
public function location()
{
return $this->belongsTo(Location::class, 'id_location')
}
Then load Tour and get related location:
$tour = Tour::find($req->id);
$relatedLocation = $tour->location;
First thing, if you are using model then using eloquent relationship will be a better idea to deal with the situation like yours. But if you want to join your table then this will be the way:
public function getTourDetail($id)
{
$tour = Tour::where('id',$id)->first();
//I want to take location.city of the location table
$detail = DB::table('location')
->join('tour','tour.id_location','=','location.id')
->select(
'tour.id as id_tour',
'location.image',
'tour.name',
'tour.id_location',
'location.city'
)->get();
return view('page.tour-detail',compact('tour','detail'));
}
Note: if you are getting id from submitted form then replace first portion of the code with:-
public function getTourDetail(Request $request)
{
$tour = Tour::where('id',$request->id)->first();

Laravel With and wherePivot

I'm trying to extract all companies and contacts with pivot.main_contact = 1.
Tables:
Company: id, name
Company_contacts: id, company_id, contact_id, main_contact
Contacts: id, name
Model:
class Company extends Model
{
public function mainContact()
{
return $this->belongsToMany('App\Contact', 'company_contacts')
->wherePivot('main_contact', '=', 1);
}
}
Controller:
$query = Company::with('mainContact')->get();
This returns companies + ALL contacts for the companies and NOT ONLY the ones with main_contact = 1.
First, for reasons I'm unsure of, you need to add withPivot('main_contact'); to your relation. This will return main_contact in your collection under pivot
class Company extends Model
{
public function mainContact()
{
return $this->belongsToMany('App\Contact', 'company_contacts')
->withPivot('main_contact');
}
}
The second thing you need to do is use withPivot() while constraint eager loading like so:
$companies = Company::with(['mainContact'=> function($query){
$query->wherePivot('main_contact', 1);
}])->get();
I've checked it, it works.
Just to go a bit above and beyond. Sometimes you'll want to query a pivot table without knowing the value. You can do so by:
$companies = Company::with(['mainContact'=> function($query) use ($contact){
$query->wherePivot('main_contact', $contact);
}])->get();
Try to add withPivot():
return $this->belongsToMany('App\Contact', 'company_contacts')
->withPivot('main_contact')
->wherePivot('main_contact', '=', 1);
Company::with('mainContact')->get(); returns all compaines and all contacts. You should be doing $company->mainContacts()->get(); instead.
$companies=Company::all();
foreach($companies as $company)
{
print_r($company->mainContact()->get());
}
die;

Laravel Eloquent: How to order results of related models?

I have a model called School and it has many Students .
Here is the code in my model:
public function students()
{
return $this->hasMany('Student');
}
I am getting all the students with this code in my controller:
$school = School::find($schoolId);
and in the view:
#foreach ($school->students as $student)
Now I want to order the Students by some field in the students table. How can I do that?
You have a few ways of achieving this:
// when eager loading
$school = School::with(['students' => function ($q) {
$q->orderBy('whateverField', 'asc/desc');
}])->find($schoolId);
// when lazy loading
$school = School::find($schoolId);
$school->load(['students' => function ($q) {
$q->orderBy('whateverField', 'asc/desc');
}]);
// or on the collection
$school = School::find($schoolId);
// asc
$school->students->sortBy('whateverProperty');
// desc
$school->students->sortByDesc('whateverProperty');
// or querying students directly
$students = Student::whereHas('school', function ($q) use ($schoolId) {
$q->where('id', $schoolId);
})->orderBy('whateverField')->get();
you can add orderBy to your relation, so the only thing you need to change is
public function students()
{
return $this->hasMany('Student');
}
to
public function students()
{
return $this->hasMany('Student')->orderBy('id', 'desc');
}
To answer the original question, the students dynamic property can also be accessed as a relationship method.
So you have this to fetch all students:
$students = $school->students;
Now as a relationship method, this is equivalent:
$students = $school->students()->get();
Given this, you can now add in some ordering:
$students = $school->students()->orderBy('students.last_name')->get();
Since eloquent will be performing a join, make sure to include the table name when referencing the column to order by.
You can also add this to your students method if you want to set a default order that $school->students will always return. Check out the documentation for hasMany() to see how this works.
For Many to one relation I found one answer on:
https://laracasts.com/discuss/channels/eloquent/order-by-on-relationship
$order = 'desc';
$users = User::join('roles', 'users.role_id', '=', 'roles.id')
->orderBy('roles.label', $order)
->select('users.*')
->paginate(10);
this can save day... of anyone
You can use this like this:
$students = $school->students()->orderBy('id', 'desc');
You can also use
$students = $school->students()->orderBy('id', 'desc')->paginate(10);

Categories