Missing required parameter error on route with multiple parameters - laravel-5

this error message keep showing
Missing required parameters for [Route: merchants.add.channel] [URI: merchants/{key}/denomination/{keydenom}/channel/addChannel].
here is my route
Route::get('merchants/{key}/denomination/{keydenom}/channel', 'MerchantsController#channel')->name('merchants.channel');
here is my function
public function channel($id)
{
if (!can('merchants_list_channel')) {
return redirect()->route('home');
}
$merchant = MerchantDenomination::where('id', $id)->pluck('name')->first();
$statusDenomList = [
'' => '-- All --',
'ACTIVE' => 'Active',
'DISABLE' => 'Disable',
'OFS' => 'OFS'
];
return view('merchants.channel', compact('statusDenomList', 'id', 'merchant'));
}

The route has 2 route parameters: {key} and {keydenom}.
Your controller function should expect both of them:
public function channel($key, $keydom) {
// Do your job
}
You also need to express the params everytime you call route():
route('merchants.channel', ['key' => "your-key", 'keydom' => "your-keydom"])

Related

Laravel how to skip single field validation in update time

I have a common blade form for add and update
<form action={{ $bookEnt->id == null ? route( "book-post" ) : route("book-update",['bookId'=>$bookEnt->id]) }} method = "POST" enctype="multipart/form-data">
#csrf
#method( $bookEnt->id == null ? 'post':'put')
//blade fields
</form>
I have created a request for validation the form request
public function rules()
{
return [
'title' => 'required|string',
'file_pdf' => 'required|mimes:pdf|max:30720',
];
}
Below by add controller
public function add(\App\Http\Requests\BookFormRequest $request)
{
// todo : msg
if( $request->isMethod('put') || $request->isMethod('post') )
{
$book = $bookId ? $this->getBookById($bookId):new Book;
-----
}
}
When the method is put how I will skip validation for 'file_pdf' ? I don't want to create another request class.
use switch case and check request method type
public function rules()
{
switch ($this->method()) {
case 'POST':
return [
'title' => 'required|string',
'file_pdf' => 'required|mimes:pdf|max:30720',
];
case 'PUT':
return [
'title' => 'required|string'
];
default:
return [];
}
}

Json response returns nested object

I am new in vue laravel and trying to get data from API controller with json response. I feel its not in best practices that I get data in nested object. Whats the best way to implement this so that I can get this.user_data.designations = All_Designation(with res.data) and some other like this.user_data.employment = All_Employments, etc.
Also when I try to send data via props I can access it as
designations: this.data.map(d => ({label: d.designation_name, value: d.designation_name, id:d.id}))
I want other data also so I guess it should be like this.data.designations, this.data.employment. This made me confused. how can I manage everything without changing things in trait?
This is my controller method:
public function index()
{
$designations = Designation::all();
if (!$designations->isEmpty()) {
$this->responseData['response'] = 1;
$this->responseData['message'] = "Designations has been Recieved.";
$this->responseData['data'] = $designations;
$this->status = 200;
}
return $this->apiResponse();
}
Api trait:
protected $responseData = [
'response' => 0,
'message' => "Not Found",
'data' => null
];
/**
* #var int
*/
protected $status = 404;
/**
* #return mixed
*/
public function apiResponse()
{
return response()->json($this->responseData, $this->status);
}
Axios Call:
this.$store.dispatch('employee/getDesignations' )
.then(res => { this.user_data.designations = res.data.data })
.catch(err => {
console.error(err)
})
Just return response like this
return response()->json(['response' => ['status' => true, 'data' => $apiResponse]], JsonResponse::HTTP_OK);
HTTP_OK =200 and its a enum type of use Illuminate\Http\JsonResponse;
status =true mean you returning the data .
for example if your data is empty then return something like this
return response()->json(['response' => ['status' => false, 'message' => 'Unable to find data ']], JsonResponse::HTTP_BAD_REQUEST);
just return this .
return response()->json($data, JsonResponse::HTTP_OK);
hope this will hel you

Passing return data to another function in same controller laravel

Try to connect to external API.
In first function, I already received token with authentication.
To send POST request, I need to put xtoken that I received from first function as second function.
I don't know how to send value to second function (registerUser)
Route::get('/connect', 'Guzzlecontroller#registerUser')->name('registeruser');
this is my route file
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
use App\Http\Controllers\Auth\RegisterController;
class GuzzleController extends Controller
{
public function Gettoken()
{
$client = new \GuzzleHttp\Client();
$request = $client->get(
'http://api01.oriental-game.com:8085/token',
[
'headers' => [
'X-Operator' => 'mog189b',
'X-key' => 'sQxAVNaEMe0TCHhU',
]
]
);
$response = $request->getBody();
$tokenReturn = json_decode($response, true);
$xtoken = array("x-token:" . $tokenReturn['data']['token'],);
$this->registerUser($xtoken);
}
public function registerUser($xtoken)
{
$client = new \GuzzleHttp\Client();
$url = "http://api01.oriental-game.com:8085/register";
$request = $client->post($url, [
'headers' => $xtoken,
'body' => [
'username' => 'test1',
'country' => 'Korea',
'fullname' => 'test user1',
'language' => 'kr',
'email' => 'testuser1#test.com',
]
]);
$response = $request->send();
dd($response);
}
}
Too few arguments to function App\Http\Controllers\GuzzleController::registerUser(), 0 passed and exactly 1 expected
this is error I am getting.
please help me to how to send $xtoken value to registerUser function
The problem is Laravel is calling registerUser directly instead of going through getToken. So the token is never retrieved and passed to the register action.
Instead of calling registerUser() from Gettoken(). Have Gettoken() return the token and call it from registerUser()
public function Gettoken()
{
...
return $xtoken;
}
public function registerUser()
{
$xtoken = $this->Gettoken();
...
}

laravel on saving model return json from validation

Hi I'm having a problem outputting my json information on saving method in the model. I get the following error -
UnexpectedValueException in Response.php line 397:
The Response content must be a string or object implementing __toString(), "boolean" given.
I do validation on the model while saving and in the validate method of the model I need to out put the json but I'm getting boolean instead of json object
Javascript:
submit: function(e) {
e.preventDefault();
var contact = this.model.save({
firstname: this.firstname.val(),
lastname: this.lastname.val(),
company: this.company.val(),
email_address: this.email_address.val(),
description: this.description.val(),
}, {success:function(response){ console.log(response)}, wait: true});
Contact Model:
class Contact extends Model
{
protected $table = "contacts";
protected $fillable = ['firstname', 'lastname', 'company', 'email_address', 'description'];
public static function boot() {
parent::boot();
static::creating(function($model) {
return $model->validate('POST');
});
static::updating(function($model) {
return $model->validate('PUT');
});
static::saving(function($model) {
return $model->validate('PUT');
});
}
public function rules($method)
{
switch($method)
{
case 'GET':
case 'DELETE':
{
return [];
}
case 'POST':
{
return [
'firstname' => 'required',
'lastname' => 'required',
'email_address' => 'required|email|unique:contacts,email_address',
'description' => 'requried'
];
}
case 'PUT':
case 'PATCH':
{
return [
'firstname' => 'required',
'lastname' => 'required',
'email_address' => 'required|email|unique:contacts,email_address,'.$this->id,
'description' => 'required',
];
}
default: break;
}
return [];
}
public function messages() {
return [
'firstname.required' => 'Please enter your first name.',
'lastname.required' => 'Please enter your first name.',
'email_address.required' => 'Please enter a email address.',
'email_address.email' => 'Please enter a valid email address',
'email_address.unique' => 'The email is not unique.',
'description' => 'Please enter a description.'
];
}
public function validate($method)
{
$data = $this->attributes;
// if( $data['slug'] === '') {
// // if the slug is blank, create one from title data
// $data['slug'] = str_slug( $data['title'], '-' );
// }
// make a new validator object
$v = Validator::make($data, $this->rules($method), $this->messages());
// check for failure
if ($v->fails())
{
// set errors and return false
// json here not return response it's always boolean true or false
return new JsonResponse(array('error' => true, 'errors' => $v->messages()));
}
// validation pass
return true; //new JsonResponse(array('errors'=>false));
}
public function errors() {
return $this->errors;
}
public function user() {
return $this->hasOne('App\User', 'email', 'email_address');
}
}
Saving the model:
public function update(Request $request, $id) {
$contact = Contact::find($id)->with('user')->first();
$contact->firstname = $request->get('firstname');
$contact->lastname = $request->get('lastname');
$contact->email_address = $request->get('email_address');
$contact->company = $request->get('company');
$contact->description = $request->get('description');
return $contact->save(); //return formatted json
}
According to your implementation of validation, you should change the following part (in Contact):
// check for failure
if ($v->fails())
{
// set errors and return false
// json here not return response it's always boolean true or false
return new JsonResponse(array('error' => true, 'errors' => $v->messages()));
}
To something like this:
if ($v->fails()) {
$this->errors = $v->errors();
return false;
}
Then, from the Controller, try something like this:
// If validation failed
if(!$contact->save()) {
return response()->json([
'error' => true,
'errors' => $contact->errors()
]);
}
// Contact created if reached here...
return response()->json(['error' => false, 'contact' => $contact]);
Also, check the Ajax-Request-Validation and Form-Request-Validation (Easier and Managable).
Note: Don't try to return any kind of HTTP Response from model. Returning the HTTP response is part of your application logic and model should not care about these.
As save() does return boolean so You've to check if it's ok.
1) Change Your Contact model to put errors to model's errors param:
/* if($v->fails()) remove/comment this line
...
} */
$this->errors = $v->errors();
return !$v->fails();
2) In Your controller put this code:
public function update(Request $request, $id) {
$contact = Contact::find($id)->with('user')->first();
if(!$contact) {
return response('Contact not found', 404);
}
$contact->firstname = $request->get('firstname');
$contact->lastname = $request->get('lastname');
$contact->email_address = $request->get('email_address');
$contact->company = $request->get('company');
$contact->description = $request->get('description');
return $contact->save()?
$contact->toJson() : // returns 200 OK status with contact (json)
response($contact->errors, 400); // returns proper 400 Bad Request header with errors (json) in it
}
p.s. it's nice to answer to requester with http status, industry has made all to make life of developer easy, so if it's not 2xx, 3xx status so => response for Your request from client-side app will be treated as error (handler success: function(response) will not catch error here)

What does The Response content must be a string or object implementing __toString(), "object" given. mean and why do I only get it on the remote?

I'm running an app on PagodaBox. Locally it works fine, but when I push it to pagoda, I get the following error:
The Response content must be a string or object implementing __toString(), "object" given.
Here is the route:
Route::get('tweets/{q}', function($q)
{
return Twitter::getSearch(array('q' => $q, 'count' => 5, 'lang' => 'sv', 'result_type' => 'recent'));
});
Other routes work fine. What does this error mean and what could cause this error?
Do this to see what is returned.
dd(Twitter::getSearch(array('q' => $q, 'count' => 5, 'lang' => 'sv', 'result_type' => 'recent')));
You have to return View class which has __toString() method or if it's ajax request then something like Response::json($data);
create the following function
function utf8_encode_deep(&$input) {
if (is_string($input)) {
$input = utf8_encode($input);
} else if (is_array($input)) {
foreach ($input as &$value) {
self::utf8_encode_deep($value);
}
unset($value);
} else if (is_object($input)) {
$vars = array_keys(get_object_vars($input));
foreach ($vars as $var) {
utf8_encode_deep($input->$var);
}
}
try to do the following
$response = Twitter::getSearch(array('q' => $q, 'count' => 5, 'lang' => 'sv', 'result_type' => 'recent'));
utf8_encode_deep($response);
return response;

Resources