Laravel 5 QueryException When I try to call Edit Function - laravel-routing

I am learning about Laravel 5 and studying from this source
https://laracasts.com/series/laravel-5-fundamentals/episodes/13
But when I try to make my own project which involves editing like the source taught me, I got "QueryException in Connection.php line 620:SQLSTATE[42S22]: Column not found: 1054 Unknown column 'mscustomer.id' in 'where clause' (SQL: select * from mscustomer where mscustomer.id = 1 limit 1)"
Here are the code snippets
Routes.php
Route::get('edit/{member}', 'MemberController#edit');
MemberController.php
public function edit($custid)
{$member=mscustomer::findOrFail($custid);return view('member.edit' ,compact('member'));}
And here is the edit.blade.php
<h1>Edit</h1>
<hr/>
{!!Form::open(['url'=>'member'])!!}
<div class="form-group">
{!!Form::label('custname','Name :')!!}
{!!Form::text('custname',null,['class' => 'form-control'])!!}
<br>
{!!Form::label('password','Password :')!!}
{!!Form::password('password',null,['class' => 'form-control'])!!}
<br>
{!!Form::label('email','E-mail :')!!}
{!!Form::text('email',null,['class' => 'form-control'])!!}
{!!Form::submit('Register')!!}
{!!Form::close()!!}
#if ($errors->any())
#foreach ($errors->all() as $error)
<br>{{ $error }}</br>
#endforeach
#endif

As the error states, the id column doesnt' exist in Database.
If you did setup your database migrations properly you wanna run "php artisan migrate". IF you didn't. You need to set them up befor you can start using your Eloquent models.

Related

Binding Expression in Aurelia if.bind

I would like to put in a simple expression to a template in Aurelia.
When working in a loop of <span repeat.for="link of links">, I want to show a '/' between all items, except after the last one.
I would expect I could use the following:
<span if.bind="${$index + 1} !== ${links.length}"> / </span>
But this gives me the following error:
Uncaught (in promise) Error: Parser Error: Missing expected : at column 10 in [${$index + 1} !== ${links.length}]
Is there a way I can do this?
Try if.bind="$index !== links.length - 1" instead of doing string interpolation. That should make it work.
or even shorter:
<span>${links.join(' / ')}</span>

Query returns Object(Builder), undefined property

I have following code
public function detailCustomer(Customer $customer)
{
$vehicles = DB::table('vehicles')
->selectRaw('*')
->where('cust_id', '=', $customer->id);
return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles));
}
Where table vehicles consists of:
spz
cust_id <FK> //this is foreign key to customer->id
type
brand
In the customers.detail view, I tried to use following code to show data, but I get this error:
Undefined property: Illuminate\Database\PostgresConnection::$spz
Code:
#if (count($vehicles) > 0)
<?php $i = 1; ?>
#foreach ($vehicles as $vehicle)
<?php $i++; ?>
<td>{{$vehicle->spz}}</td>
<td>{{$vehicle->type}}</td>
<td>{{$vehicle->brand}}</td>
#endforeach
#endif
I have read this topic but seems it's not my problem because I use foreach to iterate through the object but seems I do not get the object from database into my $vehicles variable, because in the error page, it shows also something like this:
'customer' => object(Customer), 'vehicles' => object(Builder)
What makes me think that customer gets its object correctly, but vehicles gets Builder?? Really no idea what is wrong there. Any ideas?
Just to describe what am I doing in the project that I work on, I have a customer detail page where I click a button to add a vehicle to his detail page (profile page) and I send customer id as parameter to the function where I add vehicle into database, which works (vehicle is added correctly with the customer id). Now that problem shows up when I want to show detail page with vehicle information like the code above shows.
Hope its clear enough. Thanks for suggestions.
Try to add ->get() or ->paginate($YOUR_LIMIT_ONE_PAGE) in your Controller
public function detailCustomer(Customer $customer)
{
$vehicles = DB::table('vehicles')
->selectRaw('*')
->where('cust_id', '=', $customer->id)->get();
// ->where('cust_id', '=', $customer->id)->paginate($YOUR_LIMIT_ONE_PAGE);
return view('customers.detail', array('customer' => $customer, 'vehicles' => $vehicles));
}
And try to replace your foreach to this forelse
#forelse ($vehicles as $index => $vehicle)
<tr>
<td>{{$index}}</td>
<td>{{($vehicle->spz !== null) ? $vehicle->spz : '-'}}</td>
<td>{{($vehicle->type !== null) ? $vehicle->type : '-'}}</td>
<td>{{($vehicle->brand !== null) ? $vehicle->brand : '-'}}</td>
</tr>
#empty
<td colspan='4'>Data not found</td>
#endforelse

Notice: Undefined variable: on line 24

I have the code:
if(isset($_POST['id'])){ $res = $_POST['id']; }
if(isset($_POST['ora2'])){ $oratwo = $_POST['ora2']; }
$operator = $_SESSION['login'];
$order = "
UPDATE `frontdes_dep`.`flux_receptie`
SET `status_preluare` = 'ALOCAT', `ora_preluare` =$oratwo, `operator_preluare` =$operator
WHERE `flux_receptie`.`id` =$res";
$result = mysqli_query($connection, $order);
When I press the submit button which take actions above, it returns me error:
( ! ) Notice: Undefined variable: oratwo in C:\wamp\www\interfata_client.php on line 24
The code is working because when I look into DB, the columns populate with values.
Line 24 is the line with $order = ...
The submit is in an echo:
echo "<div id='example3'>
<form style='padding:15px 0px 0px 0px' id='interfata' name ='interfata' method='POST' action='interfata_client.php' enctype='multipart/form-data'>
<input type='hidden' name='id' value='$res[0]'>
<input type='hidden' name='ora2' value='now()'>
<input type='submit' name='name' value='ALOCARE'>
</form>
<td><center>preluat de:<p><b>$res[8]</center></b></td>
</tr>
</div>
<br>
";
Does anynone know what am I doing wrong? I have WAMP installed.
Thanks! :)
There are 2 ways to get rid of this issue , The first one is to ignore the issue by turning of Notice exceptions .
error_reporting(E_ALL ^ E_NOTICE );
Or try the second solution below.
Notice : check whether the value is coming up from the form ('ora2')
before trying the second one.
Your code
if(isset($_POST['ora2'])){ $oratwo = $_POST['ora2']; }
Updated code
if(isset($_POST['ora2'])){
$oratwo = isset($_POST['ora2']) ? $_POST['ora2'] : '';
}

Laravel Display a record details while grouped by year or month

I just moved to the laravel framework and am starting to migrate some legacy sites and I have hit a problem with SQL or blade - dunno which.
I have to display a load of rows 'sports classes' which are grouped by year and then month. each needs to show attendance etc.
I am unsure which way to proceed.
I am able to display all rows and sort by date - easy squeezy
I am able to groupBy year AND month - fiddly but sorted it.
These are all displayed in an accordian.
Click the month - the individual rows drop down - you get the idea
I can get a number of rows per month/year
What I am unable to figure out is how to actually display the rows.
The groupBy is this:
$LinkClasses = DB::table('classes_lists')
->select('id, class, teacher, size')
->select(DB::raw('YEAR(date) AS year, MONTH(date) AS month, MONTHNAME(date) AS month_name, COUNT(*) post_count'))
->groupBy('year')
->groupBy('month')
->orderBy('year', 'desc')
->orderBy('month', 'desc')
->orderBy('id', 'desc')
If the code you provided is within your controller, then you can append ->get() after your last ->orderBy(). This will return a Collection. You can then do whatever you want with the Collection (http://laravel.com/api/master/Illuminate/Support/Collection.html), including conversion to an array using ->toArray(), but I think it would be best to utilize the Eloquent ORM if possible.
Anyway, once you have it in the format you want, just pass it to the view like so:
return view('your.view', compact('LinkClasses'));
Then, inside the your.view blade template, you can access this by using the following:
#foreach ($LinkClasses as $currentRow)
<tr>
<td>{{ $currentRow['id'] }}</td>
<td>{{ $currentRow['class'] }}</td>
<td> ... </td>
</tr>
#endforeach
Best guess I can offer without seeing the blade template to get a better idea of what you're doing. Hope that helps!
UPDATE BASED ON OP FEEDBACK:
Since you are only receiving a single record, it seems as though the issue lies in your query. I suggest you simplify your query to fetch all records and then do your sorting within an array. Something like this in your controller:
$allClasses = DB::table('classes_lists')->all();
foreach ($allClasses as $currentClass) {
$yearMonth = date('Y-m', $currentClass['date']);
$classesByYearMonth[$yearMonth][] = $currentClass;
}
ksort($classesByYearMonth);
/* now you have an array of all classes sorted by year-month like this:
// $classesByYearMonth[2014-01] = array(
// [0] => array(1, 'class name', 'teacher name', 23),
// [1] => array(2, 'another class', 'different teacher', 25),
// ...
// );
//
// $classesByYearMonth[2014-02] = ...
*/
return view('your.view', compact('classesByYearMonth'));
Then, inside your blade template:
#foreach ($classesByYearMonth as $yearMonth => $classListArray)
Found {{ sizeof($classListArray) }} classes for {{ $yearMonth }}
#foreach ($classListArray as $currentClass)
<div>
<div>ID: {{ $currentClass['id'] }}</div>
<div>Class: {{ $currentClass['class'] }}</div>
<div>Teacher: {{ $currentClass['teacher'] }}</div>
<div>Size: {{ $currentClass['size'] }}</div>
</div>
#endforeach
#endforeach
I will leave it to you to fix the formatting to make your accordion work. But hopefully that will get you on the right path.
DNoe - thank you so much.
Your reply put me on exactly the right track.
I had to mod some bits due to laravel ambiguities and add the strtotime but the logic was all there.
foreach ($allClasses as $currentClass) {
$ym = $currentClass['date'];
$yearMonth = date("Y-m",strtotime($ym));
$classesByYearMonth[$yearMonth][] = $currentClass;
}
krsort($classesByYearMonth);
return View::make('classes.index', compact('classesByYearMonth'));
The css is simple from here.
I owe you some beers. And thanks for helping me take my head from my butt!
Send me a pm and i would be very very happy to forward beer donation :o
Great work and thank you again. :)
Also, part of the problem was that the results were throwing an stdObject rather than an array.
Being able to compare your code with my own has enabled me to create a dbquery with multiple joins from which meaningfull data is selected and then converted to an array.
$classes = DB::table('table2')
->join('table1', 'table2.id', '=', 'table1.id2' )
->join('table3', 'table1.id3', '=', 'table3.id' )
->orderBy('classes_lists.date','DESC')
->get(array('table1.id', 'teacher', 'date', 'size', 'students', 'fname', 'classname', 'table1.notes'));
$cfr = count($classes);
foreach($classes as $object)
{
$arrays[] = (array) $object;
}
foreach ($arrays as $currentClass){
$ym = $currentClass['date'];
$yearMonth = date("Y-m",strtotime($ym));
$clazByYearMonth[$yearMonth][] = $currentClass;
}
krsort($clazByYearMonth);
This was the output into blade:
Not formatted :
#foreach ($clazByYearMonth as $yearMonth => $classListArray)
Found {{ sizeof($classListArray) }} classes for {{ $yearMonth }}
#foreach ($classListArray as $currentClass)
<div>
date: {{ $currentClass['date'] }} | class: {{ $currentClass['classname'] }} | Size: {{ $currentClass['size'] }} Teacher: {{ $currentClass['fname'] }} |
</div>
#endforeach
#endforeach

search form error

I'm webdesigner and I touch NOTHING in development, so, I really need your help (exam in school). So, indeed, I've made a search form in the same page. There a field to search by town (ville) and the results should be appareate in a div. Sometimes it works, but I really need to fix that.
The error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\foodsurfing\index.php on line 55
There is the code
<?php
require_once('Connections/foodsurfing.php');
if(isset($_GET['recherche']))
$requete="SELECT * FROM fiche_membres WHERE ville LIKE '%".$_GET['recherche']."%'
LIMIT 0 , 1 ";
else
$requete="SELECT * FROM fiche_membres
LIMIT 1 , 1 ";
mysql_query("SET NAMES UTF8");
$resultat=mysql_query($requete);
if (false === $resultat) {
echo mysql_error();
}
?>
and the result's div
<div id="gmap_result_container">
<div class="gmap_result">
<?php while($fiche_membres=mysql_fetch_array($resultat)) {?> <p>
<a class="prenom"><?php echo($fiche_membres['prenom']); ?></a>
<a> , </a>
<a class="ville"><?php echo($fiche_membres['ville']); ?></a></p>
<img src="images/avatar_unknown.jpg">
<p><?php echo($fiche_membres['experience_culinaire']); ?></p>
<p><img src="images/ensavoirplus.png" name="roll" border="0" alt="En savoir plus" align="middle" class="ensavoirplus"></p>
<?php }?>
</div>
</div>
Thank you so much for your help !!
Glad to be into the community I just discover as beginner !
Warning says that the resource is not valid which is parameter of mysql_fetch_array() method. You have to verify the reference before retrieving rows.
if(isset($_GET['recherche']))
$requete="SELECT * FROM fiche_membres WHERE ville
LIKE '%" . mysql_real_escape_string($_GET['recherche']) . "%' LIMIT 0 , 1 ";
else
$requete="SELECT * FROM fiche_membres LIMIT 1 , 1 ";
$resultat=mysql_query($requete);
if($resultat)
{
while($fiche_membres=mysql_fetch_array($resultat))
{
//
}
}