Simple view not rendering:
public function getFranchise() {
echo 'getFranchise';
$franchiseData['shopViews'] = array(1 => 'a');
$franchiseData['aaa'] = 'bbb';
return View::make('test.filteredData.franchise', $franchiseData);
}
view in test/filteredData/franchise.blade.php
franchise
{{--$shopsViews}} {{-- Fatal error: Method Illuminate\View\View::__toString() must not throw an exception in D:\projektai\dashboard\app\storage\views\d2973247ea68aed2fdfd33dc19cccada on line 5}}
{{ $aaa }}
#foreach ($shopsViews as $shop)
<strong>aaa</strong>
#endforeach
Only word getFranchise is displayed which means controller function is called. No any errors, no anything. WHat is that?
Even in constructor is added
ini_set("display_errors", true);
Edited
Found that this:
{{--$shopsViews}} {{-- Fatal error: Method Illuminate\View\View::__toString() must not throw an exception in D:\projektai\dashboard\app\storage\views\d2973247ea68aed2fdfd33dc19cccada on line 5}}
comment was causing stop of execution in the script. WHy is that? this is valid laravel comment. Also I noticed weird thigs when I comment
<?php //print_r ?>
then it shows something like web page not found, like interent connection has gone. I dont get at all whats happening with commenting.
Your blade view must contain #extends() and #section() to work in this case.
And comment should look like this {{-- $shopsViews --}}. That should fix your problem.
#extends('your_layout_folder.layout')
#section('content')
#foreach ($shopsViews as $shop)
<strong>aaa</strong>
#endforeach
#stop
Please follow the documentation! http://laravel.com/docs
Related
using Laravel 5.6.28 my route on web.php file looks like this:
Route::get('kitysoftware/besttrades', 'SectorsController#besttradesview');
My controller with bestrasdeview function:
class SectorsController extends Controller
{
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
foreach ($sectors as $sector) {
echo $sector->SectorName;
}
//passing variable sectors1 to my view (is = $sector)
return view('besttradesview', ['sectors1' => $sector]);
}}
My view Bestradesview.blade.php is:
<form method="GET">
<div class="selectsector">
<Select class="selectsector" name = "sectors">
<option value="{{ sectors1 }}"></option>
<!-- test#2: <option value="{{ $sectors1->sector[] }}"></option> -->
<!-- test#3: <option value="{{ $sectors1->sector }}"></option> -->
</form>
</select>
And i get this error: Use of undefined constant sectors1 - assumed
'sectors1' (this will throw an Error in a future version of PHP)
When testing #2 commented line instead i get another error: Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Cannot use [] for reading
When testing #3 commented line without [] i get another error: Use of undefined constant sectors1 - assumed 'sectors1' (this will throw an Error in a future version of PHP)
Probably it's pretty simple but it's driving me nuts because i can't see why the variable is not passing to the view.
I know it's reading my table because if i remove the last return view call line on my controller, it echoes out all the values from my SectorName column, but i want it on a select dropdown menu.
I have been reading the docs, forums and watching laracast videos without luck. Any insight or just pointing me out to where to learn the proper sintax solution will be appreciatted.
Thanks in advance
It looks like you're trying to populate the select options from the query. If that's the case, your loop is in the wrong place.
Take the loop out of the controller method, and pass the $sectors collection directly to the view.
public function besttradesview()
{
$sectors = DB::table('Sectors')->get();
return view('besttradesview', ['sectors1' => $sectors]);
}
Then loop in the view to output the options.
<select class="selectsector" name="sectors">
#foreach($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
</select>
I assumed you wanted some text in the option. If you use the sector name as the option text it will also be used as the option value by default. If you want to use something else for the value, it would be like this, for example:
<option value="{{ $sector->id }}">{{ $sector->SectorName }}</option>
Change,
return view('besttradesview', ['sectors1' => $sector]);
To,
return view('besttradesview', ['sectors1' => $sectors]);
Notice the s in $sectors ($sectors = DB::table('Sectors')->get();) and in the actual view, it should be $sectors1 not just sectors1.
You also need to loop through actual sector:
#foreach ($sectors1 as $sector)
<option>{{ $sector->SectorName }}</option>
#endforeach
Also, I would definitely suggest creating a model for your Sectors table rather than using the DB:: facade. You should make use of the M in MVC.
Consider the manual authentication. If the order ID has not been found in database, we redirect user to page with input fields and with error 'wrongOrderId':
public function login(Request $request) {
$inputted_orderId = $request->input('order_id');
$orderIdInDB = DB::table(self::SITE_IN_DEVELOPMENT_TABLE_NAME)
->where(self::ORDER_ID_FIELD_NAME, $inputted_orderId)->first();
if (is_null($orderIdInDB)) {
return Redirect::route('loginToSitesInDevelopmentZonePage')->withErrors('wrongOrderId');
}
}
In this example, we don't need to pass the error message: the message box is already exists in View; all we need is to display this message box when user has been redirected with error 'wrongOrderId':
#if (!empty($errors->first('wrongOrderId')))
<div class="signInForm-errorMessagebox" id="invalidID-errorMessage">
<!-- ... -->
</div>
#endif
All above code is working without laravel/php errors; I get into is_null($orderIdInDB) if-block when input wrong order number, however the error message box don't appears. I tried a lot of ways, and (!empty($errors->first('wrongOrderId'))) is just last one. What I doing wrong?
Did you try printing {{$errors->first()}}?
first(string), works as a key value pair, it invokes its first key VALUE
try this,
#if($errors->any())
<div class="signInForm-errorMessagebox" id="invalidID-errorMessage">
<!-- ... -->
</div>
#endif
If you want to get specific error from validator error, use the get() method.
$errors->get('wrongOrderId'); get all the wrongOrderId errors.
$errors->first('wrongOrderId'); get first error of wrongOrderId errors
I explored that $errors->first('wrongOrderId') has non-displayable but non-null value. So #if ( $errors->first('wrongOrderId') !== null ) will work.
Something more elegantly like #if ($errors->first('wrongOrderId'))? Unfortunately just one this check is not enough: even if we define
return Redirect::route('loginToSitesInDevelopmentZonePage')->withErrors(['wrongOrderId' => true]);
php will convert true to 1. So, #if ( $errors->first('wrongOrderId') == true ) will working, but #if ($errors->first('wrongOrderId')) will not.
However, we can to cast $errors->first('wrongOrderId') to bool, so the most elegant solution will be
#if ((bool) $errors->first('wrongOrderId'))
I have a simple controller function that fetch all records from db. but when i am trying to show all these records it show nothing. In fact it shows me hard coded foreach loop like this.
#foreach ($compactData as $value) {{ $value->Name }} #endforeach
this is my contoller function.
public function showallProducts()
{
$productstock = Product::all()->stocks;
$productoldprice = Product::all()->OldPrices;
$productcurrentprice = Product::all()->CurrentPrice;
$compactData=array('productstock', 'productoldprice', 'productcurrentprice');
return view('welcome', compact($compactData));
}
this is my view
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="title m-b-md">
All products
</div>
<table>
<tbody>
#foreach ($compactData as $value)
{{ $value->Name }}
#endforeach
</tbody>
</table>
</div>
</div>
</body>
why it is behaving like this. any solution?? I am using phpstorm version 17. Is their any setting issue to run project because what ever project I ran it gives me the only page which i ran with only html?
My route is.
Route::get('/', function () {
$action = 'showallProducts';
return App::make('ProductController')->$action();
});
Have you checked your $compactData variable? Please dd($compactData) to see what it contains.
Problem 1
You are accessing a relational property as a property of Eloquent collection, like this:
Product::all()->stocks
which is not correct. Because the Collection object doesn't have the property stocks but yes the Product object might have a stocks property. Please read the Laravel documentation about Collection.
Problem 2
$compactData = array('productstock', 'productoldprice', 'productcurrentprice');
This line creating an array of 4 string, plain string not variable. So, your $compactData is containing an array of 4 string. If you want to have a variable with associative array then you need to do the following:
$compactData = compact('productstock', 'productoldprice', 'productcurrentprice');
Problem 3
return view('welcome', compact($compactData));
Here you are trying to pass the $compactDate to the welcome view but unfortunately compact() function doesn't accept variable but the string name of that variable as I have written in Problem 2. So, it should be:
return view('welcome', compact('compactData'));
Problem 4
Finally, in the blade you are accessing each element of the $compactData data variable and print them as string which might be an object.
You most likely have a problem with your web server.
Try to use Laravel Valet as development environnement.
Edit : I found this : Valet for Windows
I think you didn't mention the blade in the name of the view file by which it is saved. So change the name of the file by which it is save to something like:
filename.blade.php
and try again.
Explanation:
#foreach ($compactData as $value) this is the syntax of blade template engine, and to parse and excute it, you have to mention the blade extension in the name.
Hi I am trying to get the content within a div element that also happens to be within a form into my controller. I dont want to use ajax. How may I get that done ?
<div id="editorcontents" name="editorcontents">
</div>
Then in controller
Use Input;
$content = Input::get('editorcontents');
In your controller, do something like this. Look up the correct way in the docs (https://laravel.com/docs/5.4/eloquent#retrieving-models). For example, if you want ALL input, you would do Input::all();, instead of Input::where('editorcontents')->get();
public function index() {
$content = Input::where('editorcontents')->get();
return view('your_view.blade.file', compact('content'));
}
Then in your view your would now have $content, that you passed from your controller.
start of by looking at it, add this at top of your view: {{ dd($content) }}. This will die dump $content.
Go ahead and remove that line and do something like (docs here https://laravel.com/docs/5.4/blade#loops):
<div id="editorcontents" name="editorcontents">
#forelse ($content as $value)
<li>{{ $value->body }}</li>
#empty
<p>No content</p>
#endforelse
</div>
I'm doing a blog to learn how to use laravel. The blog is mostly finished but I still found some mistakes in my code. Most of them are solved but there is still one error I can't fix.
It's about the 'my thread' of the user. If the user did a new account, he also get a 'my thread' page. If he's klicking on the page, he will get a view with all his threads. My problem now is, that if he don't have any threads, he will get a:
trying to get property of non-object exception. Of course I don't want him to see this.
So this is the function in my controller to his 'my thread' page:
public function startpage($id)
{
// $userthreads = Thread::query()->where('user_id', $id)->get();
// return $userthreads;
try {
return view('test.startpage', [
'userthreads' => Thread::query()->where('user_id', $id)->get(),
'user' => User::query()->findOrFail($id)
]);
} catch (ModelNotFoundException $e) {
return view('test.notfound');
}
}
with the first two ( uncommented ) lines, I checked if there was something in the array. I just got
'[]'
as an output. That means the array is empty. I returned the $userthreads variable to the view and on my view I did this to avoid the problem:
#if(empty($userthreads))
Add Thread
#else
#foreach($userthreads as $uthread)
<ul>
{{$uthread->thread}}
</ul>
#endforeach
#endif
This haven't worked for me and I can't see why. Maybe someone of you can help me there.
Thanks for any help!
Current code of Alexey Mezenin answers:
public function startpage($id)
{
try {
$arr = [];
$arr['userthreads'] = Thread::where('user_id', $id)->get();
$arr['user'] = User::findOrFail($id);
return view('test.startpage', $arr);
} catch (ModelNotFoundException $e) {
return view('test.notfound');
}
}
HTML:
#if(count($userthreads) > 0)
Thread hinzufügen
#else
#foreach($userthreads as $uthread)
<ul>
{{$uthread->thread}}
</ul>
#endforeach
#endif
Error :
Trying to get property of non-object (View: /var/www/laravel/logs/resources/views/test/startpage.blade.php)
Ok, we discussed it in chat and found a problem:
#if(Auth::user()->id == $userthreads->first()->user_id)
Error is in your startpage.blade.php here
#if( Auth::user()->id == $userthreads->first()->user_id)
And change it like below
#if( isset($userthreads) && count($userthreads) > 0 && Auth::user()->id == $userthreads->first()->user_id)
that's it
#if(isset($userthreads) && count($userthreads) > 0)
#foreach($userthreads as $uthread)
<ul>
{{$uthread->thread}}
</ul>
#endforeach
#else
Thread hinzufügen
#endif
#ItzMe488
I think replacing the following code :
#if( Auth::user()->id == $userthreads->first()->user_id)
with
#if( Auth::user()->id == $user->first()->user_id)
should work without any errors and bugs.
Explanation:
In our chat #AlexeyMezenin found out that the error is because $userthreads is empty and $userthreads->first() errors out because of that. I think since you are passing $user also into the template, using that we can authenticate for the current user and this should prevent user B from creating threads in User A's page.