I'm still learning Laravel and how the blade system all works..
I'm wondering what the better way of doing something is and that something is; I've done a query to pull a row from the database and then I want to put that query into an array so I can pick out whatever column I want.
Then I want to pass that to my home.blade.php and be able to use {{ $name }} for example. T
This is what I've got:
TO NOTE: THIS WORKS THE WAY I WANT IT TO BUT IM SURE IM DOING IT THE LONG WAY (WRONG) ROUND.
HomeController.php
<?php
class HomeController extends BaseController {
public function home() {
$hero_query = DB::table('heros')->where('owner_id', Auth::user()->id)->pluck('hero');
if($hero_query) {
$owner_id = Auth::user()->id;
$user = DB::table('heros')->where('owner_id', $owner_id)->first();
$name = $user->hero;
$level = $user->level;
$exp = $user->exp;
$str = $user->str;
$atk = $user->atk;
$def = $user->def;
$int = $user->int;
$blk = $user->blk;
return View::make('home', array(
'name' => $name,
'level' => $level,
'exp' => $exp,
'str' => $str,
'atk' => $atk,
'def' => $def,
'int' => $int,
'blk' => $blk
));
} else {
return View::make('home');
}
}
}
home.blade.php
#if($hero = DB::table('heros')->where('owner_id', Auth::user()->id)->pluck('hero'))
<form action="{{ URL::route('hero-delete') }}" method="POST">
Your hero:<br>
<b>{{ $hero; }}</b> | <input type="submit" value="Delete"><br>
<b>Stats:</b>
LvL: {{ $level }}
Exp: {{ $exp }}
Str: {{ $str }}
Atk: {{ $atk }}
Def: {{ $def }}
Int: {{ $int }}
Blk: {{ $blk }}
</form>
#else
<form action="{{ URL::route('hero-create') }}" method="POST">
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::token()}}
</form>
#endif
Now I'm sure I'm doing it in some stupid moronic way as I'm just starting out.. but could someone explain where I'm going wrong?
Thanks in advance!
Just pass the entire $user to the view
$user = DB::table('heros')->where('owner_id', $owner_id)->first();
return View::make('home', array('user' => $user));
Then in your view
Str: {{ $user->str }}
Int: {{ $user->int }}
and so on.
You're doing lots of things wrong :(
I fix a little bit the code, but I didn't test it, so use this as a guide to implement what you want.
class HomeController extends BaseController {
public function home() {
$hero = Hero::where('owner_id', '=', $owner_id)->first();
if($hero)
{
return Response::make('home', array('hero' => $hero->toArray()));
}
else
{
return Response::make('home');
}
}
}
The blade template
#if(isset($hero))
{{ Form::open(array('route' => 'hero-delete', 'method' => 'DELETE')) }}
Your hero:<br>
<b>{{ $hero->hero }}</b> | <input type="submit" value="Delete"><br>
<b>Stats:</b>
<!-- Here you can implement a foreach for performance -->
LvL: {{ $hero->level }}
Exp: {{ $hero->exp }}
Str: {{ $hero->str }}
Atk: {{ $hero->atk }}
Def: {{ $hero->def }}
Int: {{ $hero->int }}
Blk: {{ $hero->blk }}
{{ Form::close() }}
#else
{{ Form::open(array('route' => 'hero-create')) }}
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::close() }}
#endif
Related
here is my ContactController.php:
public function destroy($id){
$contact = Contact::find($id);
$contact->delete();
return Redirect::to('http://localhost:8000/contactsview');
}
Here is my rountes.php
Route::delete('/contactsview/destroy/{id}', array('uses'=>'ContactController#destroy'));
Here is my index.blade.php:
{{ Form::open(array('url'=>'/contactsview/delete/'.$contact->id, 'method'=>'DELETE', 'style'=>'display:inline;')) }}
<!-- {{ Form::hidden('id', $contact->id) }} -->
{{ Form::submit('Delete') }}
{{ Form::close() }}
What did I do wrong?
Try the form with this instead, passing in the $contact->id as a param rather than directly in the URL:
{{ Form::open(array('method' => 'DELETE', 'action' => array('ContactController#destroy', $contact->id )) }}
I just have a very simple product category creation form in laravel , like so:
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
For the create method i have the following code:
public function postCreate() {
$validator = Validator::make(Input::all() , Category::$rules);
if($validator->passes()) {
$category = new Category;
$category->name = Input::get('name');
$category->save();
return Redirect::to('admin/categories/index')
->with('message' , 'Category created');
}
return Redirect::to('admin/categories/index')
->with('message' , 'something went wrong')
->withError($validator)
->withInput();
}
Now when i click on the submit button, i get the following error:
C:\xampp\htdocs\ecomm\bootstrap\compiled.php
if (!is_null($route)) {
return $route->bind($request);
}
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0) {
return $this->getOtherMethodsRoute($request, $others);
}
throw new NotFoundHttpException();
}
protected function checkForAlternateVerbs($request)
You can see the error more visvually HERE.
What am i doing wrong ?
Instead of
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
try this:
{{ Form::open(array('route'=>'post.homes')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
In routes.php:
Route::post('aboutus', array('as' => 'post.homes', 'uses' => 'HomeController#postContactUs'));
I have the following route entry:
Route::get('admin/user/edit/{id}', 'AdminController#editUser');
And given is controller Method:
public function editUser($id)
{
$user = User::where('id',1);
return View::make('admin.edit_user')
->with('user',$user);
}
All I want to bind model in my edit form which looks like this:
#extends('layouts.admin_master')
#section('content')
<div>
{{ Form::model($user) }}
{{ Form::label('first_name', 'First Name') }}
{{ Form::text('first_name') }}
{{ Form::close() }}
</div>
#stop
I can see text box but value is not being populated. first_name is column in my table users
I was trying to do what mention here
Try this..
public function editUser($id)
{
$user = User::where('id',1)->first();
return View::make('admin.edit_user')
->with('user',$user);
}
#extends('layouts.admin_master')
#section('content')
<div>
{{ Form::model($user) }}
{{ Form::label('first_name', 'First Name') }}
{{ Form::text('first_name',$user->first_name) }}
{{ Form::close() }}
</div>
#stop
sorry if this is a very newbie Q..
but please help me to solve this problem. plus give me the reason about why this error happened..
this is my edit view
new.blade.php
#section('content')
#include('common.show_error')
{{Form::open(array('url'=>'author/update', 'method'=>'PUT'))}}
<p>
{{ Form::label('name', 'Name: ') }}</br>
{{ Form::text('name', $author->name) }}
</p>
<p>
{{ Form::label('bio', 'Biography: ') }}</br>
{{ Form::textarea('bio', $author->bio) }}
</p>
{{ Form::hidden('id', $author->id) }}
<p>{{ Form::submit('Edit Data') }}</p>
#stop
this is my show view
show.blade.php
#extends('layouts.default')
#section('content')
<h1>{{ $author->name }}</h1>
<p>{{ $author->bio }}</p>
<p>{{ $author->updated_at }}</p>
<span>
{{ HTML::linkRoute('authors', 'Home') }} |
{{ HTML::linkRoute('edit_author', 'Edit', array($author->id)) }} |
{{ Form::open(array('url'=>'author/destroy', 'method'=>'DELETE', 'style'=>'display: inline;')) }}
{{ Form::hidden('id', $author->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
</span>
#stop
this is my controller
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
Member::where('id','=',$id)->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated');
}
}
the case: when I try to edit data using edit button. it said:
"Trying to get property of non-object laravel"
and when I check at the error log. it refers to
<h1>{{ $author->name }}</h1>
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
$author = Member::find($id);
$author->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated')
->with('author', $author);
}
}
Little changes in your controller, try it :) In your code, you are not send variable "author" into your view.
Earlier today I had the exact same problem with Auth::attempt always retuning false. I realized that Auth checks for a hashed password, so by doing so I was able to get it to return true, but now it always does. Even if I type asdadfasdfaf in my form, the if statement loads the page. Any suggestions?
Controller:
class userController extends \BaseController
{
public function login()
{
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::attempt($user))
{
return Redirect::route('home');
}
else
{
return View::make('login');
}
}
}
Form
{{ Form::open(array('url' => 'home' )) }}
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
The Routes file:
Route::post('home', 'userController#login');
No matter what I enter it always directs me to my "home" page?
The action url should be login
{{ Form::open(array('url' => 'login' )) }}
^^^^ as you used Auth::attempt to this URL
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}