how to save data in multiple tables with the one form - laravel

I have 2 tables: customers and address one form to save data into this tables. For this, I made the relation like:
Address model
public function customer()
{
return $this->belongsTo(Customer::class);
}
customer model
public function address()
{
return $this->hasMany(Address::class);
}
How can i save the address in table? I already done with customer i can save, update and delete. I read the DOC of Eloquent of laravel, but i don't really get it.
Here is my controller
class CustomerController extends Controller
{
public function index()
{
// $customer = Customer::all();
$customer = Customer::with('address')->get();;
return view('customer.index', compact('customer'));
}
public function create(Address $address)
{
$address = $address->all();
return view('customer.create');
}
public function store(CustomerRequest $request , Customer $customer)
{
$customer = Customer::create($request->all());
return redirect()->route('customer.index',compact('customer'));
}
public function show(Customer $customer)
{
return view('customer.show',compact('customer'));
}
public function edit(Customer $customer)
{
return view('customer.edit', compact('customer'));
}
public function update(CustomerRequest $request, $id)
{
$customer = Customer::find($id)->update($request->all());
return redirect()->route('customer.index',compact('customer'));
}
public function destroy($id)
{
Customer::destroy($id);
return redirect()->route('customer.index');
}
}
Any suggestions would be appreciated!!

Related

How would I edit the Laravel Nova indexQuery for a Resource to only show records through multiple relationships?

firstly thank you in advance.
I have the following Models , User , Location, Listing, Offer.
The relationships are:
User Model:
public function location()
{
return $this->hasOne(Location::class);
}
Location Model:
public function listings()
{
return $this->hasMany(Listing::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
Listing Model:
public function offers()
{
return $this->hasMany(Offer::class);
}
public function location()
{
return $this->belongsTo(Location::class);
}
Offer Model:
public function listing()
{
return $this->belongsTo(Listing::class);
}
In my Offer Resourse I would like to show only the offers that belongTo the listings that in turn belongsTo the location that in turn belongTo the authenticated user. I have tried the following.
public static function indexQuery(NovaRequest $request, $query)
{
$user = Auth::user();
if ($user->is_admin === 1){
return $query;
}elseif($user->is_admin === 0) {
return $user->location->listings->offers;
}
}
But get an error Property [offers] does not exist on this collection instance. Any assistance will be greatly appreciated.
I would try something like this.
More information here on querying relationship existence:
https://laravel.com/docs/9.x/eloquent-relationships#querying-relationship-existence
public static function indexQuery(NovaRequest $request, $query) {
$user = Auth::user();
if (!$user->is_admin) {
$query = $query->whereHas('listings.location.user', function ($builder) use ($user) {
return $builder->where('id', $user->id);
});
}
return $query;
}
The reason you are getting the "Property does not exist" error is due to you not pulling it from database.
Two alternative solutions include
Querying them separately: ...->listings()->offers()
Eager loading: ->with(['location.listings.offers']);

How can i store more than one product_ticket?

Im trying to store more than one product with my controller. In my function store first i generate a ticket and then i generate a product_ticket with the ticket_id recently generated and the product_id from the selected product from the form. But how can i do to store more than one if i select more than one product in the form.
This are my relationships:
Product.php:
class Product extends Model
{
public function tickets()
{
return $this->belongsToMany(Ticket::class);
}
public function productXticket()
{
return $this->hasMany(ProductXTicket::class);
}
}
Ticket.php
class Ticket extends Model
{
public function productXticket()
{
return $this->hasMany(ProductXTicket::class);
}
public function products()
{
return $this->belongsToMany(Product::class);
}
}
ProductXTicket:
class ProductXTicket extends Model
{
protected $table = 'product_ticket';
public function ticket_id(){
return $this->belongsTo(Ticket::class);
}
public function product_id(){
return $this->belongsTo(Product::class);
}
}
product_ticket migration:
Schema::create('product_ticket', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained();
$table->foreignId('ticket_id')->constrained();
$table->string('serial_number');
$table->integer('quantity');
$table->timestamps();
});
}
TicketController:
public function store(Request $request){
/*dd($request->all());*/
$ticket = new Ticket();
/*$ticket->cuenta_id = $request->cuenta_id;*/
$ticket->contact_id = $request->contact_id;
$ticket->statusTicket_id = $request->statusTicket_id;
$ticket->typeTicket_id = $request->typeTicket_id;
$ticket->idOwner = Auth::user()->id;
$ticket->save();
$productXticket = new ProductXTicket();
$productXticket->ticket_id = $ticket->id;
$productXticket->serial_number = $request->serial_number;
$productXticket->quantity = $request->quantity;
$ticket->productXticket()->save($productXticket);
Session::flash('success');
return redirect()->route('tickets.view');
}
So here im storing one ticket and one product_ticket. I want form each ticket to store the same amount of product_ticket as the amount products selected
Instead of:
$ticket->productXticket()->save($productXticket);
Use:
$ticket->productXticket()->saveMany([$productXticket,...]);

How to get multiple relationships Data in My Laravel Vue Project

I have a 3 model "User","Review","ReviewReplay"
in user Model
public function reviews()
{
return $this->hasMany('App\Models\Review');
}
public function reviewReplys()
{
return $this->hasMany('App\Models\ReviewReply');
}
In Review Model
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function reviewReplys()
{
return $this->hasMany('App\Models\ReviewReply');
}
in Review Replay Model
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function review()
{
return $this->belongsTo('App\Models\Reviews');
}
in My Controller
public function getReview($id)
{
$review= Review::where('product_id',$id)->with('user','reviewReplys')->paginate(10);
return response()->json($review);
}
Now here I run Review foreach loop and here I need review.reviewReplay.user information. How I Get The Review Replay User information inside Review Foreach loop.
You can do it as below.
$review= Review::where('product_id',$id)->with(['reviewReplys.user'])->paginate(10);
with(['reviewReplys.user']) --> it will make connection between Review model to reviewReplys model +
reviewReplys model to User model

simple One to many relationship laravel

i have a user(ADMIN) and a student i want them to be in one table that is on the dashboard controller i cant find and exact answer in the searching please help me
Student model
public function user()
{
return $this->belongsTo("App\User", 'User_id', 'id');
}
User model
public function student()
{
return $this->hasMany("App\Student");
}
DashboardController
$users = User::with('user_id');
return view('superadminpage.admin_table')->with('users', $users);
Edit Student model code like this:
public function user(){
return $this->belongsTo("App\User");
}
Also edit User model:
public function students(){
return $this->hasMany("App\Student");
}
then use in DashboardController
$users = User::query()->with("students")->all();
return view('superadminpage.admin_table')->with('users', $users );
then you can do this:
foreach($users as $user){
$students = $user->students;
// use
}
first change the User_id to user_id at student model
public function user()
{
return $this->belongsTo("App\User", 'user_id');
}
second change at DashboardController
$users = User::with('user_id'); to $users = User::with('student')->all();
first change the User_id to user_id at student model
public function user()
{
return $this->belongsTo("App\User", 'user_id' , 'id');
}
second change at DashboardController
$users = User::with('user_id'); to $users = User::with('student')->all();
it's also good to change at user model public function student() to public function students() and call at DashboardController $users = User::with('students')->all();
public function users()
{
return $this->hasMany('App\User');
}
public function role()
{
return $this->belongsTo('App\Role');
}

Multiple Eager Loading With Pagination in laravel

I want to return paginated data of the eager loaded relations so as I can display them using pagination in my view template
public function show(Group $group)
{
$data = ['members', 'assets', 'liabilities', 'loans','shares'];
$group = $group->load($data);
return view('admin.groups.show', compact('group'));
}
This is my Group Model
<?php
namespace App;
class Group extends BaseModel
{
use Enumable;
public function constitution()
{
return $this->hasOne(Constitution::class);
}
public function members()
{
return $this->hasMany(Member::class);
}
public function assets()
{
return $this->hasManyThrough(Asset::class, Meeting::class)
->orderByDesc('meeting_id')
->orderBy('member_id');
}
public function loans()
{
return $this->hasManyThrough(Loan::class, Member::class)
->orderByDesc('meeting_id')
->orderBy('member_id');
}
public function shares()
{
return $this->hasManyThrough(Share::class, Member::class)
->orderByDesc('meeting_id')
->orderBy('member_id');
}
public function liabilities()
{
return $this->hasOne(Liability::class)->latest();
}
}
I need to paginate $data array
Use setRelations():
public function show(Group $group)
{
$group->setRelations([
'members', $group->members()->paginate(5),
'assets', $group->assets()->paginate(5),
'liability', $group->liability()->paginate(5),
'loans', $group->loans()->paginate(5)
]);
return view('admin.groups.show', compact('group'));
}

Resources