i want to fix the undifined variable $photos
upload.blade.php
#extends('layouts.app')
#section('content')
<h1>voir les modeles</h1>
<form action="/upload" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" name="Upload">
</form>
</hr>
<ul>
#foreach ($photos as $photo)
<li> {{ $photo->name }}<img src="{{ asset('storage/images/', $photo->name) }}"></li>
#endforeach
</ul>
#endsection
here is the controler photoenter code here
PhotoController
<?php
namespace App\Http\Controllers;
use App\Models\Photo;
use Illuminate\Http\Request;
class PhotoController extends Controller
{
public function create()
{
$photos = Photo::all();
return view('upload', compact('photos'));
}
public function store(Request $request)
{
// dd($request->file());
$size = $request->file('image')->getSize();
$name = $request->file('image')->getClientOriginalName();
$request->file('image')->storeAs('public/images/', $name);
$photo = new Photo();
$photo->name = $name;
$photo->size = $size;
$photo->save();
return redirect()->back();
}
}
enter code here
public function create()
{
$photos = Photo::all();
return view('upload', ['photo =>$photos'] );
}
should be,
return view('upload', ['photos' => $photos]);
Notice the incorrect placement of the single quote and the singular use of photo.
Also, please ask an actual question and don't just dump the error into the title.
Change create function code.
public function create()
{
$photos = Photo::all();
return view('upload', ['photos' => $photos] );
}
Check documentation.
//here is my controller
public function edituser(Request $request,$id){
$user=User::find($id);
$user->name=$request->name;
$user->email=$request->email;
$user->role_id=$request->role_id;
$users=User::all();
return view('edit',compact('users'));
}
//here is blade
<form action="{{route('edituser',['id'=>$user->id])}}" method="post">
#csrf
<div class="form-row align-items-center">
<div class="col-auto">
<label class="sr-only" for="inlineFormInput">Adı</label>
<input type="text" value="" class="form-control mb-2" id="inlineFormInput" placeholder="Adı">
</div>
<div class="col-auto">
<label class="sr-only" for="inlineFormInputGroup">Emaili</label>
<div class="input-group mb-2">
<input type="text" class="form-control" id="inlineFormInputGroup" placeholder="Emaili">
</div>
</div>
<select class="mdb-select md-form">
#foreach ($users as $user)
<option>--Səlahiyyət seç---</option>
<option value="1">{{$user->name}}</option>
#endforeach
</select>
//here is route
Route::get('edit/{id}','AdminController#edit')->name('edit');
Route::post('edituser/{id}','AdminController#edituser')->name('edituser');
Update: here is my all controller
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class AdminController extends Controller
{
public function delete($id){
$delete=User::where('id',$id)->delete();
if($delete){
return redirect()-> back();
}
}
public function edit(){
$users=User::all();
return view('edit',compact('users'));
}
public function edituser(Request $request,$id){
$user=User::find($id);
$user->name=$request->name;
$user->email=$request->email;
$user->role_id=$request->role_id;
}
It seems like you are using the wrong method on the controller.
The edituser method is used by the POST route, so you should have users/compact code on the edit method instead, as that is being used by the GET route.
Move the following code from edituser and put it into edit
$users = User::all();
return view('edit',compact('users'));
Your edituser method should probably look like:
public function edituser(Request $request,$id){
$user = User::find($id);
$user->name=$request->name;
$user->email=$request->email;
$user->role_id=$request->role_id;
return view('edit',compact('user'));
}
Please update your original answer to show both methods.
public function edit($id) {
$user = User::find($id);
$users = User::all();
return view('edit')->with(compact('user', 'users'));
}
public function edituser(Request $request, $id) {
$user = User::find($id);
$user->name = $request->name;
$user->email = $request->email;
$user->role_id = $request->role_id;
$users = User::all();
return view('edit')->with(compact('user', 'users'));
}
Hope fully it will help to you easily.
I cannot display session value in my view. I only want to display it to see if it's correctly set inside the controller. Is it correctly set in controller? How can I check?
I have this in view:
<div class="panel panel-success">
<form action="{{ route('get_table') }}" method="POST">
{{ csrf_field() }}
#foreach($tables as $data)
<button type="submit" name="tables" class="btn btn-primary" value="{{ $data->id }}">
{{$data->name }}
</button>
#endforeach
</form>
{{ Session::get('table_id') }}
</div>
This in ListController:
public function index() {
$tables = DB::table('tables')->get();
return view('welcome', compact('tables'));
}
public function getTable(Request $request) {
$table_id = $request->get('tables');
$request->session()->put('table_id', $table_id);
}
And this in web.php routes:
Route::get('/', 'ListController#index')->name('get_table');
Route::post('/', 'ListController#getTable');
I even tried
public function getTable(Request $request) {
$request->session()->put('table_id', '1234');
}
Nothing shows up in the view at {{ Session::get('table_id') }}
What am I doing wrong?
You can try this:
public function getTable(Request $request) {
$table_id = $request->get('tables');
return redirect()->back()->with('table_id',$table_id);
}
if you want to redirect to specific route then:
public function getTable(Request $request) {
$table_id = $request->get('tables');
return redirect()->route('RouteName')->with('table_id',$table_id);
}
and then in view:
#if(Session::has('table_id'))
{{ Session::get('table_id') }}
#endif
Hope you got your answer
I keep on getting this error whenever I try to enter the upload page.
Can anybody help?
I have already done the compact part to make sure that the variable is being passed to the view and also my route should be ok I think.
I tried using dd but all it does is keep on showing me the error
Error: Undefined variable: user (View: C:\xampp\htdocs\Evaluation\resources\views\upload.blade.php)
Here are my codes:
upload.blade.php
<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{$user->id}}">
<div class="form-group">
<label for="imageInput" class="control-label col-sm-3">Upload Image</label>
<div class="col-sm-9">
<input type="file" name="file">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
UploadController:
public function upload(){
return view(‘upload’);
}
public function store(Request $request,$id){
$this->validate($request, [
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
var_dump('has file '.$request->hasFile('file'));
if ($request->hasFile('file')) {
$image = $request->file('file');
$name = $image->getClientOriginalName();
$size = $image->getClientSize();
$id = $request->user_id;
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$Image = new Image;
$Image->name = $name;
$Image->size = $size;
// $Image->user_id = $id;
//$Image->save();
$user->find($id);
dd($user);
$user->Images()->save($Image);
}
return redirect('/home');
}
public function test(){
$user = user_information::where('id')->get();
return view('upload', compact('user'));
}
Route: (this are my route)
Route::get('/UploadUser/upload','UploadController#upload’);
Route::post('/UploadUser','UploadController#store');
Route::post('/UploadUser/upload', 'UploadController#test');
Another question: I keep on getting this error when i try to upload a file, so what should I do?
Here is the error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails (form.images,
CONSTRAINT images_user_id_foreign FOREIGN KEY (user_id) REFERENCES
usere_information (id)) (SQL: insert into images (name,
size, user_id, updated_at, created_at) values (download.png,
4247, 1, 2017-10-25 08:54:57, 2017-10-25 08:54:57))
Image model:
class Image extends Model
{
protected $fillable = array('name','size','user_id');
public function user_informations() {
return $this->belongsTo('App\user_information', 'user_id', 'id');
}
}
Images table:
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('size');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('user_informations');
$table->timestamps();
});
User_information table:
Schema::create('user_informations', function (Blueprint $table) {
$table->increments('id');
$table->engine = 'InnoDB';
$table->binary('signature');
$table->String('Name');
$table->timestamps();
});
User_information model:
class user_information extends Eloquent
{
protected $fillable = array('signature', 'Name');
protected $table = 'user_informations';
protected $primaryKey = 'id';
public function Images() {
return $this->hasOne('App\Image','user_id');
}
}
How to get the image?
Here is the view folder:
#foreach ($data as $object)
<b>Name: </b>{{ $object->Name }}<br><br>
Edit<br>
#foreach ($data3 as $currentUser)
<img src="{{ asset('public/images/' . $currentUser->Image->name ) }}">
#endforeach
#if($data3->count())
#foreach($data3 as $currentUser)
<a href="{!! route('user.upload.image', ['id'=>$currentUser->user_id]) !!}">
<button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
</a>
#endforeach
#else
<a href="{!! route('user.upload.image', ['id'=>$object->id]) !!}">
<button class="btn btn-primary"><i class ="fa fa-plus"></i>Upload Images</button>
#endif
#endforeach
HomeController:
public function getInfo($id) {
$data = user_information::where('id',$id)->get();
$data3=Image::where('user_id',$id)->get();
return view('test',compact('data','data3'));
Because you didn't pass the user to your upload view, try to pass it like this :
public function upload(){
$id = 1 //The wanted user or if the user is authenticated use Auth::id()
$user = User::find($id);
return view('upload')->withUser($user);
}
Or if the user is authenticated use Auth in the view :
<form class="form-horizontal" method="post" action="{{ url('/userUpload')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{auth()->id()}}">
<div class="form-group">
<label for="imageInput" class="control-label col-sm-3">Upload Image</label>
<div class="col-sm-9">
<input type="file" name="file">
</div>
</div>
<div class="form-group">
<div class="col-md-6-offset-2">
<input type="submit" class="btn btn-primary" value="Save">
</div>
</div>
</form>
For the second problem it's because in the route you have
Route::post('/UploadUser','UploadController#store');
and the your store method signature is
public function store(Request $request,$id){
The $id parameter that did the problem because it's not defined in the route so simply remove it from the method signatre
public function store(Request $request){
$this->validate($request, [
'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
if ($request->hasFile('file')) {
$image = $request->file('file');
$name = $image->getClientOriginalName();
$size = $image->getClientSize();
$id = $request->user_id; // here id is declared no need for the parameter
$destinationPath = public_path('/images');
$image->move($destinationPath, $name);
$Image = new Image;
$Image->name = $name;
$Image->size = $size;
$Image->user_id = $id;
$Image->save();
}
return redirect('/home');
}
For the third case you have to change the routes from :
Route::get('/UploadUser/upload','UploadController#upload’);
to
Route::get('/UploadUser/{user}/upload','UploadController#upload’)->name('user.upload.image');
And in the view add the id in the upload button url maybe like this :
{!! route('user.upload.image', ['user'=>$currentUser->id]) !!}
Then in the upload method :
public function upload(user_information $user){ // route model binding here
// dd($user); //for testing only :)
return view('upload')->withUser($user);
}
In the view change
<input type="hidden" name="user_id" value="{{auth()->id()}}">
To
<input type="hidden" name="user_id" value="{{$user->id()}}">
And you are good to go ;)
#foreach ($data as $currentUser)
<b>Name: </b>{{ $currentUser->Name }}<br><br>
Edit<br>
#if($currentUser->Image)
<img src="{{ asset('public/images/' . $currentUser->Image->name ) }}">
#endif
<a href="{!! route('user.upload.image', ['id'=>$currentUser->id]) !!}">
#endforeach
You have miss to pass id in your where condition,
public function test(){
$user = user_information::where('id',$id)->first();
return view('create1', compact('user'));
}
and you have to pass your user data into this,
public function upload(){
$user = user_information::where('id',$id)->first();
return view(‘upload’,compact('user'));
}
Hope it helps,
On your upload function, you have to pass the user variable because you use the $user in the view. So the controller will be
public function upload() {
$user = Auth::user();
return view('upload', compact('user'));
}
do not forget to change the $user based on your need.
You have to pass an $id variable into your test() method. Then please comment below what's the error next so I can follow you through.
Update
Since you don't want to pass an id. You can use:
public function test(){
$u = Auth::user();
$user = user_information::where('id', $u->id)->get();
return view('upload', compact('user'));
}
OR
Try to use first() instead of get().
More option:
I have noticed, you're using the upload() method here, why not passing the $user there? like so:
public function upload(){
$user = Auth::user();
return view(‘upload’, compact('user'));
}
I have a comment box in my app and its working fine. but i want to display the user who comment in my post how can i achieve this? i have this code so far.
This is my view
<div class="view-post">
<div class="body">
<h3>{{$posts->title}}</h3>
<h6>{{$posts->created_at->toFormattedDateString()}}</h6>
<p><img src="{{ asset('img/' . $posts->image) }}" class="img-rounded"/></p>
<p>{{$posts->content}}</p>
</div>
<hr>
<section>
<h5>LEAVE US A COMMENT</h5>
<form action="{{ URL::route('createComment', array('id' => $posts->id))}}" method="post">
<div class="form-group">
<input class="form-control" type="text" placeholder="Comment..." name="content">
</div>
<input type="submit" class="btn btn-default" />
</form>
</section><br>
<section class="comments">
#foreach($posts->comment as $comments)
<blockquote>{{$comments->content}}</blockquote>
#endforeach
</section>
</div>
My Controller
public function viewPost($id)
{
$post = Post::find($id);
$user = Auth::user();
$this->layout->content = View::make('interface.viewPost')->with('posts', $post )->with('users',$user);
}
public function createComment($id)
{
$post = Post::find($id);
$comment = new Comment();
$comment->content = nl2br(Input::get('content'));
$post->comment()->save($comment);
return Redirect::route('viewPost', array('id' => $post->id));
}
In your model you can set up relations between one and another model.
Like that..
User Model
class User extends Model {
public function comments()
{
return $this->hasMany('App\Comment');
}
}
Comment Model
class Comment extends Model {
public function user()
{
return $this->belongsTo('App\User');
}
}
So you can get the user with
$comment = Comment::find(1);
$user = $comment->user()->get();