Laravel add more than 2 parameters in controller - laravel

In my controller I currently give 2 parameters to the view like this:
return view('home')->with(['listings'=>$listings, 'featured_listings'=>$featured_listings]);
But when I add a third parameter it gives me the following message:
Too few arguments to function App\Http\Controllers\HomeController::index(), 0 passed and exactly 1 expected
Is there any way to give more than 2 parameters to a view from a controller?

This very simple to pass data to view more than one. There are many ways to pass data to view. I suggest you
return view('home',compact('listings','featured_listings','your_data',...));

If you can put parameter like
return view('home', [
'listings' => $listings,
'featured_listings' => $featured_listings,
..............................
]);
then you can put more than 2.

There are few ways you can share data with a view. It is not limited to 1 or 2 parameters. You can share unlimited parameters.
Option 1
$categories = ProductCategory::all();
$brands = ProductBrand::all();
$product = Product::first();
return view('product.edit', compact(['categories', 'brands', 'product']));
Option 2
$categories = ProductCategory::all();
$brands = ProductBrand::all();
$product = Product::first();
return view('product.edit', ['categories' => $categories, 'brands' => $brands, 'product' => $product]);
Option 3
$categories = ProductCategory::all();
$brands = ProductBrand::all();
$product = Product::first();
return view('product.edit')->with('categories', $categories)->with('brands', $brands)->with('product', $product);

Related

How to change collection column value in laravel

I have a laravel collection that's like this:
$loan = Loans::where([
['OL_TEMP_APP_NO', $appNo]
])
->get();
The $loan collection returns principal, terms, code. The code corresponds to a string. Example 1 = new, 2 = processing, 3 = approved.
How do I parse the values under code before sending them to the view?
You can use CASE WHEN to convert the integer to string code:
$loan = Loans::where([
['OL_TEMP_APP_NO', $appNo]
])
->select('principal', 'terms', DB::raw("
(CASE code
WHEN 2 THEN 'processing'
WHEN 3 THEN 'approved'
ELSE 'new' END) AS code
"))
->get();
if you already know the value of code, or code will always have some fixed values, then you can use like following:
public function getAll(){
// ...
$loans = Loans::where([['OL_TEMP_APP_NO', $appNo]])->get();
$data = [];
foreach($loans as $loan){
$code = Code::getValue($loan->code);
$data[] = [
'principal' => $loan->principal,
'terms' => $loan->terms,
'code' => ($loan->code == 1) ? 'new' : ( ($loan->code == 2) ? 'processing' : 'approved')
]
}
return $data;
}

another "can't save multiple checkbox" and "must be of the type array, string given"

I have seen many same question about this problem but I can't figure out how to fix mine
so I'm trying to save multiple checkbox and I think this one will work but now I get "must be of the type array, string given".
By the way this is my controller
public function store(Request $request)
{
// $multi = Multi::create($request->only(['data'])->implode(', '));
// $multi = Multi::select('data')->implode();
$multis = implode(',', $request->get('data'));
$multis = Multi::create(['data' => $request->get('data')]);
return redirect()->route('multi.create')->with('success', 'berhasil.');
and this is my create.blade.php
#foreach($multis as $multi)
{{$multi['data']}}<br>
#endforeach
<br>----------------<br>
{{Form::open(['action'=>'MultiController#store'])}}
{{Form::checkbox('data[]','A')}}A<br>
{{Form::checkbox('data[]','B')}}B<br>
{{Form::checkbox('data[]','C')}}C<br>
{{Form::submit('TAMBAH')}}
{{Form::close()}}
Try this
$multis = implode(',', $request->data));
$multis = Multi::create(['data' => $multis]);
You have to add fields name as a key of array and define all the data into different variables like this this will work.
$multis1 = implode(',', $request->d);
$multis2 = implode(',', $request->i);
$multis3 = implode(',', $request->s);
$multis4 = implode(',', $request->c);
$multis= Test::create(array('d' => $multis1,'i' => $multis2,'s' => $multis3,'c' => $multis4));

Laravel 5.5 - Show specific category from API response

I am returning an API response inside a Categories controller in Laravel 5.5 like this...
public function get(Request $request) {
$categories = Category::all();
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}
Now I am trying to also have the option to return a specific category, how can I do this as I am already using the get request in this controller?
Do I need to create a new route or can I modify this one to return a specific category only if an ID is supplied, if not then it returns all?
Better case is to create a new route, but you can also change the current one to retrieve all models if the parameter is not supplied. You first gotta choose which approach you will be using. For splitting it into multiple calls you can see Resource controllers and for using one method you can follow Optional Route Parameters
It will be much cleaner if you will create another route. For example
/categories --> That you have
/categories/{id} -> this you need to create
And then add method at same controller
public function show($id) {
$categories = Category::find($id);
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}
But if you still want to do it at one route you can try something like this:
/categories -> will list all categories
/categories?id=2 -> will give you category of ID 2
Try this:
public function get(Request $request) {
$id = $request->get('id');
$categories = $id ? Category::find($id) : Category::all();
return Response::json(array(
'error' => false,
'categories_data' => $categories,
));
}

Yii2 ActiveRecord

I'm new to Yii and struggling to get my head around a few things.
I have model with this function:
public static function findNonGeo()
{
$query = new CallerIdentityQuery(get_called_class());
$query->select([
'cidref', 'caller_id', 'expiry', 'conf_call',
'type', 'redirect', 'destination', 'status', 'start_date',
'statusDesc' => new \yii\db\Expression("CASE status
WHEN 0 THEN 'Deactivated'
WHEN 1 THEN 'Active'
WHEN 2 THEN 'Inactive'
WHEN 3 THEN 'Unregistered'
ELSE 'Permanently Deleted' END")])
->where(['status' => '1'])
->andWhere(['type' => 'N'])
->andWhere(['custref' => Yii::$app->user->identity->typedIdentity->getId()]);
return $query;
}
And in my controller I call this method like so:
$model = CallerIdentity::findNonGeo()->where(['cidref' => $id])->one();
but when the data is returned its like its just ignoring the
->andWhere(['type' => 'N'])
because I can view records that aren't of type 'N'.
so I'm having to do in my controller, maybe I'm doing something wrong or just not understanding it but I don't get it:
$model = CallerIdentity::findNonGeo()->where(['cidref' => $id])->andWhere(['type'=>'N'])->one();
another thing when using findOne($id):
$model = CallerIdentity::findOne($id);
null is returned.
Would appreciate any form of explanation/info.
When using where you're setting the where part of the query, not adding to it. So in the findNonGeo function you're building the required where parts. But with CallerIdentity::findNonGeo()->where(['cidref' => $id]) you're removing the already added where parts.
Try like this:
CallerIdentity::findNonGeo()->andWhere(['cidref' => $id])->one();
by using andWhere you'll keep the other parts and just add another one to it.

Passing variable from DOMPDF controller to view

I want create a DOMPDF with laravel, and I must passing my variable to view. I've been try passing variable like below, but it still not working yet.
here my Laravel Controller
public function pdf(Request $request, $id){
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
$data = [$salesorder, $detailemployee, $detailservice];
$pdf = PDF::loadView('summary.invoice', $data);
return $pdf->download('invoice.pdf');
}
the error on my view is :
Undefined variable: salesorder
How to passing some variable from Laravel controller to DOMPDF ?
PS : dd($data) result is correctly
You have to pass the data as below
$data = [
'salesorder' => $salesorder,
'detailemployee' => $detailemployee,
'detailservice' => $detailservice
];
or try using compact
$data = compact('salesorder', 'detailemployee', 'detailservice');
You may try this following
public function pdf(Request $request, $id){
$salesorder = $this->show($id)->salesorder;
$detailservice = $this->show($id)->detailservice;
$detailemployee = $this->show($id)->detailemployee;
$pdf = PDF::loadView('summary.invoice', array('salesorder' => $salesorder,'detailemployee'=>$detailemployee,'detailservice'=>$detailservice));
return $pdf->download('invoice.pdf');
}
You can use library function to do that easily.
$pdf_text = "It will be the text you want to load";
PDF::loadHTML($pdf_text)->save('public/you-file-name.pdf');
You can change the orientation and paper size, and hide or show errors (by default, errors are shown when debug is on)
PDF::loadHTML($pdf_text)->setPaper('a4', 'landscape')->setWarnings(false)->save('public/you-file-name.pdf')
You may try the following.
$html = view('summary.invoice', ['salesorder' => $salesorder, 'detailemployee' => $detailemployee, 'detailservice' => $detailservice])->render();
$pdf = App::make('dompdf.wrapper');
$invPDF = $pdf->loadHTML($html);
return $pdf->download('invoice.pdf');
I know this is old but this may help others. you need to use array key in your view.
Controller:
$data = [
'foo' => $bar,
];
$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('doc.pdf');
View:
<p>{{$foo}}</p>

Resources