This type of question has been asked before but not one addresses my particular query. I have tried all the solutions but non seem to work.
I am building a Blog with Laravel and this particular error occurs when I try to edit any of my posts. You are all encouraged to participate. thank you.
edit.blade.php
#extends('layouts.app')
#section('content')
#if(count($errors)>0)
<ul class="list-group">
#foreach($errors->all() as $error)
<li class="list-group-item text-danger">
{{$error}}
</li>
#endforeach
</ul>
#endif
<div class="panel panel-default">
<div class="panel-heading">
Edit post{{$post->title}}
</div>
<div class="panel-body">
<form action="{{ route('post.update', ['id'=>$post->id])}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" value="{{$post->title}}">
</div>
<div class="form-group">
<label for="featured">Featured Image</label>
<input type="file" name="featured" class="form-control">
</div>
<div class="form-group">
<label for="category">Select a Category</label>
<select type="file" name="category_id" id="category" class="form-control">
#foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea name="content" id="content" cols="5" rows="5" class="form-control" >{{$post->content}}</textarea>
</div>
<div class="form-group">
<div class="text-center">
<button class="btn btn-success" type="submit">Update Post</button>
</div>
</div>
</form>
</div>
</div>
#stop
PostController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Category;
use App\Post;
use Session;
class PostController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('admin.posts.index')->with('posts', Post::all());
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$categories =Category::all();
if ($categories-> count()==0){
Session::flash('info', 'You must have some categories before attempting to create a post');
return redirect()->back();
}
return view('admin.posts.create')->with('categories', Category::all());
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'title'=> 'required|max:255',
'featured'=>'required|image',
'content'=>'required',
'category_id' => 'required'
]);
$featured=$request->featured;
$featured_new_name= time().$featured->getClientOriginalName();
$featured->move('uploads/posts', $featured_new_name);
$post=Post::create([
'title'=> $request->title,
'content'=> $request->content,
'featured'=> 'uploads/posts/' .$featured_new_name,
'category_id'=> $request->category_id,
'slug' => Str::slug($request->title)
]);
Session::flash('success', 'Post created Successfully');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$post=Post::find($id);
return view('admin.posts.edit')->with('post', Post::find($id)->with('categories', Category::all()));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$post =Post::find($id);
$this->validate($request, [
'title'=> 'required',
'content'=>'required',
'category_id'=>'required']
);
if($request->hasfile('featured')){
$featured=$request->featured;
$featured_new_name=time() . $featured->getClientOriginalName();
$featured->move('uploads/posts', $featured_new_name );
$post->featured=$featured_new_name;
}
$post->title=$request->title;
$post->content=$request->content;
$post->category_id=$request->category_id;
$post->save();
Session::flash('success', 'Your post was just updated.');
return redirect()->route('posts');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$post =Post::find($id);
$post->delete();
Session::flash('success', 'Your post was just Trashed.');
return redirect()->back();
}
}
Property [title] does not exist on the Eloquent builder instance
This error message is telling you that you are trying to load a property named 'title' on an entity of type 'Eloquent builder'.
Eloquent builder is the type of object which can be used to query the database. The results of a call to ->first() on an Eloquent builder instance would be a Property entity, which is likely what you want.
Please examine and share the code where the Property is being loaded from the database. Do you do something, such as ->first(), to execute the query?
Related
I'm getting this error when I'm trying to register something from a form. I'm going to post the code and after that explain the situation
View Code
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Crear Especialidad</h1>
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('especialidades.create') }}">
#csrf
<div class="form-group row">
<label for="nombre" class="col-md-4 col-form-label text-md-right">{{ __('Nombre') }}</label>
<div class="col-md-6">
<input id="nombre" type="text" class="form-control #error('nombre') is-invalid #enderror" name="nombre" value="{{ old('nombre') }}" required autocomplete="nombre" autofocus>
#error('nombre')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Registrar') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Controller code
public function create(array $data)
{
return Especialidades::create($data);
}
Routes
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/nuevaEspecialidad', [EspecialidadesController::class,'nuevo'])->name('nuevaEspecialidad');
Route::resource('/especialidades', EspecialidadesController::class);
Route::get('/gestionarMedicos', [PersonaController::class,'mostrarMedicos'])->name('personaMostrarMedicos');
Dunno if it is a problem with overwriting or something similar but the resource route should support post for the create function. I'm trying to insert the data from the form, a name as you can see in the view to the database.
I'm new on Laravel so dunno what can be the cause. I will gladly provide you with extra code if needed or explain better the situation if it isn't explained enough.
In your case, it should be {{ route('especialidades.store') }}
The reason being that default method for creating a row in db in a resource controller is App\Http\Controllers\<YourResourceControllerName>#store
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
Here the create method is only for showing the form of the store method and not the function for storing the data itself.
You are using resource, where especialidades.create it will accept get method. Your submit method is post, so you need to change :
{{ route('especialidades.store') }}
I have made a simple form when I am calling the form from api route it is showing an error that "errors" is an undefined variable when I am calling using web route it just works fine and shows no error. Why is this happening? Since error is a pre defined variable but why is it showing error.
Layout file:
#extends('layout')
#section('content')
<h1 class="title">Simple Form</h1>
<form method="POST" action="/website/atg/public/projects">
#csrf
<div class="field">
<label class="label" for="name">Name</label>
<div class="control">
<input type="text" class="input" name="name" placeholder="Enter Name" value="{{old('name')}}" required>
</div>
</div>
<div class="field">
<label class="label" for="email">E-mail</label>
<div class="control">
<input type="text" class="input" name="email" placeholder="Enter E-mail Address" value="{{old('email')}}" required>
</div>
</div>
<div class="field">
<label class="label" for="pincode">Pincode</label>
<div class="control">
<input type="text" class="input" name="pincode" placeholder="Enter Pincode" value="{{old('pincode')}}" required>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button">Submit</button>
</div>
</div>
#if($errors->any())
<div class="notification">
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
</form>
#endsection
Routes file:
<?php
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/
/*Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
*/
Route::apiResource('projects','ATGController');
Controller file:
<?php
namespace App\Http\Controllers;
use App\Project;
use App\Mail\ProjectCreated;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
class ATGController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('projects.index');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
request()->validate([
'name'=>'required|unique:projects,name',
'email'=>'required|email|unique:projects,email',
'pincode'=>'required|digits:6'
]);
if ($validator->fails()) {
return redirect('/projects')
->withErrors($validator)
->withInput();
}
else{
$project=Project::create(request(['name','email','pincode']));
\Mail::to('sbansal1809#gmail.com')->send(
new ProjectCreated($project)
);
//echo '<script>alert("User added sucessfully!")</script>';
return response()->json($project);
}
}
/**
* Display the specified resource.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function show(Project $project)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function edit(Project $project)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Project $project)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Project $project
* #return \Illuminate\Http\Response
*/
public function destroy(Project $project)
{
//
}
}
you have to return validator fails like this, to be able to get $errors in your blade. Check below example for reference and change the code to your parameters
don't forget to import use Illuminate\Support\Facades\Validator;
public function store(Request $request){
$validator = Validator::make($request->all(), [
'name'=>'required|unique:projects,name',
'email'=>'required|email|unique:projects,email',
'pincode'=>'required|digits:6'
]);
if ($validator->fails()) {
return redirect('/projects/create')
->withErrors($validator)
->withInput();
}else{
$project=Project::create(request(['name','email','pincode']));
\Mail::to('sbansal1809#gmail.com')->send(
new ProjectCreated($project)
);
//echo '<script>alert("User added sucessfully!")</script>';
return response()->json($project);
}
}
Im doing a university project which is a foodlog, where the user is able to log in and then log their food items. Ideally this will post to the database, then the user will be able to view all their logged foods displayed in a table.
at the minute im trying to post the form information to the database and it doesnt seem to work. I also want the user's id to be logged in the database which is a foreign key in the food log table (from users table).
This is my PostsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostsController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store()
{
Post::create([
'id'->request('id');
'user_id'->Auth::user()->id;
'name'->request('name');
'servings'->request('servings');
'servingsize'->request('servingsize');
'calories'->request('calories');
'fat'->request('fat');
]);
return redirect('/foodlog');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
This is my add.blade.php
#extends('layouts.app')
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<form method="POST" action="/posts">
{{ csrf_field() }}
<h1>Add to Foodlog</h1>
<div class="form-group">
<label for="item">Item:</label>
<input type="text" id="name" name="name" class="form-control">
</div>
<div class="form-group">
<label for="noOfServings">Number of Servings:</label>
<input type="number" id="servings" name="servings" class="form-control">
</div>
<div class="form-group">
<label for="servingSize">Serving Size (g):</label>
<input type="number" id="servingsize" name="servingsize" class="form-control">
</div>
<div class="form-group">
<label for="calories">Calories (kcal):</label>
<input type="number" id="calories" name="calories" class="form-control">
</div>
<div class="form-group">
<label for="fat">Fat (g):</label>
<input type="number" id="fat" name="fat" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Add</button>
</form>
</div>
</div>
</div>
#endsection
And my web.php
<?php
Route::get('/', 'FoodlogController#index');
Route::get('/add', 'FoodlogController#add');
Route::get('/account', 'FoodlogController#account');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/foodlog', function() {
return view('/foodlog');
});
Route::get('/logout', function(){
Auth::logout();
return Redirect::to('welcome');
});
Route::post('/posts', 'PostsController#store');
I have also included a screenshot of my create foodlog migrations file so that you can see the fields in my table
Also my Post model is empty as you can see
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
//
}
Your store function should looks like:
public function store(Request $request)
{
Post::create([
'id' => $request->id, //<--- if this is autoincrement then just delete this line
'user_id' => Auth::user()->id,
'name' => $request->name,
'servings' => $request->servings,
'servingsize' => $request->servingsize,
'calories' => $request->calories,
'fat' => $request->fat
]);
return redirect('/foodlog');
}
For relationship between tables please follow this link
Add to Post model fillable fields:
protected $fillable = [
//if id is not autoincrement then add 'id'
'user_id',
'name',
'servings',
'servingsize',
'calories',
'fat'
];
I have two tables, a table "métier" and a table "tâche" with one to many connection, a "métier" has several "tâche". In my form "ajouter tâches" I have a combobox from which I choose the "métier" associated with the "tâche". When i validate the creation i got the error above. Thank you for helping me.
Create blade.php
#extends('Layouts/app')
#section('content')
#if(count($errors))
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors ->all() as $message)
<li>{{$message}}</li>
#endforeach
</ul>
</div>
#endif
<div class="container">
<div class="row"></div>
<div class="col-md-12">
<form action=" {{url ('tache') }}" method="post">
{{csrf_field()}}
<div class="form-group">
<label for="">Libelle Tache</label>
<input type="text" name ="libelle_tache" class="form-
control"value="{{old('libelle_tache')}}">
</div>
<div class="form-group">
<label for="">Tarif</label>
<input type="text" name ="Tarif" class="form-
control"value="{{old('tarif')}}">
</div>
<div class="form-group">
<label for="metier">metier</label>
<select name="metier_id" id="metier" class="form-
control">
#foreach($metiers as $metier)
<option value="{{$metier->id}}">
{{$metier->libelle_metier}}
</option>
#endforeach
</select>
</div>
<div class="form-group">
<input type="submit" value = "enregistrer"
class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
<script>
#endsection
tachecontroller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tache;
use App\Metier;
class TacheController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$Listmetier=metier::orderBy('libelle_metier')->get();
$Listtache=tache::all();
return view('tache.index',['tache'=>$Listtache]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//$metiers = Costcenter::lists('libelle_metier', 'id');
$metiers = Metier::orderBy('id', 'desc')->get();
return view('tache.create')->with('metiers', $metiers);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$tache = new Tache();
$tache ->libelle_tache =$request->input('libelle_tache');
$tache ->Tarif =$request->input('Tarif');
$tache ->metier_id =$request->input($tache->id);
$tache->save();
return redirect('tache');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$tache=Tache::find($id);
return view('tache.edit',['libelle_tache'=>$metier],
['Tarif'=>$tache]);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$tache =Tache::find($id);
$tache->delete();
return redirect('tache');
}
}
Model1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tache extends Model
{
public function metier()
{
return $this->belongsTo(Metier::class);
}
public function tarificationtache()
{
return $this->hasMany(Tarificationtache::class);
}
}
Model2
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class metier extends Model
{
public function metier()
{
return $this->hasMany(Tache::class);
}
}
screenshot of the form
Change:
$tache ->metier_id = $request->input($tache->id);
To:
$tache ->metier_id = $request->input('metier_id');
I created a login form and a registration form in my eComerace Application but when I submit my login form ( sign in ) it returns nothing ! I have typed username and password correct but my login page is not working.
Routes
Route::get('users/signin', 'UsersController#getSignin');
Route::post('users/signin', 'UsersController#postSignin');
Route::resource('users', 'UsersController');
Route::get('/', 'StoreController#index');
Route::get('store/category/{id}', 'StoreController#getCategory');
Route::get('store/search', 'StoreController#getSearch');
Route::get('/admin', function () {
return view('welcome');
});
Route::resource('store', 'StoreController');
Route::resource('admin/categories', 'CategoriesController');
Route::resource('admin/products', 'ProductsController');
UsersController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\Http\Requests;
use App\Category;
use App\product;
use Hash;
use View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
class UsersController extends Controller
{
public function __construct(){
parent::__construct();
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function getSignin(){
return View::make('users.signin');
}
public function index()
{
return Hash::make('ifti');
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return View::make('users.newaccount');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::to('users/signin')->with('message','Thank you for creating new account.Sign in now');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function getSignout(){
Auth::logout();
return Redirect::to('users/signin')->with('message','Signouted!');
}
public function postSignin(){
$password = Hash::make(Input::get('password'));
$credentials = array('name' => Input::get('name'), 'password' => $password);
if(Auth::attempt($credentials)){
return Redirect::to('http://localhost/ecom/')->with('message','Thanks for signin');
}
return Redirect::to('users/signin')->with('message','Was Incorrect DATA!');
}
}
Signin View
{!! Form::open(array('url' => 'users/signin' , 'method' => 'post')) !!}
<div class="form-group">
<label for="username">User Name:</label>
{!! Form::text('name') !!}
</div>
<div class="form-group">
<label for="username">Password:</label>
{!! Form::password('password') !!}
</div>
<button type="submit" class="btn btn-default">Sign IN</button>
{!! Form::close() !!}
New Account View
<div class="col-md-8 col-lg-8 col-sm-12 col-xs-12">
{!! Form::open(array('url' => 'users/create' , 'method' => 'post')) !!}
<div class="form-group">
<label for="username">User Name:</label>
<input type="username" class="form-control" name="name" id="name">
</div>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" name="email" id="name">
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" class="form-control" name="password" id="name">
</div>
<button type="submit" class="btn btn-default">Create New Account</button>
{!! Form::close() !!}
</div>