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.
Related
I know that for some it might be stupid or funny question (but I am newbie) but I need to find know how to properly use DD() method in laravel projects.
For example - I have got tasks to debug some code and functionality in my project (PHP laravel). And it always takes me for ever to find the exact file or folder or code where the problem is.
My mentor says to use DD() method to find things faster (but for learning purposes he didn't explain me a lot about how to actually use it and said to find out my self), but said that I should start with Route (we use backpack as well for our project). So after finding Route (custom.php file) which controller connects to my required route what should I do next? How do I implement dd() method (or as my mentor says dd('call here') method) to fast find what I should be looking for to solve my problem and complete my task? Where should I write this dd() and how should I write it?
Thank you for the answer in advance!
for example I have a:
public function create(): View
{
return view('xxxxxx. \[
//
//
\]);
}
and if I put dd() anywhere in the code, I get error message in my URL :(
first of all ,in Laravel we use dd() before return in order to read any variable.
in controller we often use two kinds of variables : collection(which we get its members via foreach) or singular variable (we get it via its name)for example:$var = 1; dd($var).
notice:
if you are using ajax response you will not be able to see dd() results in page ,you can see the result via network tab in your browser (if u inspect your page).
dd stands for "Dump and Die."
Laravel's dd() function can be defined as a helper function, which is used to dump a variable's contents to the browser and prevent the further script execution.
Example:
dd($users,$variable1,$var2);
You can use dd() in blade
#foreach($users as $user)
#dd($user)
OR
{{dd($user)}}
#endforeach
#dd($var1)
You can read this article, the have more example and comparison
https://shouts.dev/articles/laravel-dd-vs-dump-vs-vardump-vs-printr-with-example
As Laravel is following model-view-controller or MVC design pattern. First go to the route and check which controller is called in the URL with the related URL.
Then go to the controller. **dd**() function is basically a dump and die. you also can do this by **print** or **echo** function too.
Lets assume that I have a controller name ProductController where I have method name index.From where I need to show a list of products in a table.
// in controller
public function index()
{
$products = Products::all();
// here you think ,I need to check whether I am getting the output or
not.
dd( $products );
//Or echo $products;
return view ('product.list',compact('products'));
}
let's suppose you are getting everything but in view when you loop through the products you declare the wrong variable name or mistakenly do some spelling mistakes. and want to see the result.
In view just do the dd() method by the following way:
{{ dd($products) }}
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'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);
Bit of an odd problem which is probably because I am finding my feet with Laravel.
I have an edit page which is accessed by:
Route::get('editRow/{id}', function($id){
$section = App\Section::where('id', $id)->with('panels')->first();
return view('front.editrow',['thesection'=>$section]);
});
This form then is completed and goes to a controller with a longish method to update the table and ends with:
return view('front.editrow',['message'=>'update successful','id'=>$request->id]);
The DB is updated OK. Initially I was getting an error so I copied the route and changed it from a get to a post:
Route::post('editRow/{id}', function($id){
$section = App\Section::where('id', $id)->with('panels')->first();
return view('front.editrow',['thesection'=>$section]);
});
Now although this is exactly the same parameters etc I always get;
Undefined variable: thesection
I am baffled and would greatly appreciate this mystery being solved!
Make sure that thesection variable is always passed to the view.
When you do:
return view('front.editrow',['message'=>'update successful', 'id'=>$request->id]);
this variable is not provided. You need to either provide it in the view or check for its existence in the blade template with:
#if(isset($thesection)
// whatever you want to do with the section
#endif
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.