hello guys I have this set of database
clients(#id,raison_sociale,adresse,)<br>
delivery(#id,client_id(fk),date_del) <br>
details(delivery_id(fk),product_id(fk),qt)
product(id,code,desig,price)
and here are my eloquent relationships
class Client extends Model {
public function livraison (){
return $this->hasMany('App\Models\Livraison'); } }
class Detail extends Model
{
public function delivery (){
return $this->belongsTo('App\Models\Delivery');}}
class Livraison extends Model{
public function client (){
return $this->belongsTo('App\Models\Client');
}
public function detail (){
return $this->hasMany('App\Models\detail'); }}
Do I have something wrong with these relationships ?
I have a page where I show all of my delivery but I can't find the correct way to fetch this row:
raison_soc(from client) & date_del(from delivery); and details table (for each client of course )
use App\Models\Client;
Client::with('delivery.detail')->get();
I can't test your relationships but assuming you are following the naming conventions your relations should be fine. The above will grab all details that are associated to the delivery by the client.
You could improve your relationship with a hasManyThrough method Has Many Through
You would do something like this in your Client class:
public function details(){
return $this->hasManyThrough('App\Models`Details', 'App\Models\Livarasion');
}
If you explain your 2nd point more clearly I can help you adjust this. Please let me know if you have any issues
Related
i'm have a order able in DB that has related to product and service and other models, between these tables and models there is a many to many polymorph
i want to show client all his order
in future, maybe, the model change in name or amount
is there anyway to use namespace of model that is stored middle table instead specific method?
for exammple i dont want use
public function products()
{
return $this->morphedByMany(Product::class, 'orderable');
}
public function services()
{
return $this->morphedByMany(Service::class, 'orderable');
}
and etc
instead:
public function morhToAll(){
?????
}
and then for example i can:
$orderItems=auth()->user()->orders()->morhToAll
foreach(orderItems as $item){
.....
}
the usr can see all his orders and their details.
i want to show client his/her orders details
i'm currently doing a project for my homework, which i didn't managed to see something like this to fix my problem.
I have 2 models with Course and CourseLecture name. and i'm using getPathAttribute in my class base on a tutorial in internet.
in my Course Model i'm using getPathAttribute like this :
public function getPathAttribute()
{
return "/clientside/instructor/courses/$this->slug";
}
and in my CourseLecture Model :
public function getPathAttribute()
{
return "/clientside/instructor/courses/{course}/lectures/$this->slug";
}
I need to put my course slug to this getPathAttribute like :
http://url.com/clientside/instructor/courses/php/lectures/one
also my CourseLecture is using course_id and i've got a relationship between them which, they're belongsTo' andHasMany'. so how can i add course slug base on this structor in this path?
Also for this homework, i'm using Vue.js/Laravel. and it's a spa. i've tagged vue.js bcs if there's any solution to fix this via router, I will be happy to use it.
You can dynamically grab course slug from the course() relationship defined on the CourseLecture model. See below:
In CourseLecture model:
protected $appends = ['path'];
// relationship
public function course()
{
return $this->belongsTo(Course::class);
}
public function getPathAttribute()
{
return "/clientside/instructor/courses/{$this->course->slug}/lectures/$this->slug";
}
I am using laravel 5.7. I have now a situation where i have a Fight model with structure user_id, fightable_id
i have two other tables users and monsters. so users_id refers to users (a user can have many fights) and fightable_id can refer to either a user or a monster (monsters table). so I have to define the functions for the relation ship
for User model i have to do
1.for polymorphic one to many relationship
public function fights()
{
return $this->morphMany('App\Fight', 'fightable');
}
2.for simple one to many relationship
public function fights()
{
return $this->hasMany('App\Fight');
}
I am confused now. ofcourse the only way is to change the functions name. but i will be doing the correct thing by changing the function names right (as both the functions have same name). or is there anything I am doing wrong?
I'm not sure I understand your question completely but I'll try my best!
Also this post will really help you understand the problem you are facing I think,
Laravel morph relationship
Using the code from the post I linked but taking your tables would produce models defined like this.
User Model
class User extends Model
{
public function fights()
{
return $this->hasMany('App\Fight');
}
}
Fight Model
class Fight extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function fightable()
{
return $this->morphTo();
}
}
Monster Model
class Monster extends Model
{
public function fight()
{
return $this->morphOne('App\Fight', 'fightable');
}
}
If you still feel this has not answered your question or need some more help just let me know!
Not entirely clear on this yet as it would be the first time I'm tinkering with this. My table structure is as follows:
questions
- id
- type_id
multiple_choice_options
- id
- question_id
drag_and_drop_options
- id
- question_id
The type_id field on the questions table determines which options table to load from. So essentially I'd like to setup a relationship on the Question model as follows:
class Question extends Model {
public function options() {
// not sure what to return here?
}
}
And for the option models would this be the correct inverse definition?
class MultipleChoiceOption extends Model {
public function question() {
return $this->belongsTo(Question::class);
}
}
class DragAndDropOptions extends Model {
public function question() {
return $this->belongsTo(Question::class);
}
}
How do I set this up to work with polymorphic relationships?
You could simply build a switch in the options() relation:
class Question extends Model {
public function options() {
if ($type_id === 'multiple') {
return $this->hasMany(MultipleChoiceOption::class);
} else {
return $this->hasMany(DragAndDropOptions::class);
}
}
}
Careful though with these magic relations, you'll need additional type checks and careful coding in your app.
Check out the Laravel Docs on Polymorphism, there are some functions you could leverage with a slightly different data structure as such:
questions
- id
- optionable_id
- optionable_type
multiple_choice_options
- id
drag_and_drop_options
- id
Models:
class Question extends Model {
public function optionable() {
return $this->morphTo();
}
}
class MultipleChoiceOption extends Model {
public function question() {
return $this->morphMany('App\Question', 'optionable');
}
}
class DragAndDropOptions extends Model {
public function question() {
return $this->morphMany('App\Question', 'optionable');
}
}
Note, the optionable_type column will contain the class name of the owning model.
A side note, your data structure relationship logic seems a little strange to me but may still serve what you are trying to do. I would've probably had models Question, Option, QuestionType so that options are independent of question and question type. This would enable you to change the question type without having to change the options.
Considering what you are trying to achieve:
a)An option can only belong to a one question,
b)A question can have many options,
I think a cleaner solution might involve adjusting your database design,
questions
- id
- body
- question_type_id
question_type
- id
- name
options
- id
- name
- question_id
class question extends Model {
public function options() {
$this->hasMany('App\option','question_id')
}
}
class option extends Model {
public function question() {
return $this->belongsTo('App\question');
}
}
your question_type table would then contain muiltiple_choice,drag and drop, etc. therefore there would be no need for you to use an 'if' statement or 'switch', which makes your code cleaner. you also do not need to edit your code whenever you add a new question type.
I already read this piece from the laravel 5.1 documentation:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
I have no ideia where to write that.
And this is what I tried to write inside my Model:
class Professor extends Model
{
$professor = DB:table('professor')->orderBy('name','asc')->get();
}
Also tried:
class Professor extends Model
{
Professor::orderBy('name')->get();
//$professor = Professor::orderBy('name')->get();
}
Nothing works e.e
All of them give me erros like:
syntax error, unexpected '$professor' (T_VARIABLE), expecting function (T_FUNCTION)
The piece of code your trying to write should not be placed inside a Model. It should be in a controller or a repository if your using a Repository Pattern.
Assuming you got the following in your code.
A table called professors. A model Professor . A Controller called ProfessorsController.
And a route file with the following code get('professors','ProfessorsController#index');
Then on the controller you should have the following code.
class ProfessorsController extends Controller {
public function index()
{
$professors = \DB:table('professors')->orderBy('name','asc')->get();
return view('proffesors')->with('proffessors',$professors);
}
}
This will return an order list of professors to the view. That is if you are using a view to represent the data.
It does not need to be in model. Most of time something like that goes in controller.
In model you need to define relations or functions that would be used application wide on a object.
If you want to do something similar in model you won't use DB::table you need something like:
class Professor extends \Eloquent
{
public function professorsByName(){
$professionCollection = Professor::all()->orderBy('name','asc')->get();
return $professionCollection;
}
}
Please take it as example it's not something that should go in model at least this simple not.
Mental Note
Never use DB::table reason your observer if any won't execute.
I think you all are missing the primary issue with
class Professor extends Model
{
$professor = DB:table('professor')->orderBy('name','asc')->get();
}
This is not how PHP classes work.
OP, you need to brush up on the concepts of OOP.
What you need is something like this:
your controller:
public function myRoutedMethod()
{
$professors = Professor::getModel()->orderBy('name','asc')->get();
foreach($professors as $professor)
{
var_dump($professor->toArray();
}
}
Or probably even better, create a repository to interface with your model and just call $repository->professors()->toArray();
Google search: Laravel Repository