I'm very new here please.
I'm trying to get some information and pass it to the laravel blade to be displayed on my website. What is for sure is that the call was successful but when I try passing it to the view I get an error that says "undefined variable" in the view page and sometimes the area where information was supposed to be displayed just shows an empty space.
Pass your variables in the view method instead. The with function is for passing individual data, so it doesn't accept an array. Example:
return view('profile.newaddress', [
'address'=>$address
]);
If you want to use with, try it like this instead (assuming $address in a string):
return view('profile.newaddress')->with('address', $address);
Related
I have a string inside a view that I need to pass to a second view.
Inside view1 I have
route('admin.olt.add', $id, $resp)
where $resp is the parameter I need to pass to the second view. My route file calls the controller that returns the second view
Route::get('/olt/{id}/add', 'OLTController#config_parameters_onu')->name('olt.add');
Is there any way where I can pass this parameter without adding it to the url?
I don't know if I fully understand your issue but as I got it, you can add this variable to a session and then fetch it whenever you want in any view for instance
to add the variable to the session
session(['resp' => $resp]);
to fetch it back
session('resp')
and here is the full docs for better guidance
Hope that is what you looking for
you should use an array of more than one parameter
route('admin.olt.add', ['id'=>$id, 'resp'=>$resp]);
I am currently working with Laravel 5.2, trying to display images on click
which I have currently stored in the Storage folder. I am trying to display these images in my blade view but every time it loads the page, it gets to an undefined variable exception.
Controller:
public function createemoji($action,$statusId)
{
$path = storage_path('app/public/images/'.$action.'.gif');
/*$request=new storage();
$request->comment=$path;
$request->user_id=Auth::user()->id;
$request->post_id=$statusId;
$request->save();*/
return redirect()->returnemoji()->with('file'->$path);
}
public function returnemoji($file)
{
return Image::get('$file')->response();
}
In my default view I tried using count() but everytime it loads the page, it gives me Undefined variable. How should I display it?
Try to change this:
->with('file'->$path);
To this:
->with('file', $path);
https://laravel.com/docs/5.3/views#passing-data-to-views
With function takes two arguments key and value
You can use this
return redirect()->returnemoji()->with('file',$path);
You can try this out:
Instead of:
return redirect()->returnemoji()->with('file'->$path);
Try this:
return $this->returnemoji($path);
Hope this helps you.
There are a few problems.
Single quotes do not process variables, so instead of this
return Image::get('$file')->response();
You could do this
return Image::get("$file")->response();
or
return Image::get("{$file}")->response();
but none of this is necssary since you are just using the variable by itself without any additional formatting, so remove the quotes altogether
return Image::get($file)->response();
The object operator -> is used in object scope to access methods and properties of an object. Your function returnemoji() is not a method of RedirectResponse class which is what the redirect() helper method returns.
The with() method is not appropriate here, you just need to pass a parameter to a function like this
return redirect()->returnemoji($path);
Optionally, I recommend following the PSR2 code style standard which includes camel cased variable names so createemoji() should be createEmoji(). Also I think you can usually omit response() when returning most data types in Laravel as it will handle that automatically for you.
I think you have to try the following:
Instead of:
return redirect()->returnemoji()->with('file'->$path);
Try this:
return redirect()->returnemoji($path);
And yes, remove the quotes from this:
return Image::get('$file')->response();
I am trying to pass data from my controller to my view, but am getting an undefined variable error. usersID is a column in my MySQL table.
Here is the code in my controller
$arrayWithCount = DB :: table("users_has_activities")
-> where("usersID", "=", 19)
-> pluck("usersID");
$countNumber = sizeof($arrayWithCount);
return view('pages.progress', ['countNumber' => $countNumber]);
I have also tried the following return statement without any success
return view::make('pages.progress') -> with('countNumber', $countNumber);
I have also tried reversing the puck and where clauses without any success, I didn't have high hopes that reversing them would fix the problem but thought I would try it any way. Below is the relevant code in the blade file.
<?php echo $countNumber; ?>
This is the error I am currently getting
Undefined variable: countNumber
You code looks fine, if dd() doesn't stop execution of the controller, then another controller is executing. So double check your routes and controllers.
First sizeof should be sizeOf, and is simply an alias for count(). Most people would prefer count over sizeOf as sizeOf (in many languages) would indicate something related to size on disk.
Anywho, being that pluck returns a collection, you have access to count() directly from the collection.
You can probably simply do something like:
$countNumber = $arrayWithCount->count();
Sidenote: Unless there is a particular reason why you are using <?php ?>, in blade, it would be preferred to use {{ and }}.
I had all the controller code in a method I wasn't calling, so the variable was never passed to the blade file. The method was set up to be called on a button press, after fixing that everything works. Thanks Alexey for the help.
I do some calculations in the controller, and pass the data to my view like this.
return View::make('fend.clist', compact('detail_'));
From this view controller "fend.clist" I'd like to open another view (blade) along with the 'detail_' variable which basically has some data that I need to show there. I tried POSTing it, but I wasn't able to recieve the data completely.
How else can I pass data from one view to another view?
By using the sessions, first put:
Session::put(['name'=>'value']);
Then get:
Session::get('name');
You must nest sub-view to view and pass data to sub-view.
return View::make('fend.clist')->nest('fend.detail', compact('detail_'));
More info in docs
I'm having a strange problem that causes a variable to be passed to a sub-view even if it's not actually defined in the parent view.
This is the code I'm using to include the view using Blade syntax:
#include('segments/tasksList')
But if it execute the following code in segments/tasksList.blade.php
<++ dd($user) ++>
I get bool(true) as a result.
Don't mind <++ ++> I changed the Blade syntax because it was interfering with AngularJS.
Thanks to Denker I noticed that in my controller I had
return View::make('pages/company', ['user' => Auth::user()]);
that was causing the $user variable to be passed to all sub-views.