Laravel/PHP কোডের explain bangla step by step > (2) Flowchart > (3) diagram image



Main code

(purchase

controller)


public function purchaseWiseDetail(Request $request){

$users = Auth::user();

$invoice_id = $request->invoice_id;

$key = $request->_attr_key;

$data = Purchase::with(['_master_details','purchase_account'])->where('id',$invoice_id)->first();

$form_settings = PurchaseFormSettings::first();

$permited_branch = permited_branch(explode(',',$users->branch_ids));

$permited_costcenters = permited_costcenters(explode(',',$users->cost_center_ids));

$store_houses = permited_stores(explode(',',$users->store_ids));


return view('backend.purchase.detail',compact('data','permited_branch','permited_costcenters','store_houses','key','form_settings'));

}



public function purchaseWiseDetail(Request $request){


এটি একটি Laravel Controller method যার নাম purchaseWiseDetail।

এটি HTTP request গ্রহণ করে $request আকারে।

অর্থাৎ, যখন কোন ইউজার কোন purchase detail দেখতে চায়, এই function call হবে।






$users = Auth::user();

এখানে Auth::user() ব্যবহার করে বর্তমানে লগইন করা ইউজারের তথ্য $users ভেরিয়েবলে রাখা হয়েছে।

এই $users ব্যবহার করে আমরা তার branch, store, cost center ইত্যাদি চেক করতে পারি।




$invoice_id = $request->invoice_id;

$key = $request->_attr_key;



$data = Purchase::with(['_master_details','purchase_account'])->where('id',$invoice_id)->first();


এখানে Eloquent ORM ব্যবহার করা হয়েছে।

Purchase::with(['_master_details','purchase_account']) → Purchase মডেলের সাথে সম্পর্কিত _master_details এবং purchase_account data eager load করবে।

where('id', $invoice_id)->first() → Purchase table থেকে id মিলিয়ে একটাই row নিয়ে আসবে।

অর্থাৎ, $data হলো একটি purchase invoice এর সম্পূর্ণ বিস্তারিত।



$form_settings = PurchaseFormSettings::first();

PurchaseFormSettings table থেকে প্রথম row নিয়ে এসেছে।

সম্ভবত purchase form এর configurations বা settings view এ দেখানোর জন্য।




$permited_branch = permited_branch(explode(',',$users->branch_ids));

$permited_costcenters = permited_costcenters(explode(',',$users->cost_center_ids));


ইউজারের branch_ids এবং cost_center_ids comma-separated string হিসেবে থাকে।

explode(',', $users->branch_ids) → string কে array তে রূপান্তর করে।

তারপর permited_branch() এবং permited_costcenters() function call করে শুধুমাত্র ইউজারের allowed branches ও cost centers filter করা হয়েছে।



$store_houses = permited_stores(explode(',',$users->store_ids));

একইভাবে, ইউজারের store_ids থেকে শুধুমাত্র allowed store list filter করা হয়েছে।

view এ এই $store_houses দেখানো হবে।


return view('backend.purchase.detail',compact('data','permited_branch','permited_costcenters','store_houses','key','form_settings'));


শেষে backend.purchase.detail view এ উপরের সব ভেরিয়েবল পাঠানো হয়েছে।

compact() ব্যবহার করলে ভেরিয়েবল নাম অনুযায়ী view এ পাঠানো হয়।

অর্থাৎ, view এ $data, $permited_branch, $permited_costcenters, $store_houses, $key, $form_settings ব্যবহার করা যাবে।




সারসংক্ষেপে:

  1. লগইন করা ইউজারকে চেক করে।

  2. request থেকে invoice_id ও key নেয়।

  3. Purchase invoice এর বিস্তারিত data, সম্পর্কিত master details ও purchase account fetch করে।

  4. Purchase form settings load করে।

  5. ইউজারের allowed branch, cost center ও store filter করে।

  6. সব data view এ পাঠিয়ে purchase detail page render করে।








Start

│

▼

Receive HTTP Request with:

├─ invoice_id

└─ _attr_key

│

▼

Get Logged-in User

│

▼

Fetch Purchase Data:

Purchase::with(['_master_details','purchase_account'])

where id = invoice_id

│

▼

Fetch Purchase Form Settings:

PurchaseFormSettings::first()

│

▼

Filter Permitted Branches:

permited_branch(explode(',', $users->branch_ids))

│

▼

Filter Permitted Cost Centers:

permited_costcenters(explode(',', $users->cost_center_ids))

│

▼

Filter Permitted Stores:

permited_stores(explode(',', $users->store_ids))

│

▼

Return View:

backend.purchase.detail

with data:

├─ $data

├─ $permited_branch

├─ $permited_costcenters

├─ $store_houses

├─ $key

└─ $form_settings

│

▼

End






Compact function:



Laravel এ compact() একটি খুব গুরুত্বপূর্ণ helper function, যা variable কে array আকারে convert করে view এ পাঠানোর জন্য ব্যবহার করা হয়।


return view('backend.purchase.detail',

compact('data','permited_branch','permited_costcenters','store_houses','key','form_settings')

);


return view('backend.purchase.detail', [

'data' => $data,

'permited_branch' => $permited_branch,

'permited_costcenters' => $permited_costcenters,

'store_houses' => $store_houses,

'key' => $key,

'form_settings' => $form_settings

]);


Example: $name = "Fakhrul";

$age = 25;


$data = compact('name','age');


[

"name" => "Fakhrul",

"age" => 25

]







::With

Laravel এ ::with() খুব গুরুত্বপূর্ণ একটি method, বিশেষ করে যখন তুমি Eloquent relationship নিয়ে কাজ করো।

with() কেন ব্যবহার করি?

👉 with() ব্যবহার করা হয় Eager Loading এর জন্য
👉
অর্থাৎ, related table/data একসাথে load করার জন্য



$data = Purchase::with(['_master_details','purchase_account'])
->where('id',$invoice_id)
->first();



Purchase::

👉 এটা তোমার Purchase Model
👉
এটা purchase table কে represent করে



with(['_master_details','purchase_account'])

👉 এখানে ২টা relationship load হচ্ছে:

Relation Name

Actual Table

_master_details

purchase_details

purchase_account

purchase_accounts

মানে:

  • purchase এর সাথে related সব details (items)

  • purchase এর সাথে related account info



first()

👉 matching data থেকে ১টা row return করবে
👉 object
আকারে $data তে রাখবে











$data = Purchase::with(['_master_details','purchase_account'])
->where('id',$invoice_id)
->first();




Purchase::

👉 এটা তোমার Purchase Model
👉
এটা purchase table কে represent করে

with(['_master_details','purchase_account'])

এখানে ২টা relationship load হচ্ছে:

Relation Name

Actual Table

_master_details

purchase_details

purchase_account

purchase_accounts

purchase এর সাথে related সব details (items)

purchase এর সাথে related account info



🔹 Relationship কিভাবে define করা থাকে?

👉 Purchase Model এ:

public function _master_details(){
return $this->hasMany(PurchaseDetail::class,'purchase_id','id');
}

👉 মানে:

  • 1টা purchase → অনেক purchase_details

Shape1

public function purchase_account(){
return $this->belongsTo(PurchaseAccount::class,'account_id','id');
}

👉 মানে:

  • 1টা purchase → 1টা account


where('id',$invoice_id)

purchase table থেকে specific invoice খুঁজে বের করছে

SELECT * FROM purchase WHERE id = $invoice_id;

first()

👉 matching data থেকে ১টা row return করবে
👉 object
আকারে $data তে রাখবে



https://chatgpt.com/c/69c8ec61-2b08-83a4-9a48-a30e81b7a91b








explode() হলো PHP এর একটি built-in function
👉 এটি একটি string কে ভেঙে array বানায়

$users->branch_ids = "1,2,3,5";



$branch_array = explode(',', $users->branch_ids);

Output:

[
"1",
"2",
"3",
"5"
]





implode(',', [1,2,3]);

output:

"1,2,3"



Summary

Function

কাজ

explode()

string → array

implode()

array → string





Summary

👉 with() → relation load
👉
_master_details → purchase_details table
👉
purchase_account → purchase_accounts table
👉
first() → single record







if (! function_exists('permited_branch')) {

function permited_branch($branch_ids)

{

return Branch::whereIn('id',$branch_ids)->select('id','_name')->get();

}

}

if (! function_exists('permited_branch'))

👉 এর মানে:

  • আগে চেক করছে permited_branch নামে কোনো function আগে থেকে আছে কিনা।

  • ! মানে না থাকলে।

  • ! মানে না থাকলে।

✅ কাজ:

  • একই নামের function দুইবার declare হলে error হয়।

  • তাই আগে check করে নিচ্ছে।

function permited_branch($branch_ids)

👉 এখানে একটি function declare করা হচ্ছে।

📌 $branch_ids:

  • এটি একটি parameter (input)

  • সাধারণত এটি একটি array হবে (যেমন: [1,2,3])

return Branch::whereIn('id',$branch_ids)

👉 এখানে Laravel Eloquent ORM ব্যবহার করা হয়েছে।

📌 Branch:

  • এটি একটি Model (যেটা database এর branches table এর সাথে যুক্ত)

📌 whereIn('id', $branch_ids):

  • SQL এর মতো:

SELECT * FROM branches WHERE id IN (1,2,3)

  • অর্থাৎ $branch_ids array এর মধ্যে যেসব id আছে, সেগুলো filter করবে।





->select('id','_name')

👉 এখানে নির্দিষ্ট column নেওয়া হচ্ছে।

📌 মানে:

  • শুধু id এবং _name column return করবে



->get();

👉 Query execute করে data নিয়ে আসে।