Laravel: 1066 Not unique table/alias - php

I have two queries in my controller
if (isset($input['council']) && $input['council'] != '')
{
$query = $query->join('suburb_near', 'titles.suburb', '=', 'suburb_near.suburb')
->select(array('titles.*', 'suburb_near.suburb' , 'suburb_near.council'))
->where('council', 'like', '%'. $input['council'].'%')
->orderBy('views', 'desc');
}
if (isset($input['country']) && $input['country'] != '')
{
$query = $query->join('suburb_near', 'titles.suburb', '=', 'suburb_near.suburb')
->select(array('titles.*', 'a.suburb' , 'suburb_near.country'))
->where('country', 'like', '%'. $input['country'].'%')
->orderBy('views', 'desc');
}
If I run either independently they run fine. But if I run both together, I get an error:
1066 Not unique table/alias
How should I change this?

Use table alias (as)
The first query would look like:
$query = $query
->join('suburb_near as suburb_near_one', 'titles.suburb', '=', 'suburb_near_one.suburb')
->select(array('titles.*', 'suburb_near_one.suburb' , 'suburb_near_one.council'))
// Edit after user3664594s comment
->where('suburb_near_one.council', 'like', '%'. $input['council'].'%')
->orderBy('views', 'desc');
The second query would look like:
$query = $query
->join('suburb_near as suburb_near_two', 'titles.suburb', '=', 'suburb_near_two.suburb')
->select(array('titles.*', 'a.suburb' , 'suburb_near_two.country'))
// the rest of the query

Related

Can anyone help me to solve an ambiguous error when search?

$students = Student::select('students.*', 'users.email')->join('users', 'students.user_id', 'users.id')->orderBy("id", "desc")->skip($page * $pageSize)->take($pageSize);
if (request('se') != "" || request('se') != null) {
$se = request('se');
$se = str_replace("+", " ", $se);
$students = $students
->where(function ($q) use ($se) {
$q->where('students.student_number', 'like', '%' .$se. '%')
->orWhere(DB::raw("CONCAT(`first_name`, ' ', `last_name`)"), 'LIKE', '%' . $se . '%')
->orWhere('students.gender', $se)
->orWhere('students.phone', 'like', '%' .$se. '%')
->orWhere('email', 'like', '%' .$se. '%');
});
}
$students = $students->orderBy('id', 'DESC')->get();
when I search in the list I got Integrity constraint violation: 1052 Column 'first_name' in where clause is ambiguous error.
This got when join the table because both students and users table have first name and last name columns, but I have retrieved from only students table, I didn't understand why this appears again!
Are there any better solution for this?
Change the first_name and last_name to students.first_name and students.last_name respectively

MySQL query to Laravel 5.5

SELECT * FROM table WHERE age = 10 AND (first_name like %name% OR last_name like %name%);
How do you do this in Laravel 5.5?
Using query builder:
$results = DB::table('table')
->where('age', 10)
->where(function ($query) {
$query
->where('first_name', 'like', '%name%')
->orWhere('last_name', 'like', '%name%');
})
->get();
Check the official documentation (5.5) for Query Builder
You can do something like this to query
$data= DB::table('table')
->whereRaw('age'= ? AND (first_name like ? OR last_name like ?)', [10,'%'.name.'%','%'.name.'%' ])
->get();'
Official documentation to select https://laravel.com/docs/5.6/queries#selects
I don't know how many field do you use in your view.. but if you have firstname field and lastname field, then your code should be like this:
$results = DB::table('table')
->where('age', 10)
->where(function ($query) use ($request) {
$query
->where('first_name', 'LIKE', '%' . $request->first_name .'%')
->orWhere('last_name', 'LIKE', '%' . $request->last_name .'%');
})
->get();

laravel DB:table for multi where AND Or condition for multiple slected field

My query like :
$results = User::
where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
Solution for the multi where condition in laravel ,because its not working currently
->where('twd.status','=','0')->where(function($q) use($data){
$q->where('A','like', '%' .$data. '%')
->orWhere('B','like', '%' .$data. '%');
->orWhere('C','like', '%' .$search_param. '%');
})
You can do it like that .
Concat all column that you want to search
->where(DB::raw("concat(A,' ',B)"), 'like', "%".$data."%");

Join to leftJoin queries in Laravel

I have a query that Im trying to retrieve. Its suppose to get a listings information with its trips and location details.
This is how Im calling the query inside the Destinations controller:
public function destinations($id) {
$destination = Destination::findOrFail($id);
$listingGuides = Listing::findGuidesTrips($destination)
->with('locations')
->withCount('trips')
->get();
return view('destinations.index', compact('listingGuides');
}
And the findGuidesTrips method is inside the Listings Model:
public static function findGuidesTrips($destination) {
$query = self::query()
->leftJoin('trips', 'listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
)
->groupBy('listings.id');
$query = self::query()
->leftJoin('locations', 'listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('locations.longitude as longitude')
)->addSelect(
\DB::raw('locations.latitude as latitude')
);
$query = $query->whereHas('locations',function($query) use ($destination) {
$query->where('region', 'like', $destination->location)->orWhere('country', $destination->location);
});
return $query;
}
This is what I get back:
As you can see, I have 2 $query = self::query() quires, but only one is being called (the bottom one). Its ignoring the top self::query.
I was just wondering how would I go about combining these 2 leftJoin queries into one perhaps? Or is there a better way of doing this query?
( I tried doing this: )
$query = self::query()
->leftJoin('trips', 'listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
)
->leftJoin('locations', 'listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('locations.longitude as longitude')
)->addSelect(
\DB::raw('locations.latitude as latitude')
)->groupBy('listings.id');
But it gives me Integrity constraint violation: 1052 Column 'listing_id' in on clause is ambiguous error
As stated by #Tim Lewis and #Niklesh, all I had to do was:
trips.listing_id for first query and locations.listing_id for second.
Here is the final query:
public static function findGuidesTrips($destination) {
$query = self::query()
->leftJoin('trips', 'trips.listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('listings.name,listings.slug,listings.type,listings.id,MIN(trips.cost) as starting_price')
)
->leftJoin('locations', 'locations.listing_id', '=', 'listings.id')
->addSelect(
\DB::raw('locations.longitude as longitude')
)->addSelect(
\DB::raw('locations.latitude as latitude')
)->groupBy('listings.id');
$query = $query->whereHas('locations',function($query) use ($destination) {
$query->where('region', 'like', $destination->location)->orWhere('country', $destination->location);
});
return $query;
}

Eloquent grouped orWhere

Currently have an Eloquent statement:
$contacts = Contacts::where('lname','LIKE',$searchquery.'%')
->orWhere('fname','LIKE',$searchquery.'%')
->orWhere('phone','LIKE','%'.$searchquery)
->where('active','=',1)->get();
It is treating it as
select
*
from
contacts
where
lname like $searchquery+'%'
or lname like $searchquery+'%'
or lname like $searchquery+'%'
and active = 1
what I am needing is
select
*
from
contacts
where
(lname like $searchquery+'%'
or lname like $searchquery+'%'
or lname like $searchquery+'%')
and active = 1
How do I go about grouping in Eloquent? I have found a couple examples such as:
DB::table('users')
->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})
->get();
But I am only used to Eloquent, not Laravels DB Query builder. I tried adapting the Eloquent form to this
$contacts = Contacts::->where('active', '=', '1')
->where(function ($query) {
$query->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('phone', 'LIKE', '%'.$searchquery);
})
->get();
No success as it does not recognize the $searchquery inside the function.
What am I missing?
So, this is what you have to do:
DB::table('users')->where(function($query) use ($searchQuery){
$query->where('lname', 'LIKE', $searchQuery . '%')
->orWhere('fname', 'LIKE', $searchQuery . '%')
->orWhere('phone','LIKE', '%' . $searchquery);
})
->get();
Note that I've put use ($searchQuery) so it can be used inside the closure
When you use orWhere with multiple where clauses, you need to be careful!
Where clauses after orWhere doesn't effected to sibling where clauses before orWhere clauses such as where, whereHas, whereDoesntHave, orWhereHas
also you have to pass $searchquery variable inside to function in where clause using use ($searchquery)
Contacts::->where(function ($query) use ($searchquery) {
$query->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('lname', 'LIKE', $searchquery.'%')
->orWhere('phone', 'LIKE', '%'.$searchquery);
})
->where('active', '=', '1')
->get();

Categories