Created
February 8, 2020 16:28
-
-
Save Hammad964/8d1605a92773a70cbfe4b8822c1a4dbf to your computer and use it in GitHub Desktop.
Relationship problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Country extends Model | |
{ | |
protected $guarded = []; | |
public function posts() | |
{ | |
return $this->hasManyThrough(Post::class,UserProfile::class,'country_id','user_id','id','user_id'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use App\User; | |
use App\UserProfile; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
class Post extends Model | |
{ | |
// | |
// public function data() | |
// { | |
// return[ | |
// // ['name' => 'Abc', | |
// // 'company' => 'com'] | |
// // [ | |
// // 'name' => 'xyz', | |
// // 'company' => 'mytc' | |
// // ] | |
// ]; | |
// } | |
use SoftDeletes; | |
// protected $table = 'post'; | |
protected $guard = ['user_id']; | |
public function user() | |
{ | |
return $this->belongsTo(User::class,'user_id','id'); | |
} | |
public function profile() | |
{ | |
return $this->hasOneThrough(UserProfile::class,User::class,'id','user_id','user_id'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Contracts\Auth\MustVerifyEmail; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Database\Eloquent\Builder; | |
// use App\Scopes\VerifiedUsers; | |
// use App\Scopes\NotVerifiedUsers; | |
use App\UserProfile; | |
use App\Post; | |
class User extends Authenticatable | |
{ | |
use Notifiable; | |
// protected $table = 'user'; | |
// protected $primaryKey = 'email'; | |
// protected $keyType = 'string'; | |
// public $incrementing = false; | |
/** | |
* The name of the "created at" column. | |
* | |
* @var string | |
*/ | |
const CREATED_AT = 'created_at'; | |
/** | |
* The name of the "updated at" column. | |
* | |
* @var string | |
*/ | |
const UPDATED_AT = 'updated_at'; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'name', 'email', 'password', | |
]; | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', 'remember_token','email' | |
]; | |
/** | |
* The attributes that should be cast to native types. | |
* | |
* @var array | |
*/ | |
protected $casts = [ | |
'email_verified_at' => 'datetime', | |
]; | |
// public static function boot(){ | |
// parent::boot(); | |
// static::addGlobalScope('vfu',function(Builder $builder){ | |
// return $builder->where('email_verified_at',"<>",null); | |
// }); | |
// public static function boot() | |
// { | |
// parent::boot(); | |
// static::addGlobalScope(new VerifiedUsers); | |
// static::addGlobalScope(new NotVerifiedUsers); | |
// static::addGlobalScope('nvfu',function(Builder $builder){ | |
// return $builder->where('email_verified_at','=',null); | |
// // App\User::withoutGlobalScope('nvfu')->get(); | |
// }); | |
public function scopeVfu($query){ | |
return $query->where('email_verified_at','<>',null); | |
} | |
public function scopeNvfu($query){ | |
return $query->where('email_verified_at','=',null); | |
} | |
public function scopeFindById($query,$id) | |
{ | |
return $query->where('id',$id); | |
} | |
public function profile() | |
{ | |
return $this->hasOne(UserProfile::class,'user_id','id'); | |
} | |
public function posts() | |
{ | |
return $this->hasMany(Post::class,'user_id','id'); | |
} | |
public function roles() | |
{ | |
return $this->belongsToMany(Role::class,'role_user','user_id','role_id','id'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
use App\User; | |
class UserProfile extends Model | |
{ | |
protected $guard = []; | |
public function user() | |
{ | |
return $this->belongsTo(User::class,'user_id','id'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<title>Laravel</title> | |
<link rel="stylesheet" type="text/css" href="{{asset('css/app.css')}}"> | |
<!-- Fonts --> | |
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet"> | |
<!-- Styles --> | |
<style> | |
html, body { | |
background-color: #fff; | |
color: #636b6f; | |
font-family: 'Nunito', sans-serif; | |
font-weight: 200; | |
height: 100vh; | |
margin: 0; | |
} | |
.full-height { | |
height: 100vh; | |
} | |
.flex-center { | |
align-items: center; | |
display: flex; | |
justify-content: center; | |
} | |
.position-ref { | |
position: relative; | |
} | |
.top-right { | |
position: absolute; | |
right: 10px; | |
top: 18px; | |
} | |
.content { | |
text-align: center; | |
} | |
.title { | |
font-size: 84px; | |
} | |
.links > a { | |
color: #636b6f; | |
padding: 0 25px; | |
font-size: 13px; | |
font-weight: 600; | |
letter-spacing: .1rem; | |
text-decoration: none; | |
text-transform: uppercase; | |
} | |
.m-b-md { | |
margin-bottom: 30px; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="flex-center position-ref full-height"> | |
@if (Route::has('login')) | |
<div class="top-right links"> | |
@auth | |
<a href="{{ url('/home') }}">Home</a> | |
@else | |
<a href="{{ route('login') }}">Login</a> | |
@if (Route::has('register')) | |
<a href="{{ route('register') }}">Register</a> | |
@endif | |
@endauth | |
</div> | |
@endif | |
<div class="content"> | |
<!-- @php | |
$num = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; | |
$count = 0; | |
@endphp | |
@foreach ($num as $n) | |
{{-- @if($loop->iteration % 4 == 0) --}} | |
@if($loop->odd) | |
<span style="color: red">{{$n}}</span> | |
@endif | |
@php | |
$count = $loop->count; | |
@endphp | |
@endforeach | |
{{$count}} | |
--> | |
<!-- @include('partials.message',['message' => 'laravel 6 msg']) --> | |
<table class="table table-hover"> | |
<thead> | |
<tr> | |
<th>Name</th> | |
<th>Email</th> | |
<th>Address</th> | |
<th>Country</th> | |
</tr> | |
</thead> | |
<tbody> | |
@foreach($users as $user) | |
<tr> | |
<td>{{$user->name}}</td> | |
<td>{{$user->email}}</td> | |
<td>{{$user->profile->address}}</td> | |
<td>{{$user->profile->country->name}}</td> | |
</tr> | |
@endforeach | |
</tbody> | |
</table> | |
<div class="links"> | |
<a href="https://laravel.com/docs">Docs</a> | |
<a href="https://laracasts.com">Laracasts</a> | |
<a href="https://laravel-news.com">News</a> | |
<a href="https://blog.laravel.com">Blog</a> | |
<a href="https://nova.laravel.com">Nova</a> | |
<a href="https://forge.laravel.com">Forge</a> | |
<a href="https://vapor.laravel.com">Vapor</a> | |
<a href="https://github.com/laravel/laravel">GitHub</a> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Post; | |
use DB; | |
class WelcomeController extends Controller | |
{ | |
// | |
// public function welcome() | |
// { | |
// $posts = new Post; | |
// $data = $posts->data(); | |
// return view('welcome', compact('data')); | |
// } | |
// public function welcome() | |
// { | |
// $post = Post::data(); | |
// $data['data'] = $post; | |
// return view('welcome', $data); | |
// } | |
public function welcome() | |
{ | |
// $data = DB::table('profile')->get(); | |
// $data = DB::table('profile')->first(); | |
// $data = DB::table('profile')->where('id',1)->get(); | |
// $data = DB::table('profile')->where('id',">",1)->get(); | |
// $data = DB::table('profile')->where('id',">",1)->value('name'); | |
// $data = DB::table('profile')->value('name'); | |
// $data = DB::table('profile')->pluck('name'); | |
// $data = DB::table('profile')->select('name')->get(); | |
// $data = DB::table('profile')->count(); | |
// $data = DB::table('profile')->where('id',5)->exists(); | |
// $query = DB::table('profile')->select('name'); | |
// $data = $user = $query->addselect('country')->get(); | |
// foreach ($data as $d) { | |
// echo $d->name; | |
// } | |
// dd($data); | |
// DB::table('profile')->select(DB::raw(" count(*) as staff "))->where('role','staff')->groupby('role')->first(); | |
// DB::enableQueryLog(); | |
// DB::table('profile')->select(DB::raw(" count(*) as staff "))->where('role','staff')->groupby('role')->first(); | |
// DB::getQueryLog(); | |
// DB::table('profile')->select(DB::raw(" count(*) as staff "))->groupby('role')->first(); | |
// DB::table('profile')->selectRaw("count(*) as staff")->where('role','staff')->first(); | |
// DB::table('profile')->whereRaw('`role` = ?',['staff'])->get(); | |
// DB::table('users')->unionAll(DB::table('users'))->get(); | |
// DB::table('users')->where('id',1)->union(DB::table('users')->where('id',1))->get(); | |
// DB::table('profile')->where([ | |
// ['role','staff'], | |
// ['id',4] | |
// ])->get(); | |
// DB::table('profile')->where('id',4)->orwhere('role','staff')->get(); | |
// DB::table('profile')->orderby('id','desc')->get(); | |
// DB::table('profile')->inRandomOrder()->first(); | |
// DB::table('users')->insert(['email'=> | |
// 'xyz@gmail.com']) | |
// DB::table('users')->insertOrIgnore(['email'=>'abc@gmail.com']) | |
// DB::table('users')->insertGetId(['email'=>'abc@gmail.com']) | |
// DB::table('users')->where('id',6)->update(['email'=>'myemail@email.com']) | |
// DB::table('users')->get(); | |
// DB::table('users')->updateOrInsert(['id'=>7],['email'=>'abx@gmail.com']); | |
// DB::table('orders')->where('id',1)->increment('price',100) | |
// DB::table('orders')->where('id',2)->delete() | |
// return view('welcome',compact('data')); | |
$users = \App\User::with('profile.country:id,name')->get(); | |
return view('welcome',compact('users')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
problem is in your user_profiles table where you may have 1 or more country_id columns with 0 just change them in db with any existing countr_id which exists in countries table, and your problem will be solved