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
Related
#foreach($posts as $post)
<tr>
<th scope="row">{{$post->id}}</th>
<td><img src="{{asset('img/') . '/' .$post->file_path}}" width="100px " height="50px" class="img_table"></td>
<td>{{$post->post_name}}</td>
<td>{{$post->name}}</td>
<td>
#if(strcmp($post->status , 'Published') == 0)
<div class="btn btn-success">{{$post->status}}</div>
#else
<div class="btn btn-warning">{{$post->status}}</div>
#endif </td>
<td>{{$post->created_at->diffForHumans()}}</td>
<td>{{$post->slug}}</td>
<td>{{$post->cat->game_category_id}}</td>
<td><i class="far fa-edit"></i></td>
<td><i class="far fa-trash-alt"></i></td>
</tr>
#endforeach
Post Model
protected $table = 'posts';
protected $fillable = [
'post_name',
'post_content',
'name',
'status',
'file_path',
'slug',
'game_category_id'
];
public function cat(){
return $this->belongsTo('App\cat');
}
Cat Model
use HasFactory;
protected $table = 'cats';
protected $fillable = ['name','slug'];
public function post(){
return $this->hasMany('App\post');
}
Relationship model name is false, must be as the name of them Model
please use class Cat::class is better and readable
public function cat(){
return $this->belongsTo(Cat::class);
//return $this->belongsTo('App\Cat');
}
Same for post
public function post(){
return $this->hasMany(Post::class);
}
Change this line:
<td>{{$post->cat->game_category_id}}</td>
to:
<td>{{$post->cat ? $post->cat->game_category_id : ''}}</td>
It first checks if the related cat exists, if it is, then gets the id, else it returns an empty string.
Attempt to read property "game_category_id" on null (View: E:\xampp\htdocs\Blog\resources\views\layout\show.blade.php)
I'm trying to make a relationship in Laravel but always get the error:
Trying to get property 'nama_guru' of non-object (View: D:\xampp\htdocs\supervisi_digital\resources\views\ManagementSupervisi\index.blade.php).
Here is my code:
Dokumen Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Dokumen extends Model
{
protected $table = "dokumen";
protected $primarikey = "id";
protected $fillable =
[
'id', 'nama_guru', 'mapel', 'file', 'keterangan'
];
public function supervisi()
{
return $this->hasMany(Supervisi::class);
}
}
Supervisi Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Supervisi extends Model
{
protected $table = "supervisi";
protected $primarikey = "id";
protected $fillable =
[
'id', 'id_guru', 'id_mapel', 'id_keterangan', 'penilaian'
];
public function dokumen()
{
return $this->belongsTo(Dokumen::class);
}
}
Blade View
<table>
<thead>
<tr>
<th>#</th>
<th>NAMA GURU</th>
<th>MAPEL</th>
<th>KETERANGAN</th>
<th>PENILAIAN</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
#foreach ($dtSupervisi as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->dokumen->nama_guru }}</td>
<td></td>
<td></td>
<td></td>
</td>
</tr>
#endforeach
</tbody>
</table>
Controller
public function index()
{
$dtSupervisi = Supervisi::with('dokumen')->paginate(10);
return view('ManagementSupervisi.index', compact('dtSupervisi'));
}
I don't know what the problem is.
You need to define the foreign key and local key in your relationship.
public function dokumen()
{
return $this->belongsTo(Dokumen::class,'foreign_key','localkey');
}
so
public function dokumen()
{
return $this->belongsTo(Dokumen::class,'id_guru','id');
}
protected $fillable =
[
'id', 'id_guru', 'id_mapel', 'id_keterangan', 'penilaian', 'dokumen_id'
];
Use dokumen_id in Supervisi model and supervisi table after migrate database may be it's work
I want to get interest amount which is multiple of amount and interest rate in a table "loan". I want to call the value from a table and used for display loan information.
I have tried using mutators in which case gives same error as mentioned below
Loan.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Loan extends Model
{
protected $fillable = ['amount', 'interest', 'status', 'member_id', 'loan_type_id', 'interest_type_id', 'loan_payment_type_id'];
public function getInterestAmountAttribute()
{
return $this->amount * $this->interest;
}
public function member()
{
return $this->belongsTo(Member::class, 'member_id', 'id');
}
}
loan.blade.php
<table class="table table-stripped">
<thead>
<tr>
<th>Loan Id</th>
<th>Amount</th>
<th>Interest</th>
<th>Interest Amount</th>
<th>Interest Type</th>
<th>Loan Type</th>
<th>Status</th>
<th>Payment Type</th>
<th>Member</th>
<th>Action</th>
</tr>
#foreach($loanData as $key=>$loan)
<tr>
<td>{{++$key}}</td>
<td>{{$loan->amount}}</td>
<td>{{$loan->interest}}</td>
<td>{{ $loan->interest_amount}}</td>
<td>{{$loan->interesttype->interest_type}}</td>
<td>{{$loan->loantype->loan_type}}</td>
<td align="center">
<span class="bg bg-primary" style="border-radius: 10px;padding:2px 5px">{{$loan->status}}</span>
<form action="{{route('update-loan-status')}}" method="post">
{{csrf_field()}}
<input type="hidden" name="criteria" value="{{$loan->id}}"/>
#if($loan->status== 1)
<button class="btn btn-success btn-xs" name="paid"><i class="fa fa-check"></i></button>
#endif
#if($loan->status== 0)
<button class="btn btn-danger btn-xs" name="unpaid"><i class="fa fa-close"></i></button>
#endif
</form>
</td>
<td>{{$loan->paymentmethod->method}}</td>
<td>{{$loan->member->name}}</td>
<td>
Delete
Edit
</td>
</tr>
#endforeach
</thead>
</table>
{{$loanData->links()}}
This gives the following error:
Method Illuminate\Database\Eloquent\Collection::links does not exist.
when I remove brackets
<td>{{$loan->getInterest}}</td>
the error is
App\Loan::getInterest must return a relationship instance.
LoanController.php
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use App\Loan;
use App\Interest;
use App\LoanPaymentMethod;
use App\Member;
use App\Loantype;
use DB;
class LoanController extends Controller
{
protected $_backendPath = 'backend.';
protected $_pagePath = 'backend.pages.';
protected $_data = [];
public function index()
{
$loanData = Loan::orderBy('id', 'desc')->get();
$loanData = Loan::paginate(10);
$loanData = Loan::all();
$memberData = Member::all();
$loantypeData = Loantype::all();
$paymentmethodData = LoanPaymentMethod::all();
$interestData = Interest::all();
$this->_data['loanData'] = $loanData;
//$results = Loan::with('member')->get();
//$dat->loan = Loan::with('member')->get();
//$loan = Member::find($memberData)->loan;
return view($this->_pagePath . 'loan.loan', $this->_data);
//$userData = DB::table('members')->get();
//return view('home',compact('userData'));
}
}
Define your function as accessor in your Loan.php :
public function getGetInterestAttribute()
{
return $this->amount * $this->interest;
}
Now you can access it , like this :
<td>{{ $loan->get_interest }}</td>
The right way to call an Accessor,
public function getModifiedInterestAttribute() // first get then ModifiedInterest then Attribute
{
return $this->amount * $this->interest;
}
Then you can call the Accessor like below,
<td>{{$loan->modified_interest }}</td>
You can see this for more details : https://laravel.com/docs/5.8/eloquent-mutators#defining-an-accessor
You should append that variable in model data Like
class Loan extends Model
{
protected $appends = ['interest'];
}
Your accessor should like
public function getInterest()
{
return $this->amount * $this->interest;
}
Access in blade file as you use above
<td>{{$loan->interest}}</td>
I'm creating a web based interface for my dovecot database.
I can get a list of all the virtual domains in the database and the number of emails and aliases easily enough.
But when I try to load a page to list the emails under a specific domain, it goes weird.
Three simple models:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class VirtualDomain extends Model
{
public function emails()
{
return $this->hasMany('App\VirtualUser', 'domain_id');
}
public function aliases()
{
return $this->hasMany('App\VirtualAlias', 'domain_id');
}
}
class VirtualUser extends Model
{
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
];
}
class VirtualAlias extends Model
{
//
}
My default controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\VirtualDomain;
use App\VirtualUser;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* #return \Illuminate\Http\Response
*/
public function index()
{
return view('home', [
'domains' => VirtualDomain::all(),
]);
}
public function domain($name)
{
$domain = VirtualDomain::where('name', $name)->first();
return view('domain', [
'domain' => $domain,
'emails' => VirtualUser::where('domain_id', $domain->id),
]);
}
}
and a couple of simple blades
home.blade.php
<p>
{{ $domains->count() }} domains
</p>
<table class="table-summary">
<thead>
<tr>
<th>name</th>
<th>emails</th>
<th>aliases</th>
<th class="table-summary-options">options</th>
</tr>
</thead>
<tbody>
#foreach ($domains as $domain)
<tr>
<td><a title="view emails for this domain" href="domain/{{ $domain->name }}">{{ $domain->name }}</a></td>
<td>{{ $domain->emails->count() }}</td>
<td>{{ $domain->aliases->count() }}</td>
<td class="table-summary-options"><a class="ui-action" title="remove this domain" href=""><img src="/img/ui/remove.png" alt="remove"></a></td>
</tr>
#endforeach
</tbody>
</table>
and domain.blade.php
<p>
<< - {{ $domain->name }} - {{ $emails->count() }} emails
</p>
<table class="table-summary">
<thead>
<tr>
<th>email</th>
<th class="table-summary-options">options</th>
</tr>
</thead>
<tbody>
#foreach ($emails as $email)
<tr>
<td><a title="view aliases for this domain" href="email/{{ $email->email }}">{{ $email->email }}</a></td>
<td class="table-summary-options"><a class="ui-action" title="remove this email" href=""><img src="/img/ui/remove.png" alt="remove"></a></td>
</tr>
#endforeach
</tbody>
</table>
The view outputs the correct number of emails under the domain with {{ $emails->count() }} - but the#foreach ($emails as $email)` does not loop.
When I modify the blade to simple use the emails from the domain variable ({{ $domain->emails->count() }} and #foreach ($domain->emails as $email)), I get the right count and the list is populated correctly.
What's making it go wrong when using the emails variable?
You have to make a small change for it to work
public function domain($name)
{
$domain = VirtualDomain::where('name', $name)->first();
return view('domain', [
'domain' => $domain,
'emails' => VirtualUser::where('domain_id', $domain->id)->get(),
]);
}
Without ->get() you will get a query builder instance while with get() will return a collection. In the foreach loop a collection can be iterated while a query builder instance can't be.
Hope this helps
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