Hi there I have two routes that look identical except that one has an extra paramter called genre how do make them call their on routines and not have them mixed up.
Route::get('browse/{product_slug}', array(
'as' => 'products.view_by_producttype',
function($slug)
{
}
))->where('product_slug', '[A-Za-z\-]+');
Route::get('browse/{producttype_slug}/{genre_slug}', array(
'as' => 'products.view_by_producttype_genre',
function($productype_slug, $genre_slug)
{
}
))->where('producttype_slug', '[A-Za-z\-]+')->where('genre_slug', '[A-Za-z\-]+');
Upated Code and order:
Route::get('browse/{producttype_slug}', array(
'as' => 'products.view_by_producttype',
function($producttype_slug)
{
$producttype = ProductTypes::where('slug', '=', $producttype_slug)->firstOrFail();
$items = DB::table('products')->join('productvariations', function($join) {
$join->on('productvariations.product_id', '=', 'products.id');
})->where('producttype_id', '=', $producttype->id)->paginate(1);
return View::make('products.view_by_producttype')->with(compact('items', 'producttype'));
}
))->where('producttype_slug', '[A-Za-z\-]+');
Route::get('browse/{producttype_slug}/{genre_slug}', array(
'as' => 'products.view_by_producttype_genre',
function($producttype_slug, $genre_slug)
{
$producttype = ProductTypes::where('slug', '=', $producttype_slug)->firstOrFail();
$genre = GenreTypes::where('slug', '=', $genre_slug)->firstOrFail();
$items = DB::table('products')->join('product_genretypes', function($join) {
$join->on('product_genretypes.product_id', '=', 'products.id');
})->join('productvariations', function($join) {
$join->on('productvariations.product_id', '=', 'products.id');
})
->where('genretype_id', '=', $genre->id)
->where('producttype_id', '=', $producttype->id)
->paginate(1);
return View::make('products.view_by_type_genre')->with(compact('items', 'producttype', 'genre'));
}
))->where('producttype_slug', '[A-Za-z\-]+')->where('genre_slug', '[A-Za-z\-]+');
Just swap the order - have the Genre route defined first - like this:
Route::get('browse/{producttype_slug}/{genre_slug}', array(
'as' => 'products.view_by_producttype_genre',
function($productype_slug, $genre_slug)
{
}
))->where('producttype_slug', '[A-Za-z\-]+')->where('genre_slug', '[A-Za-z\-]+');
Route::get('browse/{product_slug}', array(
'as' => 'products.view_by_producttype',
function($product_slug)
{
}
))->where('product_slug', '[A-Za-z\-]+');
That way - if a route matches with a Genre route - it will be used first.
Otherwise it will default back to the normal Product route.
Related
This question already has answers here:
Laravel - Method Illuminate\\Support\\Collection::makeHidden does not exist
(4 answers)
Closed 1 year ago.
I want to hide the columns password & OTP ,that is included in $users result. Actually these 2 columns are part of the users table. My ultimate need is that i need to join 3 tables : users,location,user_technical_details and want to hide the password & OTP columns in the users table. Can use any methods. Through any methods, i want to attain this result I've tried many methods. Nothing works. How to solve this? Any suggestions..
Things i tried:
1)
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
$d=$users->makeHidden(['password','OTP']);
return response()->json([
'message' => 'profile viewed successfully',
'data' => $d,
'statusCode' => 200,
'status' => 'success'],200);
This generates the error - Method Illuminate\\Support\\Collection::makeHidden does not exist
2)
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
$exclude_columns=['password','OTP'];
$get_columns = array_diff($users, $exclude_columns)->get();
return response()->json([
'message' => 'profile viewed successfully',
'data' => $get_columns,
'statusCode' => 200,
'status' => 'success'],200);
3)
$users = DB::table('users')
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
$d=collect($users->toArray())->except(['password','OTP']);
return response()->json([
'message' => 'profile viewed successfully',
'data' => $d,
'statusCode' => 200,
'status' => 'success'],200);
4)
protected $hidden = ['password','OTP'];
5)
$users = DB::table('users')->exclude(['password','OTP','ph_OTP','email_OTP','user_access_token','remember_token'])
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
return response()->json([
'message' => 'profile viewed successfully',
'data' => $users,
'statusCode' => 200,
'status' => 'success'],200);
This generates the Error -Call to undefined method Illuminate\\Database\\Query\\Builder::exclude()
when you want to limit the attributes, such as passwords, that are included in your model's array or JSON representation. To do so, add a $hidden property to your model. In attributes that are listed in the $hidden property's array will not be included in the serialized representation of your model:
class User extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = ['password','OTP'];
}
now in your code you have to use User model instead of DB facade:
$users = User::query()
->join('location', 'users.id', '=', 'location.id')
->join('user_technical_details', 'users.id', '=', 'user_technical_details.id')
->get();
now, $users will not have the hidden attributes
How do I search within a BelongsToMany Relationship within laravel?
Looks like I seem to be understanding something wrong
Below is my code:
public function search(Request $request, Project $post)
{
$this->validate($request, [
'query' => 'required'
]);
$query = $request->input('query');
$post = Post::where('title', 'like', "%$query%")
->orWhere($post->tags()->role, 'like', "%$query$%")
->paginate(6);
return view('post.search', [
'post' => $post,
]);
}
If you want to search into a relationship you may use whereHas or orWhereHas methods.
Your search function may be ok like this.
public function search(Request $request, Project $post)
{
$this->validate($request,[
'query' => 'required'
]);
$query = $request->input('query');
$post = Post::where('title', 'like', "%$query%")
->orWhereHas('tags', function($q) use ($query) {
$q->where('role', 'like', "%{$query}%");
})->paginate(6);
return view('post.search',[
'post' => $post,
]);
}
I am working on laravel/php in which I am checking how the method that is being used in the controller to return the view.
public function client($uuid) {
$client = clients::with([
'client_details' => function($q){
$q->with([
'client_ratings' => function($q){
$q->with(['rating_review',
'rating_user' => function($q){
$q->select('user_id','first_name','last_name');
},
'rating_user_media' => function($q) {
$q->select('client_id','owner_id','url')->where('media_type','image')->where('context','user');
}]);
$q->orderBy('created_at', 'desc');
$q->take(2);
}]);
},'owner_details','media','cancellation_policy'])->where('uuid',$uuid)->first();
$groups = groups::getgroups(NULL);
$data = [
'groups' => $groups,
'client' => $client
];
//echo '<pre>';var_dump(json_decode(json_encode($data)));exit;
return View::make('client',['data'=>$data]);
}
Problem Statement:
The above php code is creating the JSON but I am not sure how is this JSON getting created.
I have this query:
User::leftJoin('friends', function ($join) {
$join->on('friends.user_id_1', '=', 'users.id')
->orOn('friends.user_id_2', '=', 'users.id');
})
->where(function ($query) use ($myID) {
// Group orwhere functions so the query builder knows these belong together
$query->where([
'friends.user_id_1' => $myID,
'friends.accepted' => true
])
->orWhere([
'friends.user_id_2' => $myID,
'friends.accepted' => true
]);
})
->where('users.id', '!=', $myID) // Exclude the user with id $myID
->get();
https://stackoverflow.com/a/41832867/5437864
I want to use this query twice, but with a different where clause. Is it possible to reuse this query without copying the whole code? And if so, how?
I used the PHP clone keyword. I'm not sure if this is the best solution, but it helps. Any other suggestions are welcome.
$friends_query = User::leftJoin('friends', function ($join) {
$join->on('friends.user_id_1', '=', 'users.id')
->orOn('friends.user_id_2', '=', 'users.id');
})
->where(function ($query) use ($myID) {
// Group orwhere functions so the query builder knows these belong together
$query->where([
'friends.user_id_1' => $myID,
'friends.accepted' => true
])
->orWhere([
'friends.user_id_2' => $myID,
'friends.accepted' => true
]);
});
$friends_me = clone $friends_query;
$friends_me = $friends_me->where('users.id', '!=', $myID);
$friends_others = clone $friends_query;
$friends_others = $friends_others->where('users.id', '=', $myID);
$query = User::leftJoin('friends', function ($join) {
$join->on('friends.user_id_1', '=', 'users.id')
->orOn('friends.user_id_2', '=', 'users.id');
})
->where(function ($query) use ($myID) {
// Group orwhere functions so the query builder knows these belong together
$query->where([
'friends.user_id_1' => $myID,
'friends.accepted' => true
])
->orWhere([
'friends.user_id_2' => $myID,
'friends.accepted' => true
]);
});
Adding extra where to existing query
$query->where('users.id', '!=', $myID)
$query->get();
Check this link for another example
Example
I have Eloquent statement in Laravel 4.2 that looks like this
$user_message_block = Message::where('responder_id', '=', Auth::user()->id)
->where('user_id', '=', $user->id)->first();
Then if $user_message_block doesn't exist I also have to check for a reverse case scenario and I do it like this...
if(!$user_message_block){
$user_message_block = Message::where('responder_id', '=', Auth::user()->id)
->where('user_id', '=', $user->id)->first();
}
What I really would like to do is run a single query that checks for both scenarios at once..
In pseudo expression I need something like this:
$user_message_block = Message::where('responder_id', '=', Auth::user()->id,
'AND', 'user_id', '=', $user->id,
'OR', 'responder_id', '=', $user->id,
'AND', 'user_id', '=', Auth::user()->id)->first();
So basically I need to
SELECT Message where (responder_id=x AND user_id=y) OR where (responder_id=y AND user_id=x)
How could I do this using Eloquent. I am unable to find more about OR and AND statements used with Eloquent.
Thanks!
UPDATE:
After more looking I found that this seems to work (still testing a lot)
$user_message_block =
Message::where(['responder_id' => Auth::user()->id, 'user_id' => $user->id])
->orWhere(['user_id' => Auth::user()->id, 'responder_id' => $user->id])
->first();
OR
$user_message_block =
Message::where(['responder_id' => Auth::user()->id, 'user_id' => $user->id])
->orWhere(['user_id' => Auth::user()->id, 'responder_id' => $user->id])
->get();
Are there any drawbacks to this that I need to consider?
It may be very complicated to do in Eloquent but this does exactly what you need using Laravel's query builder without reverting to raw SQL.
DB::table('messages')
->where(function($query)
{
$query->where('responder_id', '=', 'x')
->where('user_id', '=', 'y');
})
->orWhere(function($query) {
$query->where('responder_id', '=', 'y')
->where('user_id', '=', 'x');
})
->get();
This is what I get from running a toSql() on it:
select * from `messages` where (`responder_id` = ? and `user_id` = ?) or (`responder_id` = ? and `user_id` = ?)
Try this to see if it works with Eloquent.
Message::where(function($query) {
$query->where('responder_id', '=', 'x')
->where('user_id', '=', 'y');
})->orWhere(function($query) {
$query->where('responder_id', '=', 'y')
->where('user_id', '=', 'x');
})->get();
It should work.
I credited Noah for the solution on account of providing a working solution and the effort he made in helping me out. I did end up using a different solution however..
$user_message_block =
Message::where(['responder_id' => 'x', 'user_id' => 'y'])
->orWhere(['user_id' => 'x', 'responder_id' => 'y'])
->get();
In my speciffic case I only need the first record found so this works for me
$user_message_block =
Message::where(['responder_id' => 'x', 'user_id' => 'y'])
->orWhere(['user_id' => 'x', 'responder_id' => 'y'])
->first();
Suffice it to say,, I found a solution similar to mine in phpacademy videos by Alex. And I stumbled upon this too Laravel eloquent SQL query with OR and AND with where clause