character corruption in foreach in blade Laravel - laravel

What should I do to fix this problem?
to output all data in foreach.
public function index()
{
$workSchedules = DB::table('schedules')
->join('posts', 'schedules.work_id', '=', 'posts.user_id')->
leftJoin('users', 'users.id', '=', 'worker_schedules.work_id')->
get();
Log::debug($Schedules);
return view('index', ['posts' => $Schedules ]);
}
Result:
[{"worker_id":4,"name":"\u304a\","title":"\u304a\u3063\u3068\u3063\u3068","body":"\u5bfe\u9593","cost":2000,"available":"1,2,3,4"},{"worker_id":6,"name":"\u304a\","title":"\u304a\u3063\u3068\u3063\u3068\","body":"\u5bfe\u9593","cost":2200,"available":"1,2,3,4"}]

Related

Laravel 8 - How to use where conditions for relation's column

hello friends can you help me to get the contents of $filter? I want to run where which is where the target column is in the relation array
$filter = $request->get('Biogear');
$data = DetailBarang::with(['barang' => function ($q) use ($filter) {
$q->where('brand', '=', $filter);
}])->get();
return response()->json($data);
you can try this
$filter = $request->get('Biogear');
$data = DetailBarang::with('barang')->whereHas('barang',function (Illuminate\Database\Eloquent\Builder $q) use ($filter) {
$q->where('brand', '=', $filter);
})->get();
return response()->json($data);

Search result with multiple tables relation in Laravel6

I'm trying to make search function with relation.
I could display all result using relation And an another code, simple one table search and get result works fine.
but I can't combine those two.
Could you teach me how to combine multiple tables into search function please?
public function order(Request $request)
{
$data = $request->all();
$products = Product::All();
$products = Product::with('categori')
->join('creators', 'creators.id', '=', 'products.creator_id')
->join('categoris', 'categoris.id', '=', 'products.categori_id')
->join('branches', 'branches.id', '=', 'products.br_id')
->join('users', 'users.id', '=', 'products.user_id')
->join('colors', 'colors.id', '=', 'products.color_id')
->get();
$products = Product::when($data['categori_id'], function ($query, $categori_id) {
return $query->where('categori_id', $categori_id);
})->
when($data['color_id'], function ($query, $color_id) {
return $query->where('color_id', $color_id);
})->get();
//return view('result_fb', compact('images'));
$data = array(
'title' => 'index',
'no' => 1,
'products' => $products,
);
return view('product.result', $data);
}
I am not sure if you need 'categori' relation to be loaded or not as there are few $products without it. For general, you can combine all your $products queries to one as follows:
$products = Product::join('creators', 'creators.id', '=', 'products.creator_id')
->join('categoris', 'categoris.id', '=', 'products.categori_id')
->join('branches', 'branches.id', '=', 'products.br_id')
->join('users', 'users.id', '=', 'products.user_id')
->join('colors', 'colors.id', '=', 'products.color_id')
->when($data['categori_id'], function ($query, $categori_id) use ($data) {
return $query->where('categori_id', $data['categori_id']);
})
->when($data['color_id'], function ($query) use ($data) {
return $query->where('color_id', $data['color_id']);
})->get();
If you want to use eager loading with eloquent rather then join, you can use like
$products = Product::with(['creator','categori','branch','user','color'])
->when($data['categori_id'], function ($query, $categori_id) use ($data) {
return $query->where('categori_id', $data['categori_id']);
})
->when($data['color_id'], function ($query) use ($data) {
return $query->where('color_id', $data['color_id']);
})->get();
But for that you Product model should have relations with same name as follows for categori relation:
class Product extends Model
{
public function categori()
{
return $this->belongsTo(Categori::class,'categori_id','id);
}
}
Same way you have to define for all relation used for product.
Try this:
$products = Product::with('categori')
->join('creators', 'creators.id', '=', 'products.creator_id')
->join('categoris', 'categoris.id', '=', 'products.categori_id')
->join('branches', 'branches.id', '=', 'products.br_id')
->join('users', 'users.id', '=', 'products.user_id')
->join('colors', 'colors.id', '=', 'products.color_id')
->when($data['categori_id'], function ($query, $categori_id) {
return $query->where('categori_id', $categori_id);
})
->when($data['color_id'], function ($query, $color_id) {
return $query->where('color_id', $color_id);
})
->get();

How to declare the result from another function in Laravel

The first 2 functions are for the dropdown, and the last function filter function is to fetch the data into datatable. I want to use the results from function FirstName ($FirstName= [];) and LastName ($LastName= [];) for this purpose.
public function FirstName(Request $request)
{
$FirstName= [];
if($request->has('q')){
$search = $request->q;
$FirstName= DB::table("user")
->select("id","First_Name")
->where('First_Name','LIKE',"%$search%")
->get();
}
return response()->json($FirstName);
}
public function LastName(Request $request)
{
$LastName= [];
if($request->has('q')){
$search = $request->q;
$LastName= DB::table("user")
->select("id","Last_Name")
->where('Last_Name','LIKE',"%$search%")
->get();
}
return response()->json($Last_Name);
}
public function filter()
{
if(!empty($FirstName)) //catch from other function
{
$data = DB::table('user')
->select('id', 'Fn', 'Ln')
->where('First_Name', $FirstName)
->where('Last_Name', $LastName)
->get();
}
else
{
$data = DB::table('user')
->select('id', 'Fn', 'Ln')
->get();
}
return datatables()->of($data)->make(true);
$data = DB::table('user')
->select('Last_Name','First_Name')
->groupBy('First_Name')
->groupBy('Last_Name')
->orderBy('First_Name', 'ASC')
->orderBy('Last_Name', 'ASC')
->get();
return response()->json($data);
}
What I did above is put the array from function first and last name to the filter function. But i got it wrong. What should be done to fix this?

laravel 404 error page , redirect

looking for some help, I'm fetching a survey and questions, from method, when select some survey by id, from a route. But when I delete a survey, and when writing in url like http://localhost/survey_details/27 ( 27 is survey Id , which not exist anymore), I'm getting not some expection or redirection or Page not found info, but getting error where undefined variable $dat. But I need some how to show 404 page not found, or just redirect to other page if id not exist. Can some one help me?
here is my route:
Route::get('survey_draft/{id}', 'SurveyController#editSurveyDraft');
Here is my controller:
public function viewSurvey($id)
{
$object = DB::table('question')
->where('survey_id', '=', $id)
->where('name', '!=', NULL)
->get();
$teamName = DB::table('survey')->where('surveyId', '=', $id)
->join('team', 'survey.teamId', '=', 'team.teamId')
->join('company', 'company.id', '=', 'team.companyID')
->get();
$company = Auth::user()->company;
$data = Survey::where('surveyId', '=', $id)->get();
$teams = Auth::user()->teams;
$checkUserTeam = DB::table('teammembersall')
->where('UserId', '=', Auth::user()->id)
->get();
$members = Survey::where('surveyId', '=', $id)
->join('team', 'team.teamId', '=', 'survey.teamId')
->join('teammembersall', 'teammembersall.TeamId', '=', 'team.TeamId')
->join('users', 'users.id', '=', 'teammembersall.UserId')
->select('users.*')
->whereNotExists(function ($query) use ($id) {
$query->select(DB::raw(1))
->from('answer')
->whereRaw('answer.answerAboutUserId = users.id')
->where('answer.surveyId', '=', $id)
->where('answer.member_id', '=', Auth::user()->id);
})
->get();
$questions = DB::table('answer')->get();
return view('survey_details', ['object' => $object, 'data' => $data, 'teams' => $teams, 'members' => $members, 'questions' => $questions, 'checkUserTeam' => $checkUserTeam, 'teamName' => $teamName, 'company' => $company]);
}
To solve your immediate issue, you could add one of these this after $object ... get() depending on your result. One of them should work.
if(empty($object)){ abort(404); }
or
if(!$object->count()){ abort(404); }
However, your code would be much simpler if you used 2 basic laravel technologies.
ORM instead of DB::...
Route model binding. (which would handle your 404 for you)
https://laravel.com/docs/5.6/eloquent
https://laravel.com/docs/5.6/routing#route-model-binding

Access variable from other function LARAVEL

I'm trying to use the returned result from a function into another function. Here is my first function:
public function availableRooms(){
if(isset($_POST['selectedDate'])){
$checkInDateReceived = str_replace('/', '-', $_POST['selectedDate']);
$checkInDate = date('Y-m-d', strtotime($checkInDateReceived));
$availableRooms = DB::table('rooms')
->leftJoin('reservations', function ($secondJoin) use($checkInDate) {
$secondJoin->where('reservations.checkout_date', '<', $checkInDate);
})
->leftJoin('reservation_room_rel', function ($firstJoin) {
$firstJoin
->on('reservation_room_rel.room_id', '=', 'rooms.id')
->on('reservation_room_rel.reservation_id', '=', 'reservations.id');
})
->where([
['rooms.status', '=', 1]
])
->whereNull('reservation_room_rel.room_id')
->get();
return $availableRooms;
}
}
And here is my second function:
public function returnAddReservation(){
$availableRoomsRet = $this->availableRooms();
$mealSupplement = new Meal;
$accommodationSupplement = new AccommodationSupplement;
$accommodationSupplements = $accommodationSupplement::where('status', 1)
->orderBy('name', 'asc')
->get();
$mealSupplements = $mealSupplement::where('status', 1)
->orderBy('name', 'asc')
->get();
return view('addReservation', [
'availableRooms' => $availableRoomsRet,
'accommodationSupplements' => $accommodationSupplements,
'mealSupplements' => $mealSupplements
]);
}
First function returns correct result if it runs independently, but, when I dd($availableRoomsRet)it returns null.
Any idea how to access properly $availableRooms from public function availableRooms() into public function returnAddReservation() ?
Thanks!

Resources