I update the picture with the spatie media library and my picture does not appear - laravel

my controller:
public function update(SettingUpdateRequest $request, Setting $id){
$setting = Setting::first();
$setting->update($request->post());
if ($request->hasFile('site_logo')) {
$setting->addMedia($request->site_logo)->toMediaCollection('document');
}
return redirect()
->route('back.setting.index')->withMessage('Ayarlar başarıyla güncellendi!');
}
my model:
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Setting extends Model implements HasMedia
{
use HasFactory , InteractsWithMedia;
protected $table='settings';
protected $fillable=['site_title','site_desc','site_email'];
protected $appends = ['site_logo'];
public function getSiteLogoAttribute()
{
return $this->getMedia('document')->last();
}
}
my picture way in stroge:
https://prnt.sc/11q22mc
my blade:
<div class="form-group">
<label for="exampleInputFile">Site Logo</label>
<input type="file" id="exampleInputFile" name="site_logo" class="form-control" accept="image/*">
</div>
<label><strong>Resimler:</strong></label>
#if (!$setting->site_logo)
<span class="form-text text-muted">Bu Rehber ile ilgili resim bulunamadı. Yeni bir resim ekleyin!
</span>
#endif
<div class="col-lg-3">
<div class="image-input image-input-empty image-input-outline" id="kt_image_5" style="background-image: url({{$setting->getFirstMediaUrl('document')}})">
<div class="image-input-wrapper"></div>
<span class="btn btn-xs btn-icon btn-circle btn-white btn-hover-text-primary btn-shadow" data-action="cancel" data-toggle="tooltip" title="" data-original-title="Cancel avatar">
<i class="ki ki-bold-close icon-xs text-muted"></i>
</span>
<span class="btn btn-xs btn-icon btn-circle btn-white btn-hover-text-primary btn-shadow" data-action="remove" data-toggle="tooltip" title="" data-original-title="Remove avatar">
</span>
</div>
</div>
my database:
https://prnt.sc/11q2b2l
I update the picture with the spatie media library and my picture does not appear.
https://prnt.sc/11q2fvi
my console fault:
https://prnt.sc/11q2k8q

You should retrieve the last media in your blade. As you've already defined a getter in your model, you can use below code.
{{ $setting->site_logo->getUrl() }}

you should delete last image
$setting->media()->delete();
or you can use
$setting->clearMediaCollection('document');

Related

How to fetch user's profile info related to user using laravel?

I want to fetch user's profile info related to user and i have already made relationship b/w user and profile one to one relation but I don't know how to fetch this?
Does anybody have an idea please help me thanks
Database table
Profile table has User_name,phone_number,Job ,user_id
controller
public function viewprofile()
{
return view('viewprofile');
}
profile model
class Profile extends Model
{
protected $table = 'Profiles';
protected $fillable = ['User_name', 'phone_number', 'Job'];
public function user()
{
return $this->belongsTo(User::class);
}
}
User model
class User extends Authenticatable
{
public function profile()
{
return $this->hasOne(Profile::class);
}
}
profile view
<div class="col-12 col-sm-6 col-md-4 d-flex align-items-stretch">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
Digital Strategist
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>Name: Your Name</b></h2>
<p class="text-muted text-sm"><b>Job: </b> Web Designer / UX / Graphic Artist /
Coffee Lover </p>
<ul class="ml-4 mb-0 fa-ul text-muted">
<li class="small"><span class="fa-li"><i class="fas fa-lg fa-phone"></i></span>
Phone
#: + 800 - 12 12 23 52
</li>
</ul>
</div>
<div class="col-5 text-center">
<img src="../../dist/img/user1-128x128.jpg" alt="" class="img-circle img-fluid">
</div>
</div>
</div>
<div class="card-footer">
<div class="text-right">
<a href="#" class="btn btn-sm bg-teal">
<i class="fas fa-comments"></i>
</a>
<a href="#" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Profile
</a>
</div>
</div>
</div>
</div>
The users profile will be accessible through the relationship and you can get access to it by calling the relationship as a property:
$profile = auth()->user()->profile;
If you need to be able to display it in your view, then just pass it through:
public function viewprofile()
{
return view('viewprofile', ['profile' => auth()->user()->profile]);
}

Trying to get property 'nom_matiere' of non-object laravel shows me this error

i want to display the name of matiere in this page but he shows me this error Trying to get property 'nom_matiere' of non-object
i make a selection in the note page to select the matiere then give a note to here it work but when i display the name of matiere in my table it give me error
model note
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
protected $fillable = ['note'];
public function matieres()
{
return $this->belongsToMany(Matiere::class);
}
}
model matiere
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Matiere extends Model
{
protected $fillable = ['nom_matiere','coef'];
public function notes() {
return $this->belongsToMany(Note::class);
}
}
my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Note;
use App\Matiere;
class NoteController extends Controller
{
public function index()
{
$notes = Note::paginate(5);
$matieres = Matiere::all();
return view('admin.notes',compact('notes','matieres'));
}
public function store(Request $request)
{
Note::create($request->all());
session()->flash('success',' cette note a été enregistré avec succés');
return redirect()->back();
}
public function update(Request $request, $id)
{
$note = Note::findOrFail($request->note_id);
$note = Matiere::findOrFail($request->note_id);
$note->update($request->all());
session()->flash('success','cette note a été modifié avec succés');
return redirect()->back();
}
public function destroy(Request $request)
{
$note = Note::findOrFail($request->note_id);
$note->delete();
session()->flash('success','cette note a été supprimé avec succés');
return redirect()->back();
}
}
my view
<section id="no-more-tables">
<table class="table table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>id-note</th>
<th>La note</th>
<th>nom matiere</th>
<th>les actions</th>
</tr>
</thead>
<tbody>
#foreach($notes as $note)
<tr>
<td class="numeric" data-title="id-note" >{{$note->id}}</td>
<td class="numeric" data-title="Nom">{{$note->note}}</td>
<td class="numeric" data-title="Nom">{{$note->matiere->nom_matiere}}</td>
<td>
<button href="#editEmployeeModal" class="btn btn-theme" data-target="#editEmployeeModal "data-mynote="{{$note->note}}" "data-mymatiere="{{$note->nom_matiere}}" data-catid={{$note->id}} class="edit" data-toggle="modal" ><i class="material-icons" data-toggle="tooltip" title="Edit"></i> </button>
<button href="#deleteEmployeeModal" class="btn btn-theme" data-target="#deleteEmployeeModal" data-catid={{$note->id}} class="delete" data-toggle="modal" > <i class="material-icons" data-toggle="tooltip" title="Delete"></i> </button>
</td>
</tr>
</tbody>
#endforeach
</table>
<div class="text-center">
{{ $notes->links() }}
</div>
<div class="clearfix">
<div class="hint-text">Affichage de <b>5</b> sur <b>25</b> entrées</div>
<div id="addEmployeeModal" href="create" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{route('notes.store')}}" method="post">
{{csrf_field()}}
<div class="modal-header">
<h4 class="modal-title">Ajouter note</h4>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>La note</label>
<input type="text" id="note" name="note" class="form-control" required>
</div>
</div>
<div class="form-group select">
<select name="matiere_id">
<option value="">--selectionner la mtiére svp --</option>
#foreach($matieres as $matiere)
<option value="{{ $matiere->id }}">{{ $matiere->nom_matiere }}</option>
#endforeach
</select>
</select>
</div>
<div class="modal-footer">
<input type="button" class="btn btn-default" data-dismiss="modal" value="Annuler">
<input type="submit" class="btn btn-success" value="Ajouter">
</div>
</form>
</div>
</div>
You're calling $note->matiere in your view but the relationship is called matieres, which would be a collection.
Instead of:
<td class="numeric" data-title="Nom">{{ $note->matiere->nom_matiere }}</td>
You'd need:
<td class="numeric" data-title="Nom">
#foreach ($note->matieres as $matiere)
{{ $matiere->nom_matiere }}
#endforeach
</td>
I do however suspect that you have set your relationship up wrong. You've got the Note -> Matiere relationship set to belongsToMany, which means that you need a pivot table to contain the FK relationships. This would only be used if your Note is the child of many Matiere.
It does sound like you have your relationships set up wrong, but without really understanding what you're trying to do it's hard to tell you what exactly needs to be done.
Separate issue, but in your class update() method you've also got:
$note = Note::findOrFail($request->note_id);
$note = Matiere::findOrFail($request->note_id);
$note->update($request->all());
This means that you actually update the Matiere model no the Note model.

How to include a sidebar in Laravel to show on all pages

I want to show a sidebar that displays data from the database across all my pages in Laravel, but I keep getting this error:
Undefined variable: products (View:
C:\xampp\htdocs\shop\resources\views\pages\sidebar.blade.php) (View:
Sidebar
#extends('layouts.app')
<nav class=" d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<form>
<div class=" input-group">
<input class="form-control" type="text" placeholder="Search...">
<button type="submit" class="btn btn-primary" value="search">Search</button>
</div>
</form>
#foreach($products as $product)
<span id="catName">{{ $product->name }}</span>
<h2>No category to show</h2>
#endforeach
</div>
</nav>
Controller
public function index()
{
$product = Product::get();
return view('pages.sidebar', ['products' => $product]);
}
Route
Route::resource('sidebar','SidebarController');
app.blade.php
<div class="col-md-4 col-lg-4 col-sm-12">
#include('pages.sidebar')
</div>
You can either do #include() or your can return it via a controller, you cannot do both. I think you might be a little mixed up with the structure of your project.
If you are using #include it isn't going to hit a controller. So in theory, you would need to include ['products' => $products] on every controller method.
Here is how I would do it:
sidebar.blade.php:
<nav class=" d-none d-md-block bg-light sidebar">
<div class="sidebar-sticky">
<form>
<div class=" input-group">
<input class="form-control" type="text" placeholder="Search...">
<button type="submit" class="btn btn-primary" value="search">Search</button>
</div>
</form>
#foreach($products as $product)
<span id="catName">{{ $product->name }}</span>
<h2>No category to show</h2>
#endforeach
</div>
</nav>
app.blade.php:
<div class="col-md-4 col-lg-4 col-sm-12">
#include('pages.sidebar')
</div>
Create a new file (or use an existing one) for a home page inside the pages folder, we will call it home.blade.php
home.blade.php:
#extends('layouts.app')
Change the view you are returning in the controller to new home.blade.php file.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
public function index()
{
$product = Product::get();
return view('pages.home', ['products' => $product]);
}
}
I finally have to use view composer to solve this problem. Thank you

Call to a member function get() on null for a function in a model

I am trying to have a button of an employee show the text “Already booked” if the variable
count($bookingRequests) returns something and if nothing then show “Book Me’
I get the error above.
I have the following models and their relationships:
User.php and Bookings.php
User:
public function bookings() {
return $this->hasMany('App\Models\Bookings');
}
public function BookingRequest()
{
$this->bookings()->where('booking_status','=','confirmed')->get();
}
Bookings:
public function user(){
return $this->belongsTo('App\Models\User');
}
The view mentioned above is Employeeblock.blade.php:
<div class="row no-gutters mb-5 mb-lg-0" style="padding:10px">
#if(count($bookingRequests))
<a href="#" class="btn btn-primary" data-
toggle="modal" data-target=“#bookingModal">Already Booked</a>
#else
<a href="#" class="btn btn-primary" data-
toggle="modal" data-target="#bookingModal">Book Me</a>
#endif
</div>
Which has data passed onto it by a controller SearchController:
In the following way:
<?php
namespace App\Http\Controllers;
use DB;
use Auth;
use App\Models\Employee;
use App\Models\User;
use App\Models\Bookings;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Session;
.
.
.
$bookingRequests=Auth::user()->BookingRequest()->get();
return view(‘search.results’)
->with('bookingRequests',$bookingRequests)
results.blade.php above has the following html:
<div class="panel panel-default">
<div class="panel-heading"><h3></h3></div>
<div class="panel-body">
<div class="row">
<div class="col-lg-12">
#foreach($Employees as $Employee)
#include('user/partials/employeeblock')
#endforeach
</div>
</div>
</div>
</div>
</div>
Replace:
$bookingRequests=Auth::user()->BookingRequest()->get();
with
$bookingRequests=Auth::user()->BookingRequest();
Because your BookingRequest() method already use ->get() method to obtain data from db.

Cant Display my Reply under the Comment Laravel 5.7

I can't display my reply under each comment. Here is my code...
Comment model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function commentable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
Post model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'user_id', 'topic', 'body', 'category',
];
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
Comment controller
<?php
namespace App\Http\Controllers;
use App\Comment;
use App\Post;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class CommentController extends Controller
{
public function store(Request $request, $post)
{
$comment = new Comment;
$comment->body = $request->body;
$comment->user_id = Auth::user()->id;
$post = Post::find($post);
$post->comments()->save($comment);
return back();
}
public function replyStore(Request $request, $comment)
{
$comment = new Comment;
$comment->body = $request->body;
$comment->user_id = Auth::user()->id;
$comment = Comment::find($comment);
$comment->comments()->save($comment);
return back();
}
}
Routes
Route::post('/comment/store/{post}', 'CommentController#store')->name('comment.add');
Route::post('/reply/store/{commentid}', 'CommentController#replyStore')->name('reply.add');
View
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-12">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<div class="card">
<div class="card-header">{{$post->topic}}
Create New Post
</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<h3>{{$post->topic}}</h3>
<p>{{$post->body}}</p>
</div>
</div>
<form action="/comment/store/{{$post->id}}" method="post" class="mt-3">
#csrf
<div class="form-group">
<label for="">Comment :</label>
<textarea class="form-control" name="body" id="" rows="3"></textarea>
<br>
<input type="submit" value="Comment" class="btn btn-secondary">
</div>
</form>
<div class="row mt-5">
<div class="col-md-10 mx-auto">
<h6 style="border-bottom:1px solid #ccc;">Recent Comments</h6>
#foreach($post->comments as $comment)
<div class="col-md-12 bg-white shadow mt-3" style="padding:10px; border-radius:5px;">
<h4>{{$comment->user->name}}</h4>
<p>{{$comment->body}}</p>
<button type="submit" class="btn btn-link" onclick="toggleReply({{$comment->id}})">
Reply
</button>
<div class="row">
<div class="col-md-11 ml-auto">
{{-- #forelse ($replies as $repl)
<p>{{$repl->body}}</p>
#empty
#endforelse --}}
</div>
</div>
</div>
<form action="/reply/store/{{$comment->id}}" method="post"
class="mt-3 reply-form-{{$comment->id}} reply d-none">
#csrf
<div class="form-group">
<textarea class="form-control" name="body" id="" rows="3"></textarea>
<br>
<input type="submit" value="Reply" class="btn btn-secondary">
</div>
</form>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
#section('js')
<script>
function toggleReply(commentId) {
$('.reply-form-' + commentId).toggleClass('d-none');
}
</script>
#endsection
I have created the normal table with parent_id but I don't know how to display the replies for each comment. Please, anyone who can help me with this - I am stranded here and the error coming from the second controller function which is replystore() saying it doesn't recognize the comments() method. Please help me out to display the reply.

Resources