API request with multiple body inputs - laravel

I am new to laravel this is how I make an API with one single body item
$shops_categories=ShopsCategories::all()->where('title',$request->title);
how to request more than an item in the body for example title , email , password ?

Eloquent allows you to chain as many where as required. So you are able to do
$shops_categories = ShopsCategories::where('title',$request->title)->where('email',$request->email)->where('password',$request->email)->get();

You can do it like so:
$cats = ShopsCategories::query()->where(['title' => $title, 'email'=>$email, 'name'=>$name]);

i guess you are performing query operation in controller itself which is not recommended,please create an instance for the model and make a function call to model and in the model you can return like
return $this::where('id','=',$id)->get()
and also please try to avoid using text/string in where condition instead use id's or unique values

You can also convert request to array using all()
//$request->all() is same as ['title'=> 'my title', ...]
$shops_categories=ShopsCategories::where($request->all())->get();

Related

How to include an association with a user

I need to return a json object to my view. I see my subscriptions in tinker:
\App\User::find(1)->with('subscriptions')->where('id', 1)->get();
In my view, I get my user data but the subscriptions array is emply (length: 0)
Issue one, why I could not just \App\User::find(1)->with('subscriptions')->get() This returns all users in my database.
How can I return, as json, the current user with subscriptions? The long way is to make two api calls.
Im using Cashier for the subscriptions
Try to check the query log by running
\DB::enableQueryLog();
$data = \App\User::with('subscriptions')->find(1);
$query = \DB::getQueryLog();
dd($query);
It will show you what queries running behind the seens.
then for the json format try to decode into json and then pass to view.
$json_format = json_encode($data);
return view('user.index')->withJsonFormat($json_format);
Don't know why you want to pass the data with json format. but if you want to pass this into javascript, jquery or angular please create saperate API and call with AJAX.
The correct query is:
App\User::with('subscriptions')->find(1)
It will return User object with this user's subscriptions.

Merge URL parameters and generate a encrypted URL - Laravel 5.3

I have a URL like http://localhost:8000/assessment/3/199
Where 3 represents assignment id and 199 represents assessor id, in short they both represents two models.
I'm sending such URL into email. I want to first encrypt the URL and then send it to the email.
I want URL like http://localhost:8000/assessment/{some-long-random-string}
So, I want to merge both the parameters, make an encrypted string, send into email, upon accessing the URL decrypt it and get both actual parameters.
I would like a solution which uses Laravel to implement that.
May be using these:
https://laravel.com/docs/5.3/encryption
https://laravel.com/docs/5.3/hashing
The way I would tackle this, is by manually implementing it in a controller.
Generate the URL like this:
$url = URL::action('AssessmentController#decrypt', json_encode([
'assessment' => $assessment_id,
'assessor' => $assessor_id
]);
//now, use $url in your email
Create a route like:
Route::get('/assessment/{ciphertext}', 'AssessmentController#decrypt');
In the controller:
public function decrypt($ciphertext){
$params = json_decode(decrypt($ciphertext));
return $this->getAssessment($params['assessment'], $params['assessor']);
}
Of course, you will need more integrity-checks and some error handling, but you probably get the idea.

show name instead of id in a url using laravel routing

I have defined a route in laravel 4 that looks like so :
Route::get('/books/{id}', 'HomeController#showBook');
in the url It shows /books/1 for example , now i'm asking is there a way to show the name of the book instead but to keep also the id as a parameter in the route for SEO purposes
thanks in advance
You could also do something like this:
Route::get('books/{name}', function($name){
$url = explode("-", $name);
$id = $url[0];
return "Book #$id";
});
So you can get book by id if you pass an url like: http://website.url/books/1-book-name
if your using laravel 8, this may be helpfull.
In your Controller add this
public function show(Blog $blog)
{
return view('dashboard.Blog.show',compact('blog'));
}
In your web.php add this
Route::get('blog/{blog}', [\App\Http\Controllers\BlogController::class,'show'])->name('show');
Then add this to your model (am using Blog as my Model)
public function getRouteKeyName()
{
return 'title'; // db column name you would like to appear in the url.
}
Note: Please let your column name be unique(good practice).
Result: http://127.0.0.1:8000/blog/HelloWorld .....url for a single blog
So no more http://127.0.0.1:8000/blog/1
You are welcome.
You can add as many parameters to the url as you like, like this:
Route::get('/books/{id}/{name}', 'HomeController#showBook');
Now when you want to create an url to this page you can do the following:
URL::action('HomeController#showBook', ['id' => 1, 'name' => 'My awesome book']);
Update:
If you are certain that there will never be two books with the same title, you can just use the name of the book in the url. You just need to do this:
Route::get('/books/{name}', 'HomeControllers#showBook');
In your showBook function you need to get the book from the database using the name instead of the id. I do strongly encourage to use both the id and the name though because otherwise you can get in trouble because I don't think the book name will always be unique.
You can also use model binding check more on laravel docs
For example
Route::get('book/{book:name}',[BookController::class,'getBook'])->name('book');
The name attribute in "book/{book:name}" should be unique.

How to automatically append query string to laravel pagination links?

I am working on search filter on checkbox click, with Laravel and Ajax call. So I get results when I click on a checkbox. my query is as follows:
$editors = User::with(['editor.credentials','editor.specialties','editor.ratings']);
$temp=$editors->whereHas('editor', function($q) use ($a_data){
$q->whereHas('specialties',function($sq) use($a_data){
$sq->whereIn('specialty',$a_data);
});
})->paginate(2);
This gives me all the data I need. however how should I get the links for pagination?
$temp->setBaseUrl('editors');
$links = $temp->links()->render();
I am currently doing this and with $links which I am sending over as response to ajax call, I set the pagination with $links data. Now, I need to append the query to next page like page=2?query="something". I don't know how should I go about appending the remaining query result links to next page links. i.e. I don;t know what should come in the query="something" part. Can someone guide me. thanks
{{ $users->appends($_GET)->links() }}
It will append all query string parameters into pagination link
As of Laravel 7, you can call the withQueryString() method on your Paginator instance.
Quote from the documentation:
If you wish to append all current query string values to the pagination links you may use the withQueryString method:
{{ $users->withQueryString()->links() }}
See "Appending To Pagination Links": https://laravel.com/docs/7.x/pagination#displaying-pagination-results
Check the answer from #Arda, as it's global solution. Below you can find how to do it manually.
Use appends on Paginator:
$querystringArray = Input::only(['search','filter','order']); // sensible examples
// or:
$querystringArray = ['queryVar' => 'something', 'anotherVar' => 'something_else'];
$temp->appends($querystringArray);
Append all input except the actual page, form token and what you don't want to pass:
$paginatedCollection->appends($request->except(['page','_token']));
For the latest version of Laravel at the moment (5.2), you can just use the Request facade to retrieve the query string and pass that to your paginator's appends() method
$input = Request::input();
$myModelsPaginator = App\Models\MyModel::paginate();
$myModelsPaginator->appends($input);
Add this anywhere in your app (e.g routes.php, filters.php or anything that's autoloaded), no need to edit any pagination codes that is written already. This works flawlessly using view composers, and you don't need to know any query string parameters:
////////PAGINATION QUERY STRING APPEND
View::composer(Paginator::getViewName(), function($view) {
$queryString = array_except(Input::query(), Paginator::getPageName());
$view->paginator->appends($queryString);
});
//////////////////
Inspired from previous answers I ended up using the service container for both frontend + api support.
In your AppServiceProvider#boot() method:
$this->app->resolving(LengthAwarePaginator::class, function ($paginator) {
return $paginator->appends(array_except(Input::query(), $paginator->getPageName()));
});
you can used request helper in view as same
{{ $users->appends(request()->query())->links() }}
in your view where you display pagination...
{{ $results->appends(Request::except('page'))->links() }}
appends keeps the query string value except "page". not sure if it will work with POST request
{{ $users->appends(Request::only(['filter','search']))->links()}}
Updating #rasmus answer for Laravel 8.
In your AppServiceProvider boot method, add the following lines and your existing query string will be be used for all pagination links
$this->app->resolving(Paginator::class, function ($paginator) {
return $paginator->appends(Arr::except(Request::query(), $paginator->getPageName()));
});
$this->app->resolving(LengthAwarePaginator::class, function ($paginator) {
return $paginator->appends(Arr::except(Request::query(), $paginator->getPageName()));
});
And for completeness, use these classes:
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Request;

Zend getParams not returing url params

I have a query:
https://myaddress.ee/admin/usersearchajax?country=EE&query=arno
$this->_request->getParam('query');
returns: NULL
var_dump($_REQUEST['query']);
returns: string(5) "arno"
How to fix this problem? I mean how to get GET values with zend framework?
Lets assume i can't change the query string.
zend version 1.11.11
public function usersearchajaxAction(){
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender( true );
$userService = new Application_Services_User();
$userList = $userService->searchByName($this->_request->getParam('query'));
$this->_helper->json($userService->getArrayForAutoComplete($this->_request->getParam('query'), $userList));
}
I found the problem, there was and override of $_GET variable in my project.
So it is fix now. thank you all for reading and thinking about this
instead of
$this->_request->getParam('query');
use this
$this->_getParam('query');
there is no need to use request action helper in Action to get this params
or try to use this way which uses request action helper
$request = $this->getRequest();
$query = $request->getParam('query');
hope this link will help you
Also you can view all params in a row $this->_request->getParams();
Then find needle param in a row and get using $this->_request->getParam('needle');

Resources