this is AdminUsersController method!!!!
<?php
namespace App\Http\Controllers;
use App\Http\Requests\UsersRequest;
use App\Photo;
use App\Role;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
class AdminUsersController extends Controller
{
public function index()
{
//
$users = User::all();
return view('admin.users.index', compact('users'));
}
public function create()
{
//
$roles = Role::lists('name','id')->all();
return view('admin.users.create', compact('roles'));
}
public function store(UsersRequest $request)
{
//
$input = $request->all();
if($file = $request->file('photo_id')) {
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=> $name]);
$input['photo_id'] = $photo->id;
}
$input['password'] = bcrypt($request->password);
User::create($input);
return redirect('/admin/users');
}
public function show($id)
{
//
return view('admin.users.show');
}
public function edit($id)
{
//
return view('admin.users.edit');
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
By this images are uploaded to /public/images
This is code im using to display in index.blade.php
<img src="/images/{{$user->photo ? $user->photo->file : 'no user photo'}}" alt="">
but images are not displayed in webpage i don't know why!!! please help me with this
my routes code:
<?php
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController#index');
Route::get('/admin', function(){
return view('admin.index');
});
Route::resource('admin/users', 'AdminUsersController');
this is my code in index.blade.php
<h1>users</h1>
<table class="table table-hover">
<thead>
<tr>
<th>Id</th>
<th>Photo</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
#if($users)
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td><img scr="/images/{{$user->photo ? $user->photo->file : 'no user photo'}}"></td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role->name}}</td>
<td>{{$user->is_active == 1 ? 'Active' : 'Not Active' }}"</td>
<td>{{$user->created_at->diffForHumans()}}</td>
<td>{{$user->updated_at->diffForHumans()}}</td>
</tr>
#endforeach
#endif
</tbody>
this is my output in which i cannot display photos but photo is uploaded into /public/images folder
okey first of all to show an image with laravel you need to use {{asset(..)}} in your <img/> ... example :
<img src="{{asset('...')}}" alt="">
then you will need to store your photos in public folder by using public_path() function for exemple
$imageName = $user->id.'.'.'jpg';
$request->file('img')->move(
public_path() . '/img/', $imageName
);
now to show the photo of your user in the public path or storage path you need to do this for exemple :
<img src="{{asset('/img/user->photo')}}" alt="">
I hope that will help you
Related
Hi im new in website develpoment and i have a problem in my project(laravel, vue.js, mysql), i created the delete api and it works well when i used postman but in the Vue file when use the axios.delete it did not work ?
btw (post,get) work well.
sorry for this english.
<template>
<div class="table">
<table>
<tr>
<th>Id</th>
<th>Matricule</th>
<th>Nom</th>
<th>Prenom</th>
<th>Email</th>
<th>Annee</th>
<th>Action</th>
</tr>
<tr v-for="etud in EtudTable" :key="etud.id">
<td>{{etud.id}}</td>
<td>{{etud.matricule}}</td>
<td>{{etud.nom}}</td>
<td>{{etud.prenom}}</td>
<td>{{etud.email}}</td>
<td>{{etud.annee}}</td>
<td><button #click.prevent="Delete(etud.id)">Supprimé</button></td>
</tr>
</table>
</div>
</template>
<script>
export default {
data(){
return {
EtudTable : []
}
} ,
created:{
},
methods:{
Delete(id) {
axios.delete('api/Etudiant/${id}').then(function (response){
let index = this.EtudTable.findIndex(etud => etud.id === id);
this.EtudTable.splice(index, 1);
});
},
getVueItems: function getVueItems() {
var _this = this;
axios.get('api/Etudiant').then(function (response) {
_this.EtudTable = response.data;
});
},
....
}
}
</script>
Route\api:
Route::delete('Etudiant/{id}', 'EtudiantController#Delete')->name('etudiant.delete');
EtudiantController.php:
<?php
namespace App\Http\Controllers;
use App\Etudiant;
use Illuminate\Http\Request;
class EtudiantController extends Controller
{
...
public function Delete($id){
$etudiant = Etudiant::find($id);
$etudiant->delete();
return response()->json('successfully deleted');
}
...
}
Etudiant model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Etudiant extends Model
{
protected $fillable = ['matricule', 'nom', 'prenom', 'email', 'annee'];
}
axios.delete(`api/Etudiant/${id}`).then(....)
Use backtick ` not '.
Reference link
Get user's answer , question in page by user id , i want to display table for each user contains his answers with questions
I tried to create show page and add show function in UserController
Controller :
public function show($id)
{
$user = User::find($id);
$user_id = $user->id;
$survey = \App\Survey::pluck('title', 'id')->toArray();
$answers = \App\Answer::where('user_id','=',$user_id)->get();
return view('users.show', compact('user','survey','answers'));
}
view:
<table class="table">
<thead class="thead-light">
<tr>
<th>{{ __('Question') }}</th>
<th>{{ __('Answers') }}</th>
<th>{{ __('Creation Date') }}</th>
</tr>
</thead>
<tbody>
#foreach($answers as $t)
<tr>
<td> {{ optional($t->survey)->title }} </td>
<td> {{ $t->answer }} </td>
<td> {{$t->created_at}} </td>
</tr>
#endforeach
</tbody>
</table>
Answer model :
class Answer extends Model
{
protected $fillable = ['answer','commentaire','user_id','survey_id','last_ip'];
protected $table = 'answer';
public function survey()
{
return $this->belongsTo('App\Survey', 'survey_id');
}
public function question()
{
return $this->belongsTo(Question::class);
}
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
}
user model :
public function questions()
{
return $this->hasMany(Question::class);
}
public function answers()
{
return $this->hasMany(Answer::class);
}
the tables :
answer :
Survey :
I got empty part for question row
You are performing some queries that you actually don't need.
First get rid of this to lines:
$user_id = $user->id; $survey = \App\Survey::pluck('title', 'id')->toArray();
Then change your query to get your answers:
$answers = \App\Answer::where('user_id','=',$user->id)->with(['survey'])->get();
return view('users.show', compact('user','answers'));
Now in your view you could just do this:
<td> {{ $t->survey->title }} </td>
<td> {{ $t->answer }} </td>
<td> {{$t->created_at}} </td>
I just started using soft delete and I am not really sure how to do it, I am just following someone example and is still unsure how to do a proper soft delete. All I want is to delete the personal_info table but I keep on getting a no error message which make me at lost since I don't know what I am doing wrong, can someone help me? Thanks a lot
home.blade.php
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Action </big></strong></th>
</tr>
<td>
<tr>
#foreach($data as $value)
<tr>
<th>{{$value->Name}}</th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="post">
<button>Delete</button>
</form></th>
</tr>
#endforeach
</tr>
</tr>
</table>
Controller:
public function delete($id = 0){
if ($id > 0){
personal_info::destroy($id);
}
return redirect("/home");
}
or should I do it this way?
public function delete($id){
$data = personal_info::find($id)->delete();
return redirect("/home");
}
personal_info model:
use Illuminate\Database\Eloquent\Model;
use Eloquent;
use SoftDeletes;
class personal_info extends Eloquent
{
protected $fillable = array('Name');
protected $table = 'personal_infos';
protected $primaryKey = 'id';
protected $dates = ['deleted_at'];
public function user_info1s() {
return $this->hasMany('App\user_info1','user_id');
}
Route: (not sure should I use DELETE instead)
Route::get('/home/{id}/delete', 'HomeController#delete');
There is no need to use delete on the method. try to change your function to this
public function delete($id){
personal_info::findOrFail($id)->delete();
return back();
}
Edit
The Route method and Form method must be the same.
Change
Route::get('/home/{id}/delete', 'HomeController#delete');
To
Route::post('/home/{id}/delete', 'HomeController#delete');
Controller Method
use Carbon\Carbon;
public function delete($id)
{
personal_info::find($id)->update([
'deleted_at' => Carbon::now()
]);
return redirect()->back();
}
home.blade.php
<table class="table table-bordered">
<tr>
<th><strong><big>Name: </big></strong></th>
<th><strong><big>Action </big></strong></th>
</tr>
<td>
<tr>
// Example of adjusting your query to support soft deletes
#php
$data = Some\Model::whereNotNull('deleted_at')->get();
#endphp
#foreach($data as $value)
<tr>
<th>{{$value->Name}}</th>
<th><form action="{{ url('/home/'.$value->id.'/delete') }}" method="post">
<button>Delete</button>
</form></th>
</tr>
#endforeach
</tr>
</tr>
</table>
i am trying to develop a messaging system in my laravel app, i have managed to send a message to the user and retrieve them in the inbox section of my blade view. so my wonder is, how exactly am i going to reply a message that is in the inbox??
here is how i have them in the mail.blade.php
<table class="table table-inbox table-hover">
#foreach($messages as $meso => $message)
<tbody>
<tr class="unread">
<td class="inbox-small-cells">
<input type="checkbox" class="mail-checkbox">
</td>
<td class="inbox-small-cells"><i class="fa fa-star"></i></td>
<td class="view-message dont-show">{{ $message->sent_to_id }}</td>
<td class="view-message dont-show">{{ $message->name }}</td>
<td class="view-message dont-show">{{ $message->subject }}</td>
<td class="view-message view-message"><p>{{ $message->body }}</p></td>
<td class="view-message inbox-small-cells"><i class="fa fa-paperclip"></i></td>
<td class="view-message text-right">{{ $message->created_at }}</td>
<td class="view-message dont-show"><button data-toggle="modal" data-target="#squarespaceModal" class="btn btn-primary center-block">Reply</button></td>
</tr>
</tbody>
#endforeach
</table>
and my User Model is:
<?php
namespace App;
use App\Http\Controllers;
use App\Message;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
public function sendMessageTo($recipient, $message, $subject)
{
Auth::user()->sendMessageTo($request->recipient, $request->subject, $request->body);
}
protected $fillable = ['name', 'email', 'password'];
// A user can send a message
public function sent()
{
return $this->hasMany(Message::class, 'sender_id');
}
// A user can also receive a message
public function received()
{
return $this->hasMany(Message::class, 'sent_to_id');
}
public function message()
{
return $this->belongsTo(Message::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}
and here goes my Message model:
<?php
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Message extends Model
{
protected $fillable = ['body', 'subject', 'sent_to_id', 'sender_id'];
// A message belongs to a sender
public function sender()
{
return $this->belongsTo(User::class, 'sender_id');
}
// A message also belongs to a receiver
public function receiver()
{
return $this->belongsTo(User::class, 'sent_to_id');
}
public function user()
{
return $this->belongsTo(User::class);
}
public function message()
{
return $this->belongsTo(Message::class);
}
}
My MessageController
public function sendMessage(Request $request)
{
// Do the validation...
// Send the message from the current user to the user with ID of 1,
// You probably always want the current logged-in user as the sender.
// We talk about the recipient later...
//
Auth::user()->sent()->create([
'body' => $request->body,
'subject' => $request->subject,
'sent_to_id' => $request->recipient,
]);
$messages = Message::where('sent_to_id', '=', Auth::user()->name)->get();
$users = User::where('id', '!=', Auth::id())->get()->pluck('name', 'id');
return view('mail')->with(compact('messages', 'users'));
}
Any tips of how to reply to this messages
i want to upload the image profile and the user id from my android application to database. i tried this method and it work in local but when i test it with (POSTMAN) a rest client it doesn't work. i think because i call a view.
this is my code please help me.
my controller
class Users extends REST_Controller {
function __construct()
{ parent::__construct();
$this->load->model('Users_model','',TRUE);}public function index_get()
{
$this->load->view('users');
}
public function save_image_post()
{
$id = $_POST["id"];
$url = $this->do_upload();
$this->Users_model->save_image($id, $url);
//$this->Users_model->save_image($url);
}
private function do_upload()
{
$type = explode('.', $_FILES["pic"]["name"]);
$type = strtolower($type[count($type)-1]);
$url = "./images/".uniqid(rand()).'.'.$type;
if(in_array($type, array("jpg", "jpeg", "gif", "png")))
if(is_uploaded_file($_FILES["pic"]["tmp_name"]))
if(move_uploaded_file($_FILES["pic"]["tmp_name"],$url))
return $url;
return "";
}
}
this is my model:
public function save_image($id,$url)
{
$this->db->set('id', $id);
$this->db->set('image_user', $url);
$this->db->insert('users');
}
this is my view:
<!DOCTYPE html><html lang="en"><head></head><body>
<?php echo form_open_multipart('api/users/save_image/'); ?>
<table class="table">
<tr>
<td>Id</td>
<td><?php echo form_input('id'); ?></td>
</tr>
<tr>
<td>Image</td>
<td><?php echo form_upload('pic'); ?></td>
</tr>
<tr><td></td>
<td><?php echo form_submit('submit', 'Save', 'class="btn btn-primary"'); ?></td>
</tr>
</table></body></html>