I keep getting the file on null error and can't seem to find why he can't locate the file?
#foreach($users as $user)
<tr>
<td>
<img height="62" src="{{asset($user->userdetail->file)}}" alt="">
</td>
</tr>
#endforeach
Userdetail Model
class UserDetail extends Model
{
use HasFactory;
protected $fillable = ['file'];
protected $uploads = '/img/avatar/';
public function getFileAttribute($userdetail){
return $this->uploads . $userdetail;
}
}
user model
protected $fillable = [
'name',
'is_active',
'email',
'photo_id',
'password',
];
public function userdetail(){
return $this->belongsTo(UserDetail::class);
}
Location of file
Migration userdetails
public function up()
{
Schema::create('user_details', function (Blueprint $table) {
$table->id();
$table->string('file');
$table->timestamps();
});
}
user
ddump of $user
You have to create the relationship for photo.
make this function in user model and place the name of the correct model
public function photo(){ return $this->hasOne(UserDetails::class, 'id', 'photo_id'); }
The problem was not the relation.
public function photo(){
return $this->belongsTo(UserDetail::class);
}
my problem was the name of the function i named it userdetail and then it will look for userdetail_id instead i needed it to name photo because then it will look for photo_id
Now it works!
Related
Ticket.php
Protected $table = 'tickets';
Protected $fillable = ['user_id','kategori_id','judul','prioritas_id','status_id','message','file_tambahan'];
public function user()
{
return $this->belongsTo(User::class);
}
User.php
protected $table = 'users';
protected $fillable = [
'avatar','nama_lengkap', 'email', 'password',
];
public function getAvatar()
{
if(!$this->avatar)
{
return asset('images/default.png');
}
return asset('images/'.$this->avatar);
}
public function ticket()
{
return $this->hasMany(Ticket::class);
}
TicketController.php
use App\Model\Ticket;
use App\Model\User;
use App\Model\Kategori;
use App\Model\Prioritas;
use App\Model\Status;
class TicketController extends Controller
{
public function index()
{
$data = Ticket::with('User','Kategori','Prioritas','Status')->get();
return view('admin.crud_ticket.index', compact('data'));
}
index.blade.php
#foreach($data as $d)
<tr role="row" class="odd">
<td>{{ $d->user->nama_lengkap }}</td>
</tr>
#endforeach
{{ $data->links('vendor.pagination.custom') }}
I keep getting the same error over and over again, I do relationships between tables
namely in the user table, category, priority, status.
and cause an error like this, if you have a solution please help me
Method Illuminate\Database\Eloquent\Collection::links does not exist. (View: C:\xampp\htdocs\Helpdesk\resources\views\admin\crud_ticket\index.blade.php)
Right now in the User Dashboard view (home.blade.php) it wrongly fetches the comments count of the first post by the user.
I would like to display the correct comments count of a post next to the posts' title.
What am I missing/doing wrong?
View (home.blade.php):
#if(count($posts) > 0)
<h5>Your Posts ({{ count($posts) }})</h5><hr>
#foreach($posts as $post)
<h6> {{ $post->title }}
{{ $comments_count }} comments </h6><hr>
#endforeach
#endif
Controller:
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
$post = Post::with('commentsCount')->first();
$comments_count = $post->commentsCount->first()->aggregate;
return view('home')->with('posts', $user->posts)
->with('comments_count', $comments_count);
}
}
Models (User, Post, Comment):
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function posts()
{
return $this->hasMany('App\Post');
}
}
class Post extends Model
{
protected $table = 'posts';
public $primaryKey = 'id';
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
public function commentsCount()
{
return $this->comments()
->selectRaw('post_id, count(*) as aggregate')
->groupBy('post_id');
}
}
class Comment extends Model
{
protected $table = 'comments';
public $primaryKey = 'id';
public function post()
{
return $this->belongsTo('App\Post');
}
}
Migrations (CreatePostsTable, CreateCommentsTable):
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->string('title');
$table->mediumText('body');
$table->timestamps();
});
}
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('author');
$table->integer('post_id');
$table->integer('user_id');
$table->mediumText('comment');
$table->timestamps();
});
}
Your Post model hasMany comments, so you can able to use count() method, like : $post->comments->count() :
#if(count($posts) > 0)
<h5>Your Posts ({{ count($posts) }})</h5><hr>
#foreach($posts as $post)
<h6> {{ $post->title }}
{{ $post->comments->count() }} comments
</h6>
<hr>
#endforeach
#endif
I am trying to fetch digitizing order related to the user but unfortunately, I am facing an error.
Please see this error: https://flareapp.io/share/VmeWJ47Q
Controller
public function index()
{
$data=
[
'digitizings'=>Digitizing::with('user')->paginate(8)
];
return view('front_end.profile.digitizing.digitizing_view_order',$data);
}
User Model
class User extends Authenticatable
{
use Notifiable;
public function digitizing()
{
return $this->hasMany('App\Digitizing','user_id');
}
}
Digitizing Model
class Digitizing extends Model
{
protected $fillable = ['id','order_name','height','width','urgent','image',
'order_placement','required_format','order_fabric','instruction','user_id'];
protected $table ="digitizing_orders";
public function user()
{
return $this->belongsTo('App\User');
}
}
HTML view
#foreach($digitizings as $key =>$digitizing)
<tr>
<td>DPO# {{$digitizing->id}}</td>
<td>{{$digitizing->created_at}}</td>
<td>{{$digitizing->order_name}}</td>
<td>{{$digitizing->user->first_name}}</td>
<td>{{$digitizing->user->email}}</td>
<td>{{$digitizing->released_date ?? 'processing'}}</td>
<td>View
</td>
</tr>
#endforeach
Does every Digitizing-Entry has set a valid user_id in database? Try checking eager loaded data is set before accessing it.
Try to send the data to the view like this:
public function index()
{
$digitizings = Digitizing::with('user')->paginate(8);
return view('front_end.profile.digitizing.digitizing_view_order',$digitizings);
}
I am beginner to Laravel and now I am doing this project but I am stuck in this point for a couple days. Here is what I wish do, I would like to display the day and timeslot labels selected by the tutor!
here is my tables and their relationships
https://imgur.com/WMMwhHK
Day Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Day extends Model
{
protected $guarded = ['id', '_token'];
}
Timeslot Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Timeslot extends Model
{
protected $guarded = ['id', '_token'];
public function Tutor()
{
return $this->hasMany(Tutor::class);
}
}
Tutor_availability Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tutor_availability extends Model
{
protected $fillable = ['tutor_id', 'timeslot_id', 'day_id'];
protected $guarded = ['id', '_token'];
public function tutor()
{
return $this->belongsTo('App\Tutor');
}
public function day()
{
return $this->belongsTo('App\Day');
}
public function timeslot()
{
return $this->belongsTo('App\Timeslot');
}
}
tutor_availabilities Migration
public function up()
{
Schema::create('tutor_availabilities', function (Blueprint $table) {
// $table->bigIncrements('id');
$table->integer('day_id')->unsigned();
$table->foreign('day_id')->references('id')->on('days')->onDelete('cascade');
$table->integer('timeslot_id')->unsigned();
$table->foreign('timeslot_id')->references('id')->on('timeslots')->onDelete('cascade');
$table->integer('tutor_id')->unsigned();
$table->foreign('tutor_id')->references('id')->on('tutors')->onDelete('cascade');
$table->timestamps();
$table->primary(['day_id', 'timeslot_id', 'tutor_id']);
});
}
TutorController
public function index()
{
$tutors = Tutor::latest()->paginate(5);
$tutor_availabilities = Tutor_availability::all();
return view('tutors.index', [
'tutors' => $tutors,
'tutor_availabilities' => $tutor_availabilities
]);
}
View
<tr>
<th>Days</th> <th> Timings</th>
</tr>
<tr>
<td>
{{$tutor_availabilities->day->label}}
</td>
<td>
{{$tutor_availabilities->timeslot->label}}
</td>
</tr>
Put this in your Tutor_availability model
public function day()
{
return $this->belongsTo(Day::class);
}
public function timeslot()
{
return $this->belongsTo(Timeslot:class);
}
Then in controller
public function index()
{
$tutor_availabilities = Tutor_availability::all();
return view('tutors.index',
compact('tutor_availabilities')) ;
}
Then in view
<table>
#foreach ($tutor_availabilities as $tutor_available)
<tr><td>
{{$tutor_available->day->label}}
</td>
<td>
{{$tutor_available->timeslot->label}}
</td></tr>
#endforeach
</table>
I have three tables.
I want to add a oyuncu (player) to a grup (group), so I created a method (public function oyuncustore(Request $request, $grup_id)).
But I get an error and I can't add rows to the pivot table.
Error:
ErrorException in BelongsToMany.php line 866: Argument 1 passed to
Illuminate\Database\Eloquent\Relations\BelongsToMany::formatSyncList()
must be of the type array, string given, called in
........./vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
on line 831 and defined
Grup.php
class Grup extends Model
{
public function turnuva(){
return $this->belongsTo('App\Turnuva');
}
public function oyuncular(){
return $this->belongsToMany('App\Oyuncu');
}
}
Turnuva.php
class Turnuva extends Model
{
protected $table ='turnuvas';
public function grups(){
return $this->hasMany('App\Grup');
}
}
Oyuncu.php
class Oyuncu extends Model
{
protected $table ='oyuncular';
//$fillable = ['grup_id', 'oyuncu_id'];
public function grups(){
return $this->belongstoMany('App\Grup');
}
}
pivot table : grup_oyuncu created like that:
class CreateGrupOyuncuTable extends Migration
{
public function up()
{
Schema::create('grup_oyuncu', function (Blueprint $table) {
$table->increments('id');
$table->integer('oyuncu_id')->unsigned();
$table->foreign('oyuncu_id')->references('id')->on('oyuncular')->onDelete('CASCADE')->onUpdate('CASCADE');
$table->integer('grup_id')->unsigned();
$table->foreign('grup_id')->references('id')->on('grups')->onDelete('CASCADE')->onUpdate('CASCADE');
$table->engine = 'InnoDB';
});
}
public function down()
{
Schema::table('grup_oyuncu', function (Blueprint $table) {
//$table->dropForeign(['user_id']);
$table->dropForeign('grup_oyuncu_grup_id_foreign');
$table->dropForeign('grup_oyuncu_oyuncu_id_foreign');
});
Schema::drop('grup_oyuncu');
}
}
GrupController :
public function oyuncustore(Request $request, $grup_id)
{
$gr = new Grup;
$tumoyuncular = Oyuncu::All();
$grup= Grup::find($grup_id);
$gr->oyuncular()->sync($request->oyuncu, false);
$grpoyuncular = DB::table('grup_oyuncu')->select('oyuncu_id')->get();
Session::flash('success','Gruba oyuncu eklendi.');
return view('gruplar.show')->withGrup($grup)->withTumoyuncular($tumoyuncular)->withGrpoyuncular($grpoyuncular);
}
Form :
{!! Form::open(['route'=>['gruplar.oyuncustore',$grup->id],'method'=>'POST'])!!}
<h2>Yeni Oyuncu Ekle</h2>
{{Form::label('oyuncu','Oyuncu Adı Soyadı:')}}
<select class="form-control" name="oyuncu" id="">
#foreach($tumoyuncular as $toyuncu)
<option value="{{ $toyuncu->id}} ">{{$toyuncu->ad}} {{$toyuncu->soyad}}</option>
#endforeach
</select>
{{Form::submit('Oyuncu Ekle', array('class'=>'btn btn-success btn-lg btn-block', 'style'=>'margin-top:20px;'))}}
{!! Form::close()!!}
Maybe I created relationship with tables wrong?