In Laravel,How to Use Foreach Loop

laravel foreach loop in controller

This will throw an error:

foreach ($product->sku as $sku){ 
// Code Here
}

because you cannot loop a model with a specific column ($product->sku) from the table.


So, you must loop on the whole model:

foreach ($product as $p) {
// code
}

Inside the loop you can retrieve whatever column you want just adding ->[column_name]

foreach ($product as $p) {
echo $p->sku;
}

foreach loop in laravel controller

$livraison is array not object

foreach( $request->livraison as $livraison)
{
$produit = Produit::find($livraison['produit_id']);
}

How to use for loop instead of foreach loop in Laravel controller?

Converted your foreach to for loop

   $client = Client::all();
$count = count($client);
$a_name = "ayman";

for($i=0; $i < $count ; $i++) {
if ($client[$i]->name == $a_name)
//your code
}
}

how to use foreach loop with css

Just change the placement of loop. You can do it as following:

<div class="row">
@foreach ($data as $key => $jobs)
<div class="col-sm-3">
<div class="card">
<div class="card-body">
<h5 class="card-title">{{$jobs->tilte}}</h5>
<p class="card-text">{{$jobs->final_sub}}</p>
<a href="#" class="btn btn-primary">Apply</a>
</div>
</div>
</div>
@endforeach
</div>

foreach limit loop break blade laravel

Instead of foreach loop use laravel query to get 5 record with cat_select = 4 using whereRaw query

$cats = DB::table('blog')->whereRaw("find_in_set('4',cat)")->limit(5)->get();

@foreach($cats as $row)
{{$row->id}} | {{$row->title}}
@endforeach

so decrease the code and get faster output.

How to do a foreach loop with an object in laravel

You have to first json_decode

$myData = json_decode($project->assigned);

Then use foreach

foreach($myData as $arrayItem){

$user = Admin::select('name', 'numero_empleado')->where('numero_empleado', $arrayItem)->get();
$assigned = $user;
}

How to get data from foreach loop result

You should use arraypush function to get result.

 $attendance = DB::table('attendance')
->where('company_id', $request->company_id)
->where(DB::raw("in_out"),"=", "in")
->where(DB::raw("(STR_TO_DATE(attendance_time,'%Y-%m-%d'))"), "=", $date)->get();

$employee_checkin = [];
foreach($attendance as $atd){

// return $data[] = $atd->employee_id;

$employee_in_company = DB::table('employee_in_company')
->join('employee', 'employee.id', '=', 'employee_in_company.employee_id')
->where('employee_in_company.company_id', $atd->company_id)
->where(DB::raw("employee.id"),"!=", $atd->employee_id)
->select('employee.name as name_company', 'employee_in_company.company_id',
'employee_in_company.employee_id')
->get();

array_push($employee_checkin, $employee_in_company);

}

return $employee_checkin;


Related Topics



Leave a reply



Submit