this is my search function in users controller
public function search()
{
if( $search = \Request::get('q') ){
$users = User::where( function($query) use ($search){
$query->where('name','LIKE',"%$search%");
});
}
return $users;
}
this is the vuejs code .Fire is new instance of vuejs that i created in app.js
Fire.$on('searching',() => {
let query = this.$parent.search; // query parent(app.js) for 'search'
axios.get('api/findUser?q='+query)
.then((data) => {
this.users = data.data;
})
});
You've constructed a query, but you haven't actually run it until you call ->get() on it.
return $users->get();
Related
I would like that normal users don't see the pictures with a status of 0 (not visible) only the visible ones (status 1) but that admins can see everything.
Is this kind of solution viable or is there something cleaner to do?
The gates/policies are not adapted for that, I don't see any other solution to my knowledge, that's why I come to you
Thanks in advance
public function show($name)
{
if(Auth::user()->isAdmin()) {
$model = cache()->remember('model_show'.$name, Config::get('cache.ttl'), function() use ($name) {
return Model::where('name', $name)->with('pictures')->first();
});
$pictures = $model->pictures()->latest()->paginate(18);
} else {
$model = cache()->remember('model_show'.$name, Config::get('cache.ttl'), function() use ($name) {
return Model::where('name', $name)->with('visible_pictures')->first();
});
$pictures = $model->visible_pictures()->latest()->paginate(18);
}
return view('model.model')->with(['model' => $model, 'pictures' => $pictures]);
}
You can clean it up by using when function in the query itself.
public function show($name)
{
$model = cache()->remember('model_show'.$name, Config::get('cache.ttl'), function() use ($name) {
return Model::where('name', $name)->when( Auth::user()->isAdmin() , function ($q)
{
return $q->with('pictures');
}, function ($q)
{
return $q->with('visible_pictures');
})->first();
});
$pictures = $model->pictures()->latest()->paginate(18);
return view('model.model')->with(['model' => $model, 'pictures' => $pictures]);
}
or we could also use arrow functions
public function show($name)
{
$model = cache()->remember('model_show'.$name, Config::get('cache.ttl'), function() use ($name) {
return Model::where('name', $name)->when( Auth::user()->isAdmin() ,
fn($q) => $q->with('pictures') ,
fn($q) => $q->with('visible_pictures')
)->first();
});
$pictures = $model->pictures()->latest()->paginate(18);
return view('model.model')->with(['model' => $model, 'pictures' => $pictures]);
}
I have a problem with my Laravel site. Basically I tried adding language switches to my Instagram clone and it showed this problem while logged in and tried go to my profile site
This is the code to the controller where it shows the problem:
public function index(User $user)
{
$user = User::find($user);
$follows = (auth()->user()) ? auth()->user()->following->contains($user->id) : false;
// Caching Post count
$postCount = Cache::remember(
'count.posts.' . $user->id,
now()->addSeconds(30),
function () use ($user) {
return $user->posts->count();
});
// Caching Followers count
$followersCount = Cache::remember(
'count.followers' . $user->id,
now()->addSeconds(30),
function () use ($user) {
return $user->profile->followers->count();
});
// Caching Following count
$followingCount = Cache::remember(
'count.following' . $user->id,
now()->addSeconds(30),
function () use ($user) {
return $user->following->count();
});
return view('profiles.index', compact('user' , 'follows', 'postCount', 'followersCount', 'followingCount'));
//return view('profiles.index', compact('user' , 'follows'));
}
If someone's interested in the project that I created, I followed this video:
https://youtu.be/ImtZ5yENzgE
I want to make a filter with query params, here I want to make 3 where, but if one of them is not there, then it will not be a problem because it will display according to the filter only, and if there is no query string then it will display all data
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d')
->where('d.status','=',$request->status)
->orderBy('d.id')
->get();
return response()->json($vendor);
}
Take as reference, exact code might not work for you.
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d');
if (!empty($request->status_one)) {
$vendor = $vendor->where('d.status','=', $request->status_one);
}
if (!empty($request->status_two)) {
$vendor = $vendor->where('d.status','=', $request->status_two);
}
if (!empty($request->status_three)) {
$vendor = $vendor->where('d.status','=', $request->status_three);
}
if (empty($request->status_one) && empty($request->status_two) && empty($request->status_three)) {
$vendor= $vendor->where('d.status','=', $request->status_one)->where('d.status','=', $request->status_two)->where('d.status','=', $request->status_three);
}
$result = $vendor->orderBy('d.id')
->get();
return response()->json($result);
}
public function VendorInfoFilter(Request $request)
{
$vendor = DB::table('schema.data as d')
->when($request->status, function ($q, $status) {
return $q->where('d.status','=', $status);
})
->when($request->status_two, function ($q, $status_two) {
return $q->where('d.status_two','=', $status_two);
})
->orderBy('d.id')
->get();
return response()->json($vendor);
}
public function index(Request $request)
{
$search_book = $request->id;
$proc=DB::select(DB::raw("SELECT * FROM BOOKS WHERE BOOKID = '$Search_book'")
if ($search_book!="") {
return response()->json($proc);
return view('status.status',[
'proc' => $proc
]);
}
How to return 2 data
To determine if a request is an ajax request, you can use the ajax() method on a Request object injected into the controll action:
public function index(Request $request)
{
$results = DB::table('books')
->where('bookid', $request->id)
->get();
if ($request->ajax()) {
return response()->json($results);
}
return view('status.status', [
'proc' => $results
]);
}
I went ahead and fixed the SQL injection vulnerability in your query for you by swapping the query for a proper one. It could still be improved by using a Book model instead of a plain database query, but it is fine this way as well.
The query from your comment can be simplified by replacing the left join. Simply take the sub query as base and right join it with processspj:
DB::table('processtrans as pt')
->leftJoin('processmaster as pm', 'pm.pcm_id', '=', 'pt.pct_pcm_id')
->rightJoin('processspj as ps', 'ps.pc_id', '=', 'pt.pct_pc_id')
->where('pt.pct_pc_id', $request->id)
->select([
'ps.*',
'pm.pcm_bname',
'pt.created_at',
'pt.updated_at',
'pt.pct_id',
'pt.pct_leadtime',
'pt.pct_pcm_id',
'pt.pct_pc_id',
'pt.pct_planfinishdate',
'pt.pct_startdate',
'pt.pct_status',
])
->get();
$(document).ready(function(){
$("#dl_books").change(function()
{
var getValue=$(this).val();
$.ajax({
type: 'GET',
url: '{{route('status')}}',
data: {id:getValue},
success:function(data)
{
//Json for value textbox
$("#txtbookname").text(data[0].pcm_bname);
}
});
});
});
Just save the rendered view in a variable and do a json response:
public function index(Request $request) {
$results = DB::table('books')
->where('bookid', $request->id)
->get();
if ($results) {
$view = view('status.status', [
'proc' => $results
])->render();
return response()->json(['view'=> $view, 'proc' => '$results']);
}
}
I have 2 queries like this:
$expiresAt = Carbon::now()->addMinutes(10);
$thumbnails = Cache::remember('entities-thumbnails-'.$entity_id, $expiresAt, function () use ($entity_id) {
return Images::where(['entity_id' => $entity_id, 'image_style' => 'thumbnail'])
->select('path AS thumbnail', 'width As thumbnailWidth', 'height As thumbnailHeight');
});
$largeImages = Cache::remember('entities-largeImages-'.$entity_id, $expiresAt, function () use ($entity_id) {
return Images::where(['entity_id' => $entity_id, 'image_style' => 'large'])
->select('path AS src')
->union($thumbnails)
->get();
});
What I want to do is not execute them seperately but as one query. Overall there will be 4 queries therefore instead of doing 4 I want to do one, is that possible? How does union work exactly?
Storing whole plain results in PHP variable then let PHP do such filtering would be better in my point of view. As an example
$images = Image::where('entity_id', $entity_id)->get();
$thumbnails = Cache::remember('entities-thumbnails-'.$entity_id, $expiresAt, function () use ($images) {
return $images->filter(function ($image) {
return $image->image_style === 'thumbnail';
});
});
$largeImages = Cache::remember('entities-largeImages-'.$entity_id, $expiresAt, function () use ($images) {
return $images->filter(function ($image) {
return $image->image_style === 'large';
});
});
You might call UNION is literally the same as JOIN. But, it does combining results set into one in an operation rather than relate them horizontally. Still, both need to be separated by PHP, as each row recorded are united into one collection.
I'm assuming you still need to make aliases for specified columns. Fortunately, Laravel is able to do that out of the box.
$thumbnails = Cache::remember('entities-thumbnails-'.$entity_id, $expiresAt, function () use ($images) {
return $images->filter(function ($image) {
if ($image->image_style === 'thumbnail') {
$image->setAttribute('thumbnail', $image->path);
$image->setAttribute('thumbnailWidth', $image->width);
$image->setAttribute('thumbnailHeight', $image->height);
return true;
}
return false;
});
});
$largeImages = Cache::remember('entities-largeImages-'.$entity_id, $expiresAt, function () use ($images) {
return $images->filter(function ($image) {
if ($image->image_style === 'large') {
$image->setAttribute('src', $image->path);
return true;
}
return false;
});
});